用fminsearch同步两对曲线的交点 [英] Synchronize intersection points of two pairs of curves with fminsearch

查看:116
本文介绍了用fminsearch同步两对曲线的交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两对曲线,每对曲线在不同的时间值(x值)处都有一个交点.现在,我需要移动每对中的一条曲线在x方向上,直到两个交点具有相同的时间值/x参数:

I have two pairs of curves and every pair has an intersection point at a different time value (x-value). Now I need to move one curve of each pair equally in x-direction until both intersection points have the same time value/x-argument:

这些只是示例,您可以为我所有的真实情况(如示例中)假定相同的单调性(但形状可能不同).同样,相同颜色的曲线具有相同的x矢量(但不一定等距).在两种颜色(1 =红色和2 =蓝色)之间 x范围和元素数可以完全不同.

These are just examples, you can assume the same monotonicity (but maybe different shapes) for all my real cases like in my example. Also curves of the same color have the same x-vector (but not necessarily equidistant). Between two colors (1=red and 2=blue) x-range and the number of elements can be completely different.

输入曲线的产生者:

t1 = linspace(0,3,30);
t2 = linspace(0,5,15);
x1 = t1.^2.*5;
x2 = -t2.^2.*3+50;
y1 = t1.*2;
y2 = t2;

ip_x = InterX([t1;x1],[t2;x2]);
ip_y = InterX([t1;y1],[t2;y2]);

我在 文件交换 ,该函数返回交点的xy值.该示例的预期输出只是用来说明我的问题.

The intersections I calculate by using the function InterX at File Exchange which returns the x and y value of the intersection point. The intended output for the example is just guessed to illustrate my problem.

[ t1,x1,y1,t2,x2,y2 ] = findIntersectPair(t1,x1,y1,t2,x2,y2);

我的方法是使用fminsearch,但是经过几次迭代后,我遇到了问题.

My approach is using fminsearch but I run into problems, after some iterations.

function [ t1,x1,y1,t2,x2,y2 ] = findIntersectPair(t1,x1,y1,t2,x2,y2)

d0 = 0;

[d,dxy] = fminsearch(@findDelay,d0);

function dxy = findDelay( d )
    disp(['d = ' num2str(d)])
    t2 = t2 - d;
    ip1 = InterX([t1;x1],[t2;x2]);
    ip2 = InterX([t1;y1],[t2;y2]);
    dxy = ip1(1)-ip2(1);
    disp(['dxy = ' num2str(dxy)])
end


[t1,x1,y1,x2,y2] = deal(t1,x1,y1,x2,y2);
t2 = t2 - d;

end

d0开头,应增加,直到dxy(在这种情况下以2.5开头)成为0.

d starts with 0 and should be increased, until dxy (starting with 2.5 in this case) becomes 0.

这似乎在第一次迭代中效果很好,但是在某一点d的变化很大,因此不再有交点并且函数崩溃:

This seems to work nicely for the first iterations, but at one point the change of d is to big, so that there is no intersection point anymore and the function crashes:

d = 0
dxy = 2.4998
d = 0.00025
dxy = 2.4995
...
d = 0.00175
dxy = 2.4936
d = 0.00275
dxy = 2.4898
...
d = 0.38375
dxy = 0.67101
d = 0.51175
dxy = -0.11166
d = 0.76775

逻辑上的结论是改为使用fmincon.但是我缺少 Optimization Toolbox .有什么办法吗?

推荐答案

不需要fmincon-错误最终是一个非常简单的错误.

There is no need for fmincon - the error finally was a quite simple one.

在这一行中:t2 = t2 - d我假设t2是来自初始函数调用的原始代码,但是那是不对的.每次迭代都会覆盖它,因此会逐渐增加.

In this line: t2 = t2 - d I assummed t2 would be the original one from the initial function call, but thats not right. It is overwritten with every iteration and therefore increasing gradually.

一个临时变量tt解决了该问题.

A temporary variable tt solved the problem.

最终结果:

function [ t1,x1,y1,t2,x2,y2 ] = findIntersectPair(t1,x1,y1,t2,x2,y2)

[d,dxy] = fminsearch(@findDelay,0);

function dxy = findDelay( d )
    tt = t2 - d;
    ipx = InterX([t1;x1],[tt;x2]);
    ipy = InterX([t1;y1],[tt;y2]);
    dxy = abs(ipx(1)-ipy(1));
end

[t1,x1,y1,x2,y2] = deal(t1,x1,y1,x2,y2);
t2 = t2 - d;

end

给出所需的情节:

这篇关于用fminsearch同步两对曲线的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆