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:

 Akzeptierte Antwort

Star Strider
Star Strider am 8 Dez. 2014
Bearbeitet: Star Strider am 8 Dez. 2014

1 Stimme

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 Kommentare

J Neto
J Neto am 8 Dez. 2014
Thank you so much, perfect solution!
Star Strider
Star Strider am 8 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Roger Stafford
Roger Stafford am 8 Dez. 2014

1 Stimme

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'.

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by