why while loop repeats so long?
Afficher commentaires plus anciens
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)
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!