Plot two equations and find the intersect

I want to solve numerically for the intersects, plot the curve and line and show the intersects on the plot . E is the x axis and y is the y axis. We have;
g=9.81;v=1;y1=2;q=v*y1;z=0.9;
Syms E y2
eqn1 = E==2*g*y2^3+y2^2(2*g*z-2*g*y-q^2/y1^2)+q^2 = 0;
eqn2 = E==3;
That is as far as I got.
Thanks in advance.

 Accepted Answer

Try this
g=9.81;v=1;y1=2;q=v*y1;z=0.9;
syms E y2
E1 = 2*g*y2^3+y2^2*(2*g*z-2*g*y1-q^2/y1^2)+q^2;
E2 = 3;
sol = double(solve(E1==E2));
fplot(E1, [-0.5 1.5]) % plot from -5 to 5 on x-axis
hold on;
fplot(E2, [-0.5 1.5])
plot(sol, E2*ones(size(sol)), 'ro');

8 Comments

But E should be on the x axis, so the line E = 3 would be a vertical line upwards.
Do you want E on the horizontal axis and y on vertical axis?
There is no need to use syms for this at all. But you do need to use operators to indicate multiplication. As well, the .* and .^ operators will be used to allow vectorized evaluation. The ./ operator is also useful sometimes but not necessary here, because y1 is a scalar.
g=9.81; v=1; y1=2; q=v*y1; z=0.9;
E1 = @(E,y2) 2*g*y2.^3+y2.^2.*(2*g*z-2*g*y1-q^2/y1.^2)+q^2 - E;
fimplicit(E1,[-1 6 -0.5 1.5])
xline(3);
I've deliberately set the axis limits where they are to focus on the region of interest.
You need to use fimplicit because the relationship is not a single valued one, if we treat this having y2 be a function of E.
@John, thanks for your comment. I used the symbolic toolbox to find the point of intersection. It will be complicated using fsolve. Following extend your code to add the points of intersection
g=9.81; v=1; y1=2; q=v*y1; z=0.9;
syms E y
E = 2*g*y^3+y^2*(2*g*z-2*g*y1-q^2/y1^2)+q^2;
sol = double(solve(E==3));
E1 = @(E,y2) 2*g*y2.^3+y2.^2.*(2*g*z-2*g*y1-q^2/y1.^2)+q^2 - E;
fimplicit(E1,[-1 6 -0.5 1.5])
xline(3);
hold on;
plot(3*ones(size(sol)), sol, 'o', 'LineWidth', 1)
Thanks John and Ameer,
Just one more clarification.
So here we are using fimplicit, which forces E to be on the x axis?
Brian, the function handle E1, has two inputs. fimplicit plot 1st on x and 2nd on y-axis.
Exactly. Had I created the function handle as:
E1 = @(y2,E) 2*g*y2.^3+y2.^2.*(2*g*z-2*g*y1-q^2/y1.^2)+q^2 - E;
then E would be on the y axis.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!