how can i plot all for loop's values

1 次查看(过去 30 天)
i made a for loop programe ,but when i make plot for value .. the result is point .. how can i save all value of for loop and plot them ??
clear,clc
for th=-25:.1:50;
fai=atand(((750+450.*sin(th)))./(450.*cosd(th)));
force=(4000*cosd(th)*1500)/(450*sind(fai));
l=450*cosd(th)/cosd(fai);
plot(th,force)
end

回答(2 个)

mohammad Al-Kayyali
mohammad Al-Kayyali 2011-10-13
hi mo ,
Try to use dummy variable to save your data then use the plot function as follows :
z=1;for th=-25:.1:50;
fai=atand(((750+450.*sin(th)))./(450.*cosd(th)));
force=(4000*cosd(th)*1500)/(450*sind(fai));
thdummy(z)=th;
forcedummy(z)=force;
z=z+1;
l=450*cosd(th)/cosd(fai);
end
plot(thdummy,forcedummy)

Matt Tearle
Matt Tearle 2011-10-13
Why are you using a for-loop at all? These are all vectorized operations.
th=-25:.1:50;
fai=atand(((750+450.*sind(th)))./(450.*cosd(th)));
force=(4000*cosd(th)*1500)./(450*sind(fai));
l=450*cosd(th)./cosd(fai);
plot(th,force)
Note the use of the elementwise multiply and divide everywhere.
  3 个评论
Sean de Wolski
Sean de Wolski 2011-10-13
Matt vectorized your statements so you don't need a for-loop.
Matt Tearle
Matt Tearle 2011-10-13
Run the code I posted, then check your workspace. The first line creates a vector of values for th. Then fai, force, and l are also vectors, because all operations are performed element-by-element. No loops required. That's MATLAB for you.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by