How to define which variable is x and which variable is y axis?

I have an equation like "2*c^2 - 4*b = 0" with b ranging from 0 to 10. I want to plot a graph of b verses c with b on the x axis. How would I tell the program to plot b on the x axis and c on the y axis?

Respuestas (4)

Mischa Kim
Mischa Kim el 7 de Oct. de 2014
Editada: Mischa Kim el 7 de Oct. de 2014
John, simply use
plot(b,c)
The first argument is the x the second the y axis. Or, if you want to show both solution branches
b = 0:0.1:10;
c = sqrt(2*b);
plot(b,c,b,-c)
First, a bit of algebra to create ‘c’ as a function of ‘b’, then it’s simply another plot:
b = linspace(0,10);
c = [-sqrt(2*b); sqrt(2*b)];
figure(1)
plot(b, c)
grid

4 comentarios

The ‘V’ for your ‘new’ equation is complex, so it’s easier to simply evaluate it at each ‘b’ and plot it:
F = @(b) [pi+atan(b./(1-b)).^(1/2)+atan((b+2).*(1-b)).^(1/2)]./(1-b).^(1/2);
b = linspace(-100,100.500);
plot(real(F(b)),b, '-r', imag(F(b)),b, '-g')
xlabel('V(b)')
ylabel('b')
legend('Re(V)', 'Im(V)')
grid
Ok thank you very much for this!
My pleasure!
The sincerest expression of appreciation here on MATLAB Answers is to Accept the Answer that most closely solves your problem.
Andrew Reibold
Andrew Reibold el 7 de Oct. de 2014
Editada: Andrew Reibold el 7 de Oct. de 2014
Alternatively, PayPal is accepted too xD

Iniciar sesión para comentar.

John
John el 7 de Oct. de 2014
Thanks that clears up a lot however if I can't separate b into one term is there a way to plot it. This is the actual equation:
V=[pi+arctan(b/(1-b))^(1/2)+arctan((b+2)(1-b))^(1/2)]/(1-b)^(1/2)
I want V to be the x axis from 0 to 15. I want to solve for b and plot the results on the y axis.
Matt J
Matt J el 7 de Oct. de 2014
Editada: Matt J el 7 de Oct. de 2014
f = @(V,b)V-(pi+atan(b./(1-b)).^(1./2)+atan((b+2).*(1-b)).^(1./2))./(1-b).^(1./2);
ezplot(f,[0,1.1,15,100]);

3 comentarios

Mohammad Abouali
Mohammad Abouali el 7 de Oct. de 2014
Editada: Mohammad Abouali el 7 de Oct. de 2014
he wants 0<V<15 so ezplot(f,[0,15]); with f being defined as you suggested.
and that is still not correct, since in those range the function f returns a complex number and what is being plotted is the norm of that complex number.
Mohammad Abouali
Mohammad Abouali el 7 de Oct. de 2014
Editada: Mohammad Abouali el 7 de Oct. de 2014
Yes, with x being V and y being b. so xmin=0 xmax=15
and again put b=1.1 and check what your V turns out to be.
ff=@(b) (pi+atan(b./(1-b)).^(1./2)+atan((b+2).*(1-b)).^(1./2))./(1-b).^(1./2);
V would be a complex number
He wants 0<V<15 on x axis
Matt J
Matt J el 7 de Oct. de 2014
Editada: Matt J el 7 de Oct. de 2014
Right you are (although I think it is the real part and not the norm that is being plotted). Here's a modification that fixes that issue,
function doPlot
ezplot(@(V,b) f(V,b),[0,15,0,1]);
function fval = f(V,b)
fval = V-(pi+atan(b./(1-b)).^(1./2)+atan((b+2).*(1-b)).^(1./2))./(1-b).^(1./2);
fval=fval/(imag(fval)==0);
end
end

Iniciar sesión para comentar.

Categorías

Preguntada:

el 7 de Oct. de 2014

Editada:

el 7 de Oct. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by