Frage

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:

enter image description here

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.

input curves generated by:

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]);

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.

My question is, how can I determine the time delay between input and output?

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

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

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

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

The logical conclusion would be to use fmincon instead. But I'm lacking of the Optimization Toolbox. Is there any way around?

War es hilfreich?

Lösung

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

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.

A temporary variable tt solved the problem.

Final result:

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

gives the desired plot:

enter image description here

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top