Help needed to apply for loop.
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Adam
2018-8-20
编辑:Adam
2018-8-20
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.
回答(1 个)
OCDER
2018-8-20
编辑:OCDER
2018-8-21
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 个评论
OCDER
2018-8-21
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!