Help needed to apply for loop.

1 view (last 30 days)
wiki007
wiki007 on 20 Aug 2018
Edited: OCDER on 21 Aug 2018
Hi! Good day to everyone. I want to apply the loop to calculate the Lx value until and unless the error between new and previous Lx value is less that 10^-3. I have tried to write the code in matlab. But it is giving error and cannot run the loop vey well. I am pasting my code. Help needed!
close all; clear all; clc
syms Lx
deltax = 2.40;
Dx = 1.5;
no=10;
gamma = 1.8;
b=0;
d=0;
for n = 0:30
b= b+((gamma).^((Dx-2)*n)*(cos((2*pi/Lx)*(gamma.^n)*(deltax))));
d=d+(gamma.^((Dx-2)*n));
eqn = b == (0.5)*(d);
end
Lx= solve(eqn,Lx);
l=0:0.01:(Lx*gamma.^-no);
Rz=(1/(1-gamma).^(Dx-2)).*((l/Lx).^(2-Dx));
coefficients = polyfit(l, imag(Rz),1);
slope = coefficients(1);%plot(l,imag(Rz));
Dx=2-slope;
i = 1
while i<2
syms Lx
b=0;
d=0;
for n = 0:30
b= b+((gamma).^((Dx-2)*n)*(cos((2*pi/Lx)*(gamma.^n)*(deltax))));
d=d+(gamma.^((Dx-2)*n));
eqn = b == (0.5)*(d);
end
Lx1= solve(eqn,Lx);
l=0:0.01:(Lx1*gamma.^-no);
Rz=(1/(1-gamma).^(Dx-2)).*((l/Lx1).^(2-Dx));
coefficients = polyfit(l, imag(Rz),1);
slope = coefficients(1);%plot(l,imag(Rz));
Dx=2-slope
error = (Lx1-Lx)/Lx;
if error == (10).^-3
break
end
Lx = error
end
  2 Comments
Adam
Adam on 20 Aug 2018
Edited: Adam on 20 Aug 2018
Please format code in your questions in future if you want people to be able to read it!
Also please give information on errors and exactly what you mean. If you don't give an error message that you get how can someone help you.
'But it is giving error and cannot run the loop very well'
is very vague. What does 'cannot run the loop very well' mean? Generally code either runs or it doesn't, if it doesn't it gives an error or just misses code because of a bad logical expression, if it does then it either gives the results you want or results you don't expect.
wiki007
wiki007 on 20 Aug 2018
It is giving strange answer that i am not expecting.Please check the formation of loops especially at the end the while end loop.

Sign in to comment.

Answers (1)

OCDER
OCDER on 20 Aug 2018
Edited: OCDER on 21 Aug 2018
Your while loop conditions is odd, why i < 2? See comments below. Not sure what a "strange" answer is. This would be a math logic error in your code, which will not generate an error but will return wrong results. It'll be up to you to fix a logic error. Here are some comments and areas for you to double check.
%while i<2 %This while loop condition didn't make sense.
Err = Inf; %Just defining an error as Inf first to satisfy the first while loop iteration (Err >= 10e-3.
while Err > 10^3 %Your condition is here! It'll break when Err < 10e-3
%syms Lx %You already defined Lx as a syms variable, why define it over again?
b=0;
d=0;
for n = 0:30 %Vectorize this. Look up the function "sum"
b= b+((gamma).^((Dx-2)*n)*(cos((2*pi/Lx)*(gamma.^n)*(deltax))));
d=d+(gamma.^((Dx-2)*n));
eqn = b == (0.5)*(d); %Did you want only the final value when n == 30? Why not take it out of loop then?
end
Lx1= solve(eqn,Lx);
l=0:0.01:(Lx1*gamma.^-no);
Rz=(1/(1-gamma).^(Dx-2)).*((l/Lx1).^(2-Dx));
coefficients = polyfit(l, imag(Rz),1);
slope = coefficients(1);%plot(l,imag(Rz));
Dx=2-slope
Err = (Lx1-Lx)/Lx;
%error = (Lx1-Lx)/Lx; %Don't override error function!
%if error == (10).^-3
% break
%end
Lx = Err; %? Did you mean Lx = Lx1, or Lx = Lx + Err? Check Logic.
end
  2 Comments
Stephen23
Stephen23 on 21 Aug 2018
wiki007's "Answer" moved here:
Hi! Can you help me to apply the while loop at the end of the part. The error must be less than or equal to 10.^3
OCDER
OCDER on 21 Aug 2018
Fixed the while loop above.
while Err > 10^3 %Adjust this 10^3 to whatever tolerance you want
...
end
Your while loop will end when Err <= 10^3

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!