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
0 个评论
回答(2 个)
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)
0 个评论
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 个评论
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 Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!