在MATLAB中从for循环绘制图形

时间:2015-11-09 05:45:43

标签: matlab for-loop graph plot matlab-figure

我有这个for循环

for mass = 1:.1:4
theta = 60;
Fw = [0 -9.8*mass 0];
Rw = [ 0.2*cosd(theta) 0.2*sind(theta) 0];
Mw = cross(Rw,Fw);
Fjrf = [30 50 0];
Rjrf = [0.4*cosd(theta) 0.4*sind(theta) 0];
Mjrf = cross(Rjrf,Fjrf);
alpha = 5;
Ialpha = [0 0 alpha*(0.02+mass*0.2^2)];
Ms = Ialpha - Mjrf - Mw;
omega = [0 0 pi];
alphav = [0 0 5];
anorm = ([-cosd(theta) -sind(theta) 0]*(norm(omega))^2);
atang = cross(Rjrf,alphav);
a = anorm + atang;
Fmuscle = norm(mass*a - Fjrf - Fw)
mass
plot(mass,Fmuscle)'
hold on
end

最后,输出只是Fmuscle的各种值,对应于以1为间隔从1到4的质量变化值。我试图在图表上绘制所有这些值(质量= x和Fmuscle = y),但我无法弄清楚如何生成它。 使用上面的代码,我只需得到一个空图,即使我得到了所有正确的变量。 谢谢!

1 个答案:

答案 0 :(得分:0)

这是你想要的吗?

i = 0;  %set initial index
clear x y;  %clear these variables
for mass = 1:.1:4
  i = i + 1;  %increment index
  theta = 60;
  Fw = [0 -9.8*mass 0];
  Rw = [ 0.2*cosd(theta) 0.2*sind(theta) 0];
  Mw = cross(Rw,Fw);
  Fjrf = [30 50 0];
  Rjrf = [0.4*cosd(theta) 0.4*sind(theta) 0];
  Mjrf = cross(Rjrf,Fjrf);
  alpha = 5;
  Ialpha = [0 0 alpha*(0.02+mass*0.2^2)];
  Ms = Ialpha - Mjrf - Mw;
  omega = [0 0 pi];
  alphav = [0 0 5];
  anorm = ([-cosd(theta) -sind(theta) 0]*(norm(omega))^2);
  atang = cross(Rjrf,alphav);
  a = anorm + atang;
  Fmuscle = norm(mass*a - Fjrf - Fw);
  x(i) = mass;    % set the appropriate row of x vector
  y(i) = Fmuscle; % same for y
end
plot(x, y);