I need to plot P=0.5+thetax on 0<x<0.33 and then P=theta(x-1) on 0.33<x<1. Do i use a for loop to do this? And if so how? Here is my code so far
x=linspace(0,1,50);
g=10;
A=0.33
M=0.125
s=(A-0.5)*M.*g-(A^2)/4;
theta=s./(2*M.*(A-0.5)^2+2*M+(A^2-A+1/3)/2);
P=(1/2)+theta*(x-1);
plot(x,P)
axis([0,1,-2,2])

 Akzeptierte Antwort

Ben11
Ben11 am 20 Aug. 2014
Bearbeitet: Ben11 am 20 Aug. 2014

0 Stimmen

Maybe something like this:
x=linspace(0,1,50)
g=10;
A=0.33
M=0.125
s=(A-0.5)*M.*g-(A^2)/4;
theta=s./(2*M.*(A-0.5)^2+2*M+(A^2-A+1/3)/2);
P=(1/2)+theta*(x-1);
xS = x< 0.33 % define x < 0.33
xL = x>0.33 % define x > 0.33
figure
plot(x(xS),0.5+theta*x(xS))
hold on
plot(x(xL),theta*(x(xL)-1))
hold off
axis([0,1,-2,2])

4 Kommentare

Josie
Josie am 20 Aug. 2014
Thanks! Is there any way to get a vertical line joining the two?
Ben11
Ben11 am 20 Aug. 2014
Yes there is! I modified the code a bit so it's easier to play around with it:
x=linspace(0,1,50);
g=10;
A=0.33;
M=0.125;
s=(A-0.5)*M.*g-(A^2)/4;
theta=s./(2*M.*(A-0.5)^2+2*M+(A^2-A+1/3)/2);
P=(1/2)+theta*(x-1);
xS = x(x < 0.33);
xL = x(x > 0.33);
StartLine = 0.5+theta*(xS(end)); % Retrieve x values for starting/ending points of the line
EndLine = theta*(xL(1)-1);
figure
plot(xS,0.5+theta*xS)
hold on
plot(xL,theta*(xL-1))
line([xS(end) xL(1)],[StartLine EndLine],'Color','r','LineWidth',2)
hold off
axis([0,1,-2,2])
The line is thicker than the plots and red as well; you can customize this as you wish :)
Josie
Josie am 21 Aug. 2014
Great! Thank you
Ben11
Ben11 am 21 Aug. 2014
My pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 20 Aug. 2014

Kommentiert:

am 21 Aug. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by