Application of For Next Statement?

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

Risposte (1)

Michael Haderlein
Michael Haderlein il 3 Feb 2015
Modificato: Michael Haderlein il 3 Feb 2015

0 voti

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 Commenti

Hi Michael,
Thanks for your response. I am posting two different codes for the same output. With first code I am getting correct output but I am trying the same with For Loop but output is not as expected.
if true
%-----------------------------------------------------------------%
%------Von Mises Stress Plot for Crack Tip Plasticity-------------%
%-----------------------------------------------------------------%
theta=0:0.001:2*pi; ms=(1+cos(theta)+1.5.*(sin(theta)).^2).*(0.25) vs=(((1-(2.*(1/3)))^2).*(1+cos(theta))+((3/2).*((sin(theta)).^2)))*(0.25) ts=(((cos(theta./2)).^2).*((1+sin(theta./2)).^2)) rs=(((cos(theta./2)).^2).*((1-(2.*(1/3))+(sin(theta./2)))).^2) figure polar(theta,ms,'r') hold on polar(theta,vs,'b') hold off figure polar(theta,ts,'b') hold on polar(theta,rs,'g')
% code
end
2nd Code:
if true%------------Stress Plot---------------%
theta=0:0.001:2*pi n=length(theta) m=1/3
for x=1:n a(x)=((0.25).*(1+cosd(x)+(1.5).*(sind(x)).^2)); b(x)=((0.25).*((1-(2.*m.^2)).*(1+cosd(x))+(1.5.*(sind(x)).^2))); end figure polar(theta,a,'b') hold on polar(theta,b,'r') % code end
Pls refer the second code for the plot generation. I want to understand where I have done the mistake in understanding.
Regards, Sagar Trehan
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.

Accedi per commentare.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by