What's wrong with my code for this plot?

조회 수: 4 (최근 30일)
Josie
Josie 2014년 8월 25일
댓글: Star Strider 2014년 8월 25일
I'm trying to plot drag D against mass M for various values of the ratio R between the inertia I and mass M, i.e. R=I/M but when I plot this I only get the R=0.5 line. How do I fix this?
M=linspace(0,1,50);
I=linspace(0,1,50);
A=0.7;
g=10;
for R=[0.02, 0.1, 0.2, 0.5];
R=I/M;
s=(A-0.5)*M.*g-(A^2)/4;
theta=s./(2*M.*(A-0.5)^2+2*I+(A^2-A+1/3)/2);
D=(A.^2-A+1/3).*theta+0.5*A.^2;
plot(M,D)
axis([0,1,-1,1])
xlabel('M')
ylabel('D')
hold all
grid on
end
legend('R=0.02','R=0.1','R=0.2','R=0.5')

채택된 답변

Star Strider
Star Strider 2014년 8월 25일
One possible problem is that R isn’t used to calculate D (or anything else), and never changes because:
R=I/M;
in every iteration of the loop. D doesn’t change either.
  댓글 수: 2
Josie
Josie 2014년 8월 25일
Thanks, could I instead overlap 4 different graphs on top of each other like this but then including I=0.1*M, I=0.2*M, I=0.5*M as the overlapping graphs? How would I do this?
M=linspace(0,1,50);
A=0.7;
g=10;
I=0.02*M;
s=(A-0.5)*M.*g-(A^2)/4;
theta=s./(2*M.*(A-0.5)^2+2*I+(A^2-A+1/3)/2);
D=(A.^2-A+1/3).*theta+0.5*A.^2;
plot(M,D)
axis([0,1,-1,1])
xlabel('M')
ylabel('D')
hold all
grid on
legend('R=0.02','R=0.1','R=0.2','R=0.5')
Star Strider
Star Strider 2014년 8월 25일
I am still not certain what you are doing, but this gives you three lines, one for each value of Im:
M=linspace(0,1,50);
A=0.7;
g=10;
Im = [0.1 0.2 0.5];
for k1 = 1:length(Im)
I = Im(k1)*M;
s=(A-0.5)*M.*g-(A^2)/4;
theta=s./(2*M.*(A-0.5)^2+2*I+(A^2-A+1/3)/2);
D=(A.^2-A+1/3).*theta+0.5*A.^2;
plot(M,D)
axis([0,1,-1,1])
xlabel('M')
ylabel('D')
hold all
grid on
end
legend('R=0.02','R=0.1','R=0.2','R=0.5')

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Yona
Yona 2014년 8월 25일
you change R in the loop to be same each time (R=I/M;) so you get the exactly same graph 4 time.
they are each on other.
by the way, axis, xlabel, ylabel and grin can be after the loop, it not need to be in the loop.
  댓글 수: 1
Josie
Josie 2014년 8월 25일
Thanks, could I instead overlap 4 different graphs on top of each other like this but then including I=0.1*M, I=0.2*M, I=0.5*M as the overlapping graphs? How would I do this?
M=linspace(0,1,50);
A=0.7;
g=10;
I=0.02*M;
s=(A-0.5)*M.*g-(A^2)/4;
theta=s./(2*M.*(A-0.5)^2+2*I+(A^2-A+1/3)/2);
D=(A.^2-A+1/3).*theta+0.5*A.^2;
plot(M,D)
axis([0,1,-1,1])
xlabel('M')
ylabel('D')
hold all
grid on
legend('R=0.02','R=0.1','R=0.2','R=0.5')

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by