Graph intersection of 3 curves

4 views (last 30 days)
J Neto
J Neto on 8 Dec 2014
Commented: Star Strider on 8 Dec 2014
I have 3 functions:
x=-1:0.001:1;
y1=((3*x.^2)-1)/2;
y2=((5*x.^3)-(3*x))/2;
y3=((35*x.^4)-(30*x.^2)+3)/8;
plot(x,y1,'g-',x,y2,'r--',x,y3,'b-.');
I need to find all the interception points in the graphic.
The result should look like this:

Accepted Answer

Star Strider
Star Strider on 8 Dec 2014
Edited: Star Strider on 8 Dec 2014
This was a fun project!
y1 = @(x) ((3*x.^2)-1)/2;
y2 = @(x) ((5*x.^3)-(3*x))/2;
y3 = @(x) ((35*x.^4)-(30*x.^2)+3)/8;
e0 = linspace(-1,1,10);
for k1 = 1:length(e0)
y1y2(k1) = fzero(@(x) (y1(x)-y2(x)), e0(k1));
y1y3(k1) = fzero(@(x) (y1(x)-y3(x)), e0(k1));
y2y3(k1) = fzero(@(x) (y2(x)-y3(x)), e0(k1));
end
y1y2 = unique(round(y1y2*1E+3)/1E+3);
y1y3 = unique(round(y1y3*1E+3)/1E+3);
y2y3 = unique(round(y2y3*1E+3)/1E+3);
x = linspace(-1,1);
figure(1)
plot(x,y1(x),'g-',x,y2(x),'r--',x,y3(x),'b-.');
hold on
plot(y1y2, y1(y1y2), 'kp', 'MarkerSize',8, 'MarkerFaceColor','y')
plot(y1y3, y3(y1y3), 'kp', 'MarkerSize',8, 'MarkerFaceColor','y')
plot(y2y3, y2(y2y3), 'kp', 'MarkerSize',8, 'MarkerFaceColor','y')
hold off
This might be written to be more efficient, but it works!
  2 Comments
J Neto
J Neto on 8 Dec 2014
Thank you so much, perfect solution!
Star Strider
Star Strider on 8 Dec 2014
My pleasure!
See Roger’s answer for (as usual) a more insightful solution. It was late and I honestly didn’t see that possibility.

Sign in to comment.

More Answers (1)

Roger Stafford
Roger Stafford on 8 Dec 2014
Use 'roots' on the cubic polynomial difference between y1 and y2 to get the three intersections of the y1 and y2 curves. Similarly the difference between y1 and y3 and the difference between y2 and y3 will give quartic polynomials that produce four roots using 'roots'.

Categories

Find more on Two y-axis in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!