0% found this document useful (0 votes)
162 views

Assignment III MATLAB CODE

The document describes code to minimize multivariable functions using the Fletcher-Reeves conjugate gradient method in Matlab. It provides code for five functions, outputting iteration results including function value, variable values and step size.

Uploaded by

Alfredo Illescas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
162 views

Assignment III MATLAB CODE

The document describes code to minimize multivariable functions using the Fletcher-Reeves conjugate gradient method in Matlab. It provides code for five functions, outputting iteration results including function value, variable values and step size.

Uploaded by

Alfredo Illescas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

INSTITUTO POLITÉCNICO NACIONAL

ESCUELA SUPERIOR DE INGENIERÍA QUÍMICA E


INDUSTRIAS EXTRACTIVAS

Departamento De Ingeniería Química Petrolera

MODELACIÓN, SIMULACIÓN Y OPTIMIZACIÓN DE


PROCESOS

“ASSIGNMENT III”

Profesor: Dr. Edgar Ramirez Jimenez


Integrantes del equipo:

 Illescas Sánchez Luis Alfredo


 Ortíz Alvarez José Manuel
 Pineda López Alinait de Jesús
You are required in this Assignment to develop a program using Matlab® applying the
Fletcher-Reeves method of the Conjugate Gradient to calculate the unconstrained
minimum of the following multivariable functions:

1. Rosenbrock’s parabolic valley:

ƒ(x1, x2) = 100(x2 − x12)2 + (1 − x1)2


x0 = [−1.2 0]t
2. Wood’s function:

ƒ(x1, x2, x3, x4) = [10(x2 − x12)]2 + (1 − x1)2 + 90(x4 – x32)2 + (1 − x3)2 +10(x2 + x4 − 2)2
+ 0.1(x2 − x4)
x0 = [−3 − 1 − 3 − 1]t

3. A nonlinear function of three variables:

ƒ(x1, x2, x3) = [1/(1 + (x1 − x2)2)]+ sin (1/2 * πx2x3) + exp [ −(((x1 + x3)/x2) – 2)2]

x0 = [0 1 2]t

4. Freudenstein and Roth function:

ƒ(x1, x2) = {−13 + x1 + [(5 − x2)x2 − 2] x2}2 + {−29 + x1 + [(x2 + 1)x2 − 14] x2}2
x0 = [0.5 − 2]t

5. A quartic function:

ƒ(x1, x2) = x14 − 2x2x12 + x22 + x12 − 2x1 + 5


x0 = [0 0]t

You must submit your report covering the following items:

1. The Matlab® source code.


2. The Contour-plot (when applies) showing the successive points.
3. Table of iterations comprising: iteration number, x value, ƒ(x) value and ∇ ƒ(x).
ECUACION 1:
Código:

%funcion objetivo
f=inline('100*((x(2)-x(1)^2)^2)+(1-x(1))^2');
x=[-1.2 0];
tol=10^-4;

%gradientes
gr1=inline('200*(x(2)-x(1)^2)*(-2*x(1))-2*(1-x(1))');
gr2=inline('200*(x(2)-x(1)^2)');
gra=[gr1(x) gr2(x)];

%hessianos
he11=inline('-400*x(2)+1200*(x(1)^2)-2');
he12=inline('-400*x(1)');
he22=inline('200');
hess=[he11(x) he12(x);he12(x) he22(x)];
s=-gra;
n1=norm(s);
n2=n1;

numero=-1;

while n2>=tol
if(numero==20)
break;
else
end

numero=numero+1;

a=-(gra*s')/(s*hess*s');
x=x+a*s;

func=@(x)100*((x(2)-x(1)^2)^2)+(1-x(1))^2;
func(x);

fprintf('x1=%12.4f\t x2=%12.4f\t',x);
fprintf('f=%12.4f\t',func(x));
fprintf('Iteracion numero=%12.1d\n',numero);
fprintf('⍔=%12.2d\t',a);

end
Iteracion numero= 1 x1= -0.8224 x2= 0.1564 f= 30.3542 ?= 5.43e-04

Iteracion numero= 2 x1= -0.4447 x2= 0.3127 f= 3.4079 ?= 2.71e-04


Iteracion numero= 3 x1= -0.0671 x2= 0.4691 f= 22.7198 ?= 1.81e-04
Iteracion numero= 4 x1= 0.3105 x2= 0.6254 f= 28.4570 ?= 1.36e-04
Iteracion numero= 5 x1= 0.6882 x2= 0.7818 f= 9.5950 ?= 1.09e-04
Iteracion numero= 6 x1= 1.0658 x2= 0.9381 f= 3.9177 ?= 9.05e-05
Iteracion numero= 7 x1= 1.4434 x2= 1.0945 f= 98.0176 ?= 7.76e-05
Iteracion numero= 8 x1= 1.8211 x2= 1.2508 f= 427.2953 ?= 6.79e-05
Iteracion numero= 9 x1= 2.1987 x2= 1.4072 f= 1175.9601 ?= 6.03e-05
Iteracion numero= 10 x1= 2.5763 x2= 1.5635 f= 2577.0296 ?= 5.43e-05
Iteracion numero= 11 x1= 2.9540 x2= 1.7199 f= 4912.3299 ?= 4.94e-05
Iteracion numero= 12 x1= 3.3316 x2= 1.8762 f= 8512.4954 ?= 4.52e-05
Iteracion numero= 13 x1= 3.7092 x2= 2.0326 f= 13756.9689 ?= 4.18e-05
Iteracion numero= 14 x1= 4.0869 x2= 2.1889 f= 21074.0019 ?= 3.88e-05
Iteracion numero= 15 x1= 4.4645 x2= 2.3453 f= 30940.6540 ?= 3.62e-05
Iteracion numero= 16 x1= 4.8421 x2= 2.5016 f= 43882.7935 ?= 3.39e-05
Iteracion numero= 17 x1= 5.2198 x2= 2.6580 f= 60475.0968 ?= 3.19e-05
Iteracion numero= 18 x1= 5.5974 x2= 2.8143 f= 81341.0489 ?= 3.02e-05
Iteracion numero= 19 x1= 5.9750 x2= 2.9707 f= 107152.9434 ?= 2.86e-05
Iteracion numero= 20 x1= 6.3527 x2= 3.1270 f= 138631.8821 ?= 2.71e-05

ECUACION 2:
Código:

% funcion objetivo
f=inline('(10*(x(2)-x(1)^2))^2+(1-x(1))^2+90*(x(4)-
x(3)^2)^2+(1-x(3))^2+10*(x(2)+x(4)-2)^2+0.1*(x(2)-x(4))');
x=[-3 -1 -3 -1];
tol=10^-4;

%gradientes
gr1=inline('400*x(1)-400*x(1)*(x(2)-0.005)-2');
gr2=inline('220*x(2)+20*x(4)-200*x(1)^2-39.9');
gr3=inline('360*x(3)^2+360*x(3)*(x(4)-0.00555555555556)+2');
gr4=inline('200*x(4)+20*x(2)-180*x(3)^2-40.1');
gra=[gr1(x) gr2(x) gr3(x) gr4(x)];
%hessianos
he11=inline('1200*x(1)^2*400*(x(2)-0.005)');
he12=inline('-400*x(1)');
he13=inline('0');
he14=inline('0');
he21=inline('-400*x(1)');
he22=inline('220');
he23=inline('0');
he24=inline('20');
he31=inline('0');
he32=inline('0');
he33=inline('1080*x(3)-360*(x(4)-0.00555555555556)');
he34=inline('-360*x(3)');
he44=inline('200');
hess=[he11(x) he12(x) he13(x) he14(x);he14(x) he21(x)
he22(x) he23(x);he23(x) he24(x) he31(x) he32(x);he32(x)
he33(x) he34(x) he44(x) ];
s=-gra;
n1=norm(s);
n2=n1;
numero=0;

while n2>=tol
if(numero==20)
break;
else
end
numero=numero+1;
a=-(gra*s')/(s*hess*s');
x=x+a*s;
funcion=@(x)(10*(x(2)-x(1)^2))^2+(1-x(1))^2+90*(x(4)-
x(3)^2)^2+(1-x(3))^2+10*(x(2)+x(4)-2)^2+0.1*(x(2)-x(4));
funcion(x);

fprintf('x1=%4.4f\t x2=%4.4f\t x3=%4.4f\t x4=%4.4f\t',x);


fprintf('f=%10.4f\t',funcion(x));
fprintf('Iteracion numero =%4.1d\n',numero);
fprintf('⍔=%6.2d\t',a);

end

Iteracion numero= 1 ?=4.16e-05 x1=-3.0031 x2=-1.0027 x3=-2.9944 x4=-1.0024 f=19179.3442


Iteracion numero= 2 ?=4.16e-05 x1=-3.0062 x2=-1.0053 x3=-2.9889 x4=-1.0048 f=19167.3262
Iteracion numero= 3 ?=4.16e-05 x1=-3.0092 x2=-1.0080 x3=-2.9835 x4=-1.0072 f=19155.9303
Iteracion numero= 4 ?=4.16e-05 x1=-3.0122 x2=-1.0106 x3=-2.9781 x4=-1.0095 f=19145.1406
Iteracion numero= 5 ?=4.16e-05 x1=-3.0153 x2=-1.0132 x3=-2.9727 x4=-1.0118 f=19134.9415
Iteracion numero= 6 ?=4.16e-05 x1=-3.0182 x2=-1.0157 x3=-2.9674 x4=-1.0141 f=19125.3181
Iteracion numero= 7 ?=4.16e-05 x1=-3.0212 x2=-1.0183 x3=-2.9622 x4=-1.0164 f=19116.2555
Iteracion numero= 8 ?=4.16e-05 x1=-3.0241 x2=-1.0208 x3=-2.9570 x4=-1.0187 f=19107.7399
Iteracion numero= 9 ?=4.16e-05 x1=-3.0271 x2=-1.0234 x3=-2.9518 x4=-1.0209 f=19099.7573
Iteracion numero= 10 ?=4.16e-05 x1=-3.0300 x2=-1.0259 x3=-2.9467 x4=-1.0231 f=19092.2946
Iteracion numero= 11 ?=4.16e-05 x1=-3.0328 x2=-1.0283 x3=-2.9417 x4=-1.0253 f=19085.3389
Iteracion numero= 12 ?=4.16e-05 x1=-3.0357 x2=-1.0308 x3=-2.9367 x4=-1.0275 f=19078.8777
Iteracion numero= 13 ?=4.16e-05 x1=-3.0385 x2=-1.0333 x3=-2.9317 x4=-1.0296 f=19072.8991
Iteracion numero= 14 ?=4.16e-05 x1=-3.0414 x2=-1.0357 x3=-2.9268 x4=-1.0318 f=19067.3914
Iteracion numero= 15 ?=4.16e-05 x1=-3.0442 x2=-1.0381 x3=-2.9219 x4=-1.0339 f=19062.3431
Iteracion numero= 16 ?=4.16e-05 x1=-3.0469 x2=-1.0405 x3=-2.9171 x4=-1.0360 f=19057.7434
Iteracion numero= 17 ?=4.16e-05 x1=-3.0497 x2=-1.0429 x3=-2.9123 x4=-1.0380 f=19053.5816
Iteracion numero= 18 ?=4.16e-05 x1=-3.0525 x2=-1.0453 x3=-2.9076 x4=-1.0401 f=19049.8474
Iteracion numero= 19 ?=4.16e-05 x1=-3.0552 x2=-1.0476 x3=-2.9029 x4=-1.0421 f=19046.5308
Iteracion numero= 20 ?=4.16e-05 x1=-3.0579 x2=-1.0500 x3=-2.8982 x4=-1.0442 f=19043.6221

ECUACION 3:
Código:

% función objetivo
f=(‘1/(1+(x-y)^2)+sin(0.5*pi*y*z+exp(-(((x+z)/y)-2)^2))’);
tol=10^-4;
syms x
syms y
syms z

%derivadas
d1=diff(f,x);
d2=diff(f,y);
d3=diff(f,z);

de1=inline(char(d1));
de2=inline(char(d2));
de3=inline(char(d3));

%hessianos
he11=(diff(f,x,2));
he12=(diff(d1,y))+1e-24*y;
he13=(diff(d1,z))+1e-24*y+1e-24*z;
he21=(diff(d2,x))+1e-24*y;
he22=(diff(d2,y))+1e-24*y+1e-24*x;
he23=(diff(d2,z))+1e-24*y+1e-24*x+1e-24*z;
he33=(diff(d3,z))+1e-24*x+1e-24*y+1e-24*z;

hess11=inline(char(he11));
hess12=inline(char(he12));
hess13=inline(char(he13));
hess21=inline(char(he21));
hess22=inline(char(he22));
hess23=inline(char(he23));
hess33=inline(char(he33));

x=(0);
y=(1);
z=(2);

gra=[de1(x,y,z) de2(x,y,z) de3(x,y,z)];

h11 = hess11(x,y,z);
h12 = hess12(x,y,z);
h13 = hess13(x,y,z);
h21 = hess21(x,y,z);
h22 = hess22(x,y,z);
h23 = hess23(x,y,z);
h33 = hess33(x,y,z);

H = [h11 h12 h13; h13 h21 h22 ; h22 h23 h33];


detH = det(H);
s=-gra;
n1=norm(s);
n2=n1;
numero=0;

while n2 >= tol

if(numero==20)
break;
else
end

numero=numero+1;
h11 = hess11(x,y,z);
h12 = hess12(x,y,z);
h13 = hess13(x,y,z);
h21 = hess21(x,y,z);
h22 = hess22(x,y,z);
h23 = hess23(x,y,z);
h33 = hess33(x,y,z);
H = [h11 h12 h13; h13 h21 h22 ; h22 h23 h33];

gra=[de1(x,y,z) de2(x,y,z) de3(x,y,z)];


a=(gra*s’)/(s*H*s’);
x=x+a*s(1);
y=y+a*s(2);
z=z+a*s(3);

f=@(x)1/(1+(x-y)^2)+sin(0.5*pi*y*z+exp(-(((x+z)/y)-2)^2));
f(x);

fprintf(‘x1=%4.4f\t ‘,x);
fprintf(‘x2=%4.4f\t ‘,y);
fprintf(‘x3=%4.4f\t ‘,z);
fprintf(‘f=%10.4f\t’,f(x));
fprintf(‘Iteracion numero=%4.1d\n’,numero);
fprintf(‘⍔=%6.2d\t’,a);

end

Iteracion numero= 1 ¿=-4.78e-01 x1=0.2388 x2=-0.0497 x3=1.5946 f= 0.7990


Iteracion numero= 2 ¿=1.05e+00 x1=-0.2870 x2=2.2615 x3=2.4872 f= 0.3723
Iteracion numero= 3 ¿=6.95e-01 x1=-0.6344 x2=3.7881 x3=3.0769 f= -0.3240
Iteracion numero= 4 ¿=3.21e-01 x1=-0.7947 x2=4.4924 x3=3.3489 f= -0.9458
Iteracion numero= 5 ¿=2.38e-02 x1=-0.8066 x2=4.5446 x3=3.3690 f= -0.7825
Iteracion numero= 6 ¿=8.26e-02 x1=-0.8478 x2=4.7261 x3=3.4391 f= 0.5284
Iteracion numero= 7 ¿=-2.06e-01 x1=-0.7446 x2=4.2724 x3=3.2639 f= -0.0119
Iteracion numero= 8 ¿=-4.14e+00 x1=1.3274 x2=-4.8335 x3=-0.2531 f= 0.9623
Iteracion numero= 9 ¿=-5.69e-02 x1=1.3558 x2=-4.9587 x3=-0.3014 f= 0.7323
Iteracion numero= 10 ¿=-1.42e-01 x1=1.4270 x2=-5.2716 x3=-0.4223 f= -0.3335
Iteracion numero= 11 ¿=4.26e-01 x1=1.2143 x2=-4.3365 x3=-0.0611 f= 0.4411
Iteracion numero= 12 ¿=4.71e-01 x1=0.9786 x2=-3.3008 x3=0.3389 f= -0.9315
Iteracion numero= 13 ¿=4.71e-02 x1=0.9550 x2=-3.1973 x3=0.3789 f= -0.8915
Iteracion numero= 14 ¿=8.97e-02 x1=0.9102 x2=-3.0001 x3=0.4550 f= -0.7799
Iteracion numero= 15 ¿=1.95e-01 x1=0.8127 x2=-2.5717 x3=0.6205 f= -0.5140
Iteracion numero= 16 ?=1.16e+00 x1=0.2329 x2=-0.0238 x3=1.6046 f= 0.8783
Iteracion numero= 17 ?=1.09e+00 x1=-0.3119 x2=2.3705 x3=2.5293 f= -0.1884
Iteracion numero= 18 ?=-6.48e-01 x1=0.0122 x2=0.9465 x3=1.9793 f= -0.1767
Iteracion numero= 19 ?=-1.28e+00 x1=0.6535 x2=-1.8720 x3=0.8908 f= -0.3637
Iteracion numero= 20 ?=1.22e-01 x1=0.5926 x2=-1.6043 x3=0.9941 f= -0.4227

ECUACION 4:
Código:

% funcion objetivo
f=inline('(-13+x(1)+x(2)*((5-x(2))*x(2)-2))^2)+(-
29+x(1)+x(2)*((x(2)+1)*x(2)-14))^2)');
x=[0.5 -2];
tol=10^-4;

%gradientes
gr1=inline('4*x(1)+12*x(2)^2-32*x(2)-84');
gr2=inline('12*x(2)^5-40*x(2)^4+8*x(2)^3-
240*x(2)^2+24*x(2)*(x(1)+1)-32*x(1)+864');
gra=[gr1(x) gr2(x)];
he11=inline('4');
he12=inline('24*x(2)-32');
he22=inline('60*x(2)^4-160*x(2)^3+24*x(2)^2-
480*x(2)+24*(x(1)+1)');
hess=[he11(x) he12(x);he12(x) he22(x)];
s=-gra;
n1=norm(s);
n2=n1;

numero=0;

while n2>=tol
if (numero==20)
break;
else
end

numero=numero+1;
a=-(gra*s')/(s*hess*s');
x=x+a*s;

f=@(x)(((((-13+x(1)+x(2)*((5-x(2))*x(2)-2))^2)))+(((-
29+x(1)+x(2)*((x(2)+1)*x(2)-14))^2)));
f(x);

fprintf('x1=%4.4f\t x2=%4.4f\t',x);
fprintf('f=%10.4f\t',f(x));
fprintf('Iteracion numero =%4.1d\n',numero);
fprintf('⍔=%6.2d\t',a);

end
Iteracion numero = 1 ?=3.00e-04 x1=0.4910 x2=-1.6185 f= 120.8459
Iteracion numero = 2 ?=3.00e-04 x1=0.4820 x2=-1.2369 f= 133.9644
Iteracion numero = 3 ?=3.00e-04 x1=0.4730 x2=-0.8554 f= 313.1200
Iteracion numero = 4 ?=3.00e-04 x1=0.4640 x2=-0.4739 f= 581.8394
Iteracion numero = 5 ?=3.00e-04 x1=0.4550 x2=-0.0923 f= 893.9711
Iteracion numero = 6 ?=3.00e-04 x1=0.4460 x2=0.2892 f= 1218.1863
Iteracion numero = 7 ?=3.00e-04 x1=0.4370 x2=0.6707 f= 1526.9221
Iteracion numero = 8 ?=3.00e-04 x1=0.4280 x2=1.0523 f= 1789.7659
Iteracion numero = 9 ?=3.00e-04 x1=0.4190 x2=1.4338 f= 1971.2822
Iteracion numero = 10 ?=3.00e-04 x1=0.4100 x2=1.8153 f= 2033.2806
Iteracion numero = 11 ?=3.00e-04 x1=0.4010 x2=2.1969 f= 1941.5257
Iteracion numero = 12 ?=3.00e-04 x1=0.3920 x2=2.5784 f= 1676.8884
Iteracion numero = 13 ?=3.00e-04 x1=0.3830 x2=2.9599 f= 1250.9400
Iteracion numero = 14 ?=3.00e-04 x1=0.3740 x2=3.3415 f= 725.9866
Iteracion numero = 15 ?=3.00e-04 x1=0.3650 x2=3.7230 f= 239.5465
Iteracion numero = 16 ?=3.00e-04 x1=0.3560 x2=4.1045 f= 33.2685
Iteracion numero = 17 ?=3.00e-04 x1=0.3470 x2=4.4861 f= 486.2923
Iteracion numero = 18 ?=3.00e-04 x1=0.3380 x2=4.8676 f= 2153.0505
Iteracion numero = 19 ?=3.00e-04 x1=0.3290 x2=5.2491 f= 5805.5127
Iteracion numero = 20 ?=3.00e-04 x1=0.3200 x2=5.6307 f=12479.8706

ECUACION 5:
Código:
% funcion objetivo
f=inline('x(1)^4-2*x(1)^2*x(2)+x(2)^2+x(1)^2-2*x(1)+5');
x=[0 0];
tol=10^-2;

%gradientes
gr1=inline('4*x(1)^3-4*x(1)*x(2)+2*x(1)-2');
gr2=inline('-2*x(1)^2+2*x(2)');
gra=[gr1(x) gr2(x)];

%hessianos
he11=inline('12*x(1)^2-4*x(2)+2');
he12=inline('-4*x(1)');
he22=inline('2');
hess=[he11(x) he12(x);he12(x) he22(x)];
s=-gra;
n1=norm(s);
n2=n1;
numero=0;

while n2>=tol
if (numero==20)
break;
else
end
numero=numero+1;
a=-(gra*s')/(s*hess*s');
x=x+a*s;
f=@ (x)x(1)^4-2*x(1)^2*x(2)+x(2)^2+x(1)^2-2*x(1)+5;
f(x);

fprintf('x1=%4.4f\t x2=%4.4f\t',x);
fprintf('f=%10.4f\t',f(x));
fprintf('Iteracion numero=%4.1d\n',numero);
fprintf('⍔=%6.2d\t',a);

end

Iteracion numero= 1 ?=5.00e-01 x1=1.0000 x2=0.0000 f= 5.0000


Iteracion numero= 2 ?=5.92e-01 x1=1.2795 x2=1.1840 f= 4.2834
Iteracion numero= 3 ?=5.23e-02 x1=1.1457 x2=1.3019 f= 4.0214
Iteracion numero= 4 ?=3.28e-02 x1=1.1251 x2=1.3110 f= 4.0177
Iteracion numero= 5 ?=1.82e-02 x1=1.1208 x2=1.3108 f= 4.0176
Iteracion numero= 6 ?=2.42e-02 x1=1.1148 x2=1.3080 f= 4.0174
Iteracion numero= 7 ?=6.68e-02 x1=1.0970 x2=1.2889 f= 4.0167
Iteracion numero= 8 ?=4.69e-01 x1=0.9661 x2=0.9770 f= 4.0031
Iteracion numero= 9 ?=6.13e-02 x1=0.9633 x2=0.9304 f= 4.0014
Iteracion numero= 10 ?=2.24e-02 x1=0.9648 x2=0.9247 f= 4.0013
Iteracion numero= 11 ?=1.18e-02 x1=0.9658 x2=0.9231 f= 4.0013
Iteracion numero= 12 ?=8.16e-03 x1=0.9666 x2=0.9224 f= 4.0013
Iteracion numero= 13 ?=6.84e-03 x1=0.9673 x2=0.9221 f= 4.0013
Iteracion numero= 14 ?=6.68e-03 x1=0.9681 x2=0.9220 f= 4.0013
Iteracion numero= 15 ?=7.55e-03 x1=0.9690 x2=0.9221 f= 4.0012
Iteracion numero= 16 ?=9.95e-03 x1=0.9702 x2=0.9225 f= 4.0012
Iteracion numero= 17 ?=1.60e-02 x1=0.9724 x2=0.9240 f= 4.0012
Iteracion numero= 18 ?=3.39e-02 x1=0.9775 x2=0.9295 f= 4.0012
Iteracion numero= 19 ?=1.00e-01 x1=0.9940 x2=0.9589 f= 4.0009
Iteracion numero= 20 ?=1.02e-01 x1=1.0096 x2=1.0115 f= 4.0002
Los cambios de la función empezaron a ser mínimos, incluso con mas iteraciones

You might also like