why while loop repeats so long?

1 visualización (últimos 30 días)
arian hoseini
arian hoseini el 30 de Nov. de 2022
Comentada: arian hoseini el 1 de Dic. de 2022
i have this code that works correctly but i wrote another one that looks like loop repeats for so long...
clc
clear all
format longG %changes the output display format in the Command Window to the format specified by style.
alpha = [510; 310; 78; 120; 350; 490; 53; 150; 170; 149];
B = [7.20; 7.85; 7.98; 7.10; 7.30; 6.90; 8.01; 7.30; 7.42; 7.16];
C = [0.00142; 0.00194; 0.00482; 0.00321; 0.00210; 0.00178; 0.00212; 0.00350; 0.00267; 0.00390];
D = [0.0000001; 0.0000002; 0.0000003; 0.0000001; 0.0000002; 0.0000003; 0.0000001; 0.0000002; 0.0000003; 0.0000001];
Pmin =[150; 100; 50; 50; 100; 100; 100; 100; 100; 100];
Pmax =[600; 400; 200; 200; 350; 500; 300; 300; 300; 300];
n = 10;
Pr = 2500;
L = 8.6;
%-----------------------------program starts here--------------------------
ep=1;
count=0
while ep>=0.95
count = count+1;
for i=1:n
c=(B(i)-L);b=(2*C(i));a=(3*D(i));
delta=(b^2-(4*a*c));
P(i,:)=((-b+sqrt(delta))/(2*a));
if P(i)< Pmin(i)
P(i)=Pmin(i);
elseif P(i)> Pmax(i)
P(i)=Pmax(i);
end
Pt=sum(P);
ep=Pr-Pt;
M=sum(ones(length(D(i)),1)/(2*(D(i))));
end
DL=ep/M;
if ep>0
L=L+DL;
elseif ep<0
L=L-DL;
end
end
disp(L); %show the final Lambda
vpa(P,6)
this one is not working correctly
clc
clear all
format longG
a = [510; 310; 78; 120; 350; 490; 53; 150; 170; 149];
b = [7.20; 7.85; 7.98; 7.10; 7.30; 6.90; 8.01; 7.30; 7.42; 7.16];
c = [0.00142; 0.00194; 0.00482; 0.00321; 0.00210; 0.00178; 0.00212; 0.00350; 0.00267; 0.00390];
d = [0.0000001; 0.0000002; 0.0000003; 0.0000001; 0.0000002; 0.0000003; 0.0000001; 0.0000002; 0.0000003; 0.0000001];
Pmin =[150; 100; 50; 50; 100; 100; 100; 100; 100; 100];
Pmax =[600; 400; 200; 200; 350; 500; 300; 300; 300; 300];
n = 10;
Pr = 2500;
L = 8.6;
syms p;
F=sym('F',[1 10]);
X=sym('X',[1 10]);
ep=1;
count=0;
while ep>=0.9
count=count+1;
for i=1:10
F(i)=a(i)+b(i).*p+c(i).*p.^2+d(i).*p.^3;
D=diff(F(i),p)-L==0;
S=solve(D,p);
X(i)=max(S);
if X(i)<pmin(i)
X(i)=pmin(i);
elseif X(i)>pmax(i)
X(i)=pmax(i);
end
pt=sum(X);
ep=2500-(pt)
M=sum(ones(length(d(i)),1)/(2*(d(i))));
end
DL=ep/M;
if ep>0
L=L+DL;
elseif ep<0
L=L-DL;
end
end
disp(L)
vpa(X)

Respuesta aceptada

Torsten
Torsten el 30 de Nov. de 2022
If you output "count" for the numerical solution, it's about 14000.
Now if you measure the time for one symbolic iteration in the while loop (approximately 1.6 seconds) and multiply this value by 14000 - I guess you will have to wait quite a long time for a result:
1.6*14000/3600
ans = 6.2222
6.2 hours. Wow! So stick to the numerical solution.
Your code would be much faster if you solved the nonlinear equation for L using "fzero", e.g.
L0 = 6.0;
L = fzero(@fun,L0)
L = 8.6749
function res = fun(L)
B = [7.20; 7.85; 7.98; 7.10; 7.30; 6.90; 8.01; 7.30; 7.42; 7.16];
C = [0.00142; 0.00194; 0.00482; 0.00321; 0.00210; 0.00178; 0.00212; 0.00350; 0.00267; 0.00390];
D = [0.0000001; 0.0000002; 0.0000003; 0.0000001; 0.0000002; 0.0000003; 0.0000001; 0.0000002; 0.0000003; 0.0000001];
Pmin =[150; 100; 50; 50; 100; 100; 100; 100; 100; 100];
Pmax =[600; 400; 200; 200; 350; 500; 300; 300; 300; 300];
Pr = 2500;
c=B-L;
b=2*C;
a=3*D;
delta=b.^2-(4*a.*c);
P=(-b+sqrt(delta))./(2*a);
P(P<Pmin) = Pmin(P<Pmin);
P(P>Pmax) = Pmax(P>Pmax);
Pt=sum(P);
res=Pr-Pt;
end

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by