You are on page 1of 5

1

Appendix A:
Matlab codes:
Step1:
clc;
clear all;
syms p1
syms p2
syms p3
x1=561;y1=7.92;z1=0.00156;x2=310;y2=7.85;z2=0.00194;x3=78;y3=7.97;z3=0.0048
2;
f1= x1+y1*p1+z1*p1^2;
f2= x2+y2*p2+z2*p2^2;
f3= x3+y3*p3+z3*p3^2;
%diffreniation%
A=diff(f1);
B=diff(f2);
C=diff(f3);
N=ones(4,4);%making an matrix to fill it with values'
%filling the values of the equations into a matrix to be solved later%
N(1,1)=2*z1;
N(2,2)=2*z2;
N(3,3)=2*z3;
% the value of lamda
N(1,4)= -1;
N(2,4)= -1;
N(3,4)= -1;
%the rest values of the equations
N(1,2)= 0;
N(1,3)= 0;
N(2,1)= 0;
N(2,3)= 0;
N(3,1)= 0;
N(3,2)=0;
N(4,4)=0;
Y= [-y1; -y2; -y3;850];
Z=N\Y;% solving the 4 matrcies
p1=Z(1)
p2=Z(2)
P3=Z(3)
Lamda=Z(4)

Step2
clc;
clear all;
syms p1 ; syms p2 ; syms p3 ;
L1=0;L2=0;L3=0;
x1=459;y1=6.48;z1=0.00128;x2=310;y2=7.85;z2=0.00194;x3=78;y3=7.97;z3=0.0048
2;
f1= x1+y1*p1+z1*p1^2;

2
f2= x2+y2*p2+z2*p2^2;
f3= x3+y3*p3+z3*p3^2;
load=850;
%putting the limits
M1=600;m1=150;
M2=400;m2=100;
M3=200;m3=50;
%diffreniation%
A=diff(f1);
B=diff(f2);
C=diff(f3);
N=ones(4,4);%making an matrix to fill it with values'
%filling the values of the equations into a matrix to be solved later%
N(1,1)=2*z1;
N(2,2)=2*z2;
N(3,3)=2*z3;
% the value of lamda in the matrix
N(1,4)= -1;
N(2,4)= -1;
N(3,4)= -1;
%the rest values of the equations
N(1,2)= 0;
N(1,3)= 0;
N(2,1)= 0;
N(2,3)= 0;
N(3,1)= 0;
N(3,2)=0;
N(4,4)=0;
Y= [-y1; -y2; -y3;850];
Z=N\Y;% solving the 4 matrcies
p1=Z(1);
p2=Z(2);
p3=Z(3);
Lamda=Z(4);
%Checking the limitations%
if p1>M1
p1=M1;
iter=1;
elseif p1<m1
p1=m1;
iter=2;
else
p1=p1;
end

if p2>M2
p2=M2;
iter=1;
elseif p2<m2
p2=m2;
iter=2;
else
p2=p2;
end

3
if p3 >M3
p3=M3;
iter=1;
elseif p3<m3
p3=m3;
iter=2;
else
p3=p3;
end

p2=850-p1-p3;

if M2<p2<m2
lamda = y2*1 + z2*2*p2;
elseif M1<p1<m1
lamda = y1*1 + z1*2*p1;
elseif M3<p3<m3
lamda = y3*1 + z3*2*p3;
iter=1;
end
if (iter==1)
if p1==M1
L1=y1*1 + z1*2*p1;
if L2<= lamda
p1=M1;
end
end
if p2==M2
L2=y2*1 + z2*2*p2;
if L2 <= lamda
p2=M2;
end
end
if p3==M3
L3=y3*1 + z3*2*p3;
if L3<= lamda
p3=M3;
end
end
end
if (iter==2)
if p1==m1
L1=y1*1 + z1*2*p1;
if L2>= lamda
p1=m1;
end
end
if p2==m2
L2=y2*1 + z2*2*p2;
if L2 >= lamda
p2=m2;
end

4
end
if p3==m3
L3=y3*1 + z3*2*p3;
if L3>= lamda
p3=m3;
else
N1=[N(2,2) 0 -1;0 N(3,3) -1;1 1 0];
Y1=[Y(2);Y(3);load-p1];
Z1=N1\Y1;
p2= Z1(1);
p3= Z1(2);
lamda= Z1(3);
end
end
end
p1
p2
p3
lamda

Step3
clc;
clear all;
syms p1
syms p2
syms p3
x1=561;y1=7.92;z1=0.00156;x2=310;y2=7.85;z2=0.00194;x3=78;y3=7.97;c3=0.0048
2;
%the equations of the cost functions%
f1= x1+y1*p1+z1*p1^2;
f2= x2+y2*p2+z2*p2^2;
f3= x3+y3*p3+z3*p3^2;
load=850;
%the variable of teh losses%
xloss=0.00003;
yloss=0.00009;
zloss=0.00012;
% to make sure that the inital guess are between the limits%
prog=1;
while(prog==1)
p(1)=input('initial guess for the 1st unit: ');
p(2)=input('initial guess for the 2nd unit: ');
p(3)=input('initial guess for the 3rd unit: ');
if((p(1)+p(2)+p(3))>850|| (p(1)+p(2)+p(3))<850)
prog=1;
else
prog=0;
end
end
% diffrentiation of cost functions&

5
A=diff(f1);
B=diff(f2);
C=diff(f3);
%initiate matrix to fill it with linear cost and losses functions elements%
N=zeros(4,4);
N(1,1)=2*z1;
N(2,2)=2*z2;
N(3,3)=2*z3;
e=0.001;% the toleranced
K=[0; 0;
0];% intiate amtrix to put the updated value in ot%
iter=0;
while (abs(p(1)-Q(1))>e||abs(p(2)-Q(2))>e|| abs(p(3)-Q(3))>e)
K=p;% to keep the new value updated%
loss= xloss*p(1)^2+ yloss*p(2)^2+ zloss*p(3)^2;
diffloss=[2*xloss*p(1)
2*yloss*p(2)
2*zloss*p(3)];
N(1,4)= -(1-diffloss(1,1));
N(2,4)= -(1-diffloss(1,2));
N(3,4)= -(1-diffloss(1,3));
N(4,1)= 1;
N(4,2)= 1;
N(4,3)= 1;
Y= [-y1; -y2; -y3; (850+loss)];
%solving the equations simltanously%
M=N\Y;
p(1)=M(1);
p(2)=M(2);
p(3)=M(3);
Lamda=M(4);
iter=iter+1;
end
p1
P2
P3
Lamda

You might also like