Reputation: 1661
I want to plot relations like y^2=x^2(x+3)
in MATLAB without using ezplot
or doing algebra to find each branch of the function.
Does anyone know how I can do this? I usually create a linspace
and then create a function over the linspace
. For example
x=linspace(-pi,pi,1001);
f=sin(x);
plot(x,f)
Can I do something similar for the relation I have provided?
Upvotes: 1
Views: 787
Reputation: 4336
n = 1000;
[x y] = meshgrid(linspace(-3,3,n),linspace(-3,3,n));
z = nan(n,n);
z = (y .^ 2 <= x .^2 .* (x + 3) + .1);
z = z & (y .^ 2 >= x .^2 .* (x + 3) - .1);
contour(x,y,z)
It's probably not what you want, but I it's pretty cool!
Upvotes: 2
Reputation: 13945
Ok I was about to write my answer and I just saw that @rayryeng proposed a similar idea (Good job Ray!) but here it goes. The idea is also to use solve to get an expression for y, then convert the symbolic function to an anonymous function and then plot it. The code is general for any number of solutions you get from solve:
clear
clc
close all
syms x y
FunXY = y^2 == x^2*(x+3);
%//Use solve to solve for y.
Y = solve(FunXY,y);
%// Create anonymous functions, stored in a cell array.
NumSol = numel(Y); %// Number of solutions.
G = cell(1,NumSol);
for k = 1:NumSol
G{k} = matlabFunction(Y(k))
end
%// Plot the functions...
figure
hold on
for PlotCounter = 1:NumSol
fplot(G{PlotCounter},[-pi,pi])
end
hold off
The result is the following:
Upvotes: 4
Reputation: 104503
What you could do is use solve
and allow MATLAB's symbolic solver to symbolically solve for an expression of y
in terms of x
. Once you do this, you can use subs
to substitute values of x
into the expression found from solve
and plot all of these together. Bear in mind that you will need to cast the result of subs
with double
because you want the numerical result of the substitution. Not doing this will still leave the answer in MATLAB's symbolic format, and it is incompatible for use when you want to plot the final points on your graph.
Also, what you'll need to do is that given equations like what you have posted above, you may have to loop over each solution, substitute your values of x
into each, then add them to the plot.
Something like the following. Here, you also have control over the domain as you have desired:
syms x y;
eqn = solve('y^2 == x^2*(x+3)', 'y'); %// Solve for y, as an expression of x
xval = linspace(-1, 1, 1000);
%// Spawn a blank figure and remember stuff as we throw it in
figure;
hold on;
%// For as many solutions as we have...
for idx = 1 : numel(eqn)
%// Substitute our values of x into each solution
yval = double(subs(eqn(idx), xval));
%// Plot the points
plot(xval, yval);
end
%// Add a grid
grid;
Take special care of how I used solve
. I specified y
because I want to solve for y
, which will give me an expression in terms of x
. x
is our independent variable, and so this is important. I then specify a grid of x
points from -1 to 1 - exactly 1000 points actually. I spawn a blank figure, then for as many solutions to the equation that we have, we determine the output y
values for each solution we have given the x
values that I made earlier. I then plot these on a graph of these points. Note that I used hold on
to add more points with each invocation to plot
. If I didn't do this, the figure would refresh itself and only remember the most recent call to plot
. You want to put all of the points on here generated from all of the solution. For some neatness, I threw a grid in.
This is what I get:
Upvotes: 4