how can i plot all for loop's values

1 回表示 (過去 30 日間)
mohamed saber
mohamed saber 2011 年 10 月 13 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by