Application of For Next Statement?
Show older comments
Hi Guys,
I am unable plot the graphs for the Stress Intensity Functions using For Next Loop. Hers is the script, can anybody explain me the fallacy for the code.
Code:
%------------Stress Plot---------------%
theta=0:0.1:360
n=length(theta)
m=1/3
for x=1:n
a=((0.25).*(1+cos(x)+(1.5).*(sin(x)).^2));
b=((0.25).*((1-(2.*m.^2)).*(1+cos(x))+(1.5.*(sin(x)).^2)));
end
figure
polar(theta,a,'b')
hold on
polar(theta,b,'r')
Thanks & Regards
Answers (1)
Michael Haderlein
on 3 Feb 2015
Edited: Michael Haderlein
on 3 Feb 2015
0 votes
Please make your code readable using the {}Code button on top of the edit window in future.
You are overwriting a in every loop iteration. Make it a(x) =... and it will work (the same with b).
Edit: I just realized you span theta from 0 to 360. sin and cos are radian based, so either span theta from 0 to 2*pi or use sind and cosd functions.
2 Comments
Sagar Trehan
on 4 Feb 2015
Michael Haderlein
on 4 Feb 2015
Really, the code is barely readable without the code formatting. That's most likely the reason why I didn't see one important point in your code: You use x as argument of the trigonometric functions. It must be theta(x), of course. Anyway, you don't even need a loop:
a=((0.25).*(1+cos(theta)+(1.5).*(sin(theta)).^2));
will work for all theta at once.
Categories
Find more on Stress and Strain in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!