Assignment III MATLAB CODE
Assignment III MATLAB CODE
“ASSIGNMENT III”
ƒ(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
ƒ(x1, x2, x3) = [1/(1 + (x1 − x2)2)]+ sin (1/2 * πx2x3) + exp [ −(((x1 + x3)/x2) – 2)2]
x0 = [0 1 2]t
ƒ(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:
%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
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);
end
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);
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);
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];
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
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