Application of For Next Statement?
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Michael Haderlein
2015-2-3
编辑:Michael Haderlein
2015-2-3
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 个评论
Michael Haderlein
2015-2-4
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Stress and Strain 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!