how can i plot all for loop's values

1 view (last 30 days)
mohamed saber
mohamed saber on 13 Oct 2011
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

Answers (2)

mohammad Al-Kayyali
mohammad Al-Kayyali on 13 Oct 2011
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 on 13 Oct 2011
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 Comments
Sean de Wolski
Sean de Wolski on 13 Oct 2011
Matt vectorized your statements so you don't need a for-loop.
Matt Tearle
Matt Tearle on 13 Oct 2011
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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!