0% found this document useful (0 votes)
26 views11 pages

NLP Lab Till Lab 6

Uploaded by

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

NLP Lab Till Lab 6

Uploaded by

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

NON LINEAR PROGRAMMING LAB

ASSIGNMENT 2

Obtain the Hessian Matrix and nd maxima or minima for the following functions using the
property of Hessian matrix.

QUESTION 1

syms x;
syms y;
F = 2*x^2 - 4*x*y +y^4 +2
Fx = diff(F,x)
Fy = diff(F,y)
[extrema1,extrema2] = solve(Fx==0,Fy==0,[x,y])
Fxx = diff(F,x,2)
Fxy = diff(F,x,y)
Fyy = diff(F,y,2)
n=size(extrema1,1)
for i=1:n
Axx=subs(Fxx,[x,y],[extrema1(i),extrema2(i)]);

Axy=subs(Fxy,[x,y],[extrema1(i),extrema2(i)]);

Ayy=subs(Fyy,[x,y],[extrema1(i),extrema2(i)]);

H=zeros(2,2);
H(1,1)=Axx;
H(1,2)=Axy;
H(2,1)=Axy;
H(2,2)=Ayy;
e=eig(H)
if(all(e>0))
fprintf('(%f,%f) is local minima\n',extrema1(i),extrema2(i));
elseif(all(e<0))
fprintf('(%f,%f) is local maxima\n',extrema1(i),extrema2(i));
else
fprintf('(%f,%f) is saddle point\n',extrema1(i),extrema2(i));
end
end

QUESTION 2

syms x;
syms y;
F = 2*x^3 + 6*x*y^2 - 3*y^3 - 150*x
Fx = diff(F,x)
Fy = diff(F,y)
[extrema1,extrema2] = solve(Fx==0,Fy==0,[x,y])
Fxx = diff(F,x,2)
Fxy = diff(F,x,y)
Fyy = diff(F,y,2)
n=size(extrema1,1)
for i=1:n
Axx=subs(Fxx,[x,y],[extrema1(i),extrema2(i)]);

Axy=subs(Fxy,[x,y],[extrema1(i),extrema2(i)]);

Ayy=subs(Fyy,[x,y],[extrema1(i),extrema2(i)]);
fi
H=zeros(2,2);
H(1,1)=Axx;

H(1,2)=Axy;
H(2,1)=Axy;
H(2,2)=Ayy;
e=eig(H)
if(all(e>0))
fprintf('(%f,%f) is local minima\n',extrema1(i),extrema2(i));
elseif(all(e<0))
fprintf('(%f,%f) is local maxima\n',extrema1(i),extrema2(i));
else
fprintf('(%f,%f) is saddle point\n',extrema1(i),extrema2(i));
end
end

QUESTION 3

syms x;
syms y;
F = 2*x^2 + 2*x*y + 2*y^2 - 6*x
Fx = diff(F,x)
Fy = diff(F,y)
[extrema1,extrema2] = solve(Fx==0,Fy==0,[x,y])
Fxx = diff(F,x,2)
Fxy = diff(F,x,y)
Fyy = diff(F,y,2)
n=size(extrema1,1)
for i=1:n
Axx=subs(Fxx,[x,y],[extrema1(i),extrema2(i)]);

Axy=subs(Fxy,[x,y],[extrema1(i),extrema2(i)]);

Ayy=subs(Fyy,[x,y],[extrema1(i),extrema2(i)]);

H=zeros(2,2);
H(1,1)=Axx;

H(1,2)=Axy;
H(2,1)=Axy;
H(2,2)=Ayy;
e=eig(H)
if(all(e>0))
fprintf('(%f,%f) is local minima\n',extrema1(i),extrema2(i));
elseif(all(e<0))
fprintf('(%f,%f) is local maxima\n',extrema1(i),extrema2(i));
else
fprintf('(%f,%f) is saddle point\n',extrema1(i),extrema2(i));
end
end

QUESTION 4

syms x1;
syms x2;
syms l1;
syms l2;
syms s1;
syms s2;

f = -4*x1+x1^2-2*x1*x2+2*x2^2;
g1 = 2*x1+x2+s1^2-6;
g2 = x1-4*x2+s2^2;
L= f+l1*(g1)+l2*(g2);
Lx1 = diff(L,x1);
Lx2 = diff(L,x2);
Ls1 = diff(L,s1);
Ls2 = diff(L,s2);
Ll1 = diff(L,l1);
Ll2 = diff(L,l2);
[x1_sol,x2_sol,s1_sol,s2_sol,l1_sol,l2_sol] =
solve(Lx1==0,Lx2==0,Ls1==0,Ls2==0,g1==0,g2==0,x1,x2,s1,s2,l1,l2);
n=size(x1_sol,1)
for i=1:n
if(2*x1_sol(i)+x2_sol(i)<=6 && x1_sol(i)- 4*x2_sol(i)<=0)
fprintf('(%f,%f) is a local minima with value : %f
\n',x1_sol(i),x2_sol(i),subs(f,[x1,x2],[x1_sol(i),x2_sol(i)]));
end
end

ASSIGNMENT 3

QUESTION 1

syms x1,;
syms x2;
f = x1*x2;
x0 = [1,3];
d = [1,1];
grad = gradient(f,[x1,x2]);
g_value = subs(grad, [x1,x2],x0);
d_prod = dot(g_value,d);
if(d_prod<0)
fprintf('d is a descent direction');
else
fprintf('d is not a descent direction');
end

QUESTION 2

syms x1;
syms x2;
f = -x1-x2 ;
g1 = x1^2 + 4*x2^2-1;
g2 = -x1 ; g3 = -x2 ;
xo = [1/sqrt(5),1/sqrt(5)];
d = input('enter the direction');
active_cons=[0 0 0];
if subs(g1,[x1,x2],xo)==0
active_cons(1)=1;
end
if subs(g2,[x1,x2],xo)==0
active_cons(2)=1;
end
if subs(g3,[x1,x2],xo)==0
active_cons(3)=1;
end
grad_f = gradient(f,[x1,x2]);
grad_g1 = gradient(g1,[x1,x2]);
grad_g2 = gradient(g2,[x1,x2]);
grad_g3 = gradient(g3,[x1,x2]);
g_f = subs(grad_f,[x1,x2],xo);
g_g1 = subs(grad_g1,[x1,x2],xo);
g_g2 = subs(grad_g2,[x1,x2],xo);
g_g3 = subs(grad_g3,[x1,x2],xo);

counter=1;
active_cons
if active_cons(1)==1
feasible(counter)=dot(g_g1,d);
counter=counter+1;
end
if active_cons(2)==1
feasible(counter)=dot(g_g2,d);
counter=counter+1;
end
if active_cons(3)==1
feasible(counter)=dot(g_g3,d);
counter=counter+1;
end
if all(feasible<0)
fprintf('(%f,%f) is a feasible direction\n',d(1),d(2));

else
fprintf('(%f,%f) is not a feasible direction\n',d(1),d(2));
end
descent_dir=dot(g_f,d);
if descent_dir<0
fprintf('(%f,%f) is a descent direction\n',d(1),d(2));
else
fprintf('(%f,%f) is not a descent direction\n',d(1),d(2));
end

QUESTION 3

syms x y l 'real'

f = (x-1.5)^2 + (y-1.5)^2;
g = x+y-2;

F = f +l*g

Fx = diff(F,x)
Fy = diff(F,y)
Fl = diff(F,l)
[extrema1,extrema2,extrema3] = solve(Fx==0,Fy==0,g==0,[x,y,l])

g_eval=subs(g,[x,y,l],[extrema1,extrema2,extrema3]);

if extrema1>=0 && extrema2>=0 && g_eval<=0


fprintf('(%f,%f) is optimal point \n',extrema1,extrema2);
else
fprintf('(%f,%f) is not an optimal point\n',extrema1,extrema2);
end
x0 = [extrema1,extrema2];

grad1 = gradient(f,[x,y]);
f__value = subs(grad1,[x,y],x0)

grad2 = gradient(g,[x,y]);
g__value = subs(grad2,[x,y],x0)

QUESTION 4

syms x y l1 l2 'real'

f = 7*x^2 + 6*x + 5*y^2;


g1 = x+2*y-10;
g2 = x-3*y-9;
l = f+l1*g1+l2*g2;
grad = gradient(l,[x,y]);
fprintf('condition 1(optimality condition) : %s = 0 and %s =
0\n',char(grad(1)),char(grad(2)));
fprintf('condition 2(feasibility condition) : %s <= 0 and %s <=
0\n',char(g1),char(g2));
fprintf('condition 3(complementary slackness) : %s = 0 and %s =
0\n',char(l1*g1),char(l2*g2));
fprintf('condition 4: l1<=0 and l2<=0\n');

solution_counter=1;
fprintf('case 1: l1==0 and l2==0 -> condition 1 is in play\n');

[sol_x,sol_y]=solve(subs(grad(1),[l1,l2],[0,0])==0,subs(grad(2),[l1,l2],
[0,0])==0,x,y);
g1_eval=subs(g1,[x,y],[sol_x,sol_y]);
g2_eval=subs(g2,[x,y],[sol_x,sol_y]);
if sol_x>=0 && sol_y>=0 && g1_eval<=0 && g2_eval<=0
fprintf('(%f,%f) is feasible \n',sol_x,sol_y);
solutions(solution_counter,:)=[sol_x,sol_y];
solution_counter=solution_counter+1;
else
fprintf('(%f,%f) is infeasible\n',sol_x,sol_y);
end
fprintf('case 2: l1==0 and l2/=0 -> condition 1 & 1 part of condition 3 are in
play \n');
%display('case 2: l1==0 and l2/=0 -> condition 1 & 1 part of condition 3 are in
play \n');
[sol_x,sol_y,sol_l2]=solve(subs(grad(1),l1,0)==0,subs(grad(2),l1,0)==0,g2==0,x,y
,l2);
g1_eval=subs(g1,[x,y],[sol_x,sol_y]);
g2_eval=subs(g2,[x,y],[sol_x,sol_y]);
if sol_x>=0 && sol_y>=0 && g1_eval<=0 && g2_eval<=0
fprintf('(%f,%f) is feasible \n',sol_x,sol_y);
solutions(solution_counter,:)=[sol_x,sol_y];
solution_counter=solution_counter+1;
else
fprintf('(%f,%f) is infeasible \n',sol_x,sol_y);
end

fprintf('case 3: l1~=0 and l2==0 -> condition 1 & 2 part of condition 3 are in
play \n');
%display('case 3: l1~=0 and l2==0 -> condition 1 & 2 part of condition 3 are in
play \n');
[sol_x,sol_y,sol_l1]=solve(subs(grad(1),l2,0)==0,subs(grad(2),l2,0)==0,g1==0,x,y
,l1);
g1_eval=subs(g1,[x,y],[sol_x,sol_y]);
g2_eval=subs(g2,[x,y],[sol_x,sol_y]);
if sol_x>=0 && sol_y>=0 && g1_eval<=0 && g2_eval<=0
fprintf('(%f,%f) is feasible \n',sol_x,sol_y);
solutions(solution_counter,:)=[sol_x,sol_y];
solution_counter=solution_counter+1;
else
fprintf('(%f,%f) is infeasible \n',sol_x,sol_y);
end

fprintf('case 4: l1~=0 and l2/~=0 -> condition 1 and 3 are in play \n');
%display('case 2 l1~=0 and l2~=0 -> condition 1 and 3 are in play \n');
[sol_x,sol_y,sol_l1,sol_l2]=solve(grad(1)==0,grad(2)==0,g1==0,g2==0,x,y,l1,l2);

if sol_x>=0 && sol_y>=0 && sol_l1<=0 && sol_l2<=0


fprintf('(%f,%f) is feasible \n',sol_x,sol_y);
solutions(solution_counter,:)=[sol_x sol_y];
solution_counter=solution_counter+1;
else
fprintf('(%f,%f) is infeasible \n',sol_x,sol_y);
end

Fgbest = subs(f,[x,y],[solutions(1,1),solutions(1,2)]);
gbest=[solutions(1,1) solutions(1,2)];
for i=1:size(solutions,1)
new=subs(f,[x,y],[solutions(i,1),solutions(i,2)]);
if new>Fgbest
Fgbest=new;
gbest = [solutions(i,1) solutions(i,2)];
end
end

fprintf('solution is : %s at (%f,%f)\n',char(Fgbest),gbest(1),gbest(2));
%display(['solution is : ',char(Fgbest)]);

QUESTION 5

syms x y l1 l2 'real'

f = 2*x + 3*y ;
g1 = x^2+y^2-200;
g2 = x*y-8;

l = f+l1*g1+l2*g2;
grad = gradient(l,[x,y]);
fprintf('condition 1(optimality condition) : %s = 0 and %s =
0\n',char(grad(1)),char(grad(2)));
fprintf('condition 2(feasibility condition) : %s <= 0 and %s <=
0\n',char(g1),char(g2));
fprintf('condition 3(complementary slackness) : %s = 0 and %s =
0\n',char(l1*g1),char(l2*g2));
fprintf('condition 4: l1<=0 and l2<=0\n');

solution_counter=1;

fprintf('case 2: l1==0 and l2/=0 -> condition 1 & 1 part of condition 3 are in
play \n');
%display('case 2: l1==0 and l2/=0 -> condition 1 & 1 part of condition 3 are in
play \n');
[sol_x,sol_y,sol_l2]=solve(subs(grad(1),l1,0)==0,subs(grad(2),l1,0)==0,g2==0,x>0
,x,y,l2);
g1_eval=subs(g1,[x,y],[sol_x,sol_y]);
g2_eval=subs(g2,[x,y],[sol_x,sol_y]);
if sol_x>=0 && sol_y>=0 && g1_eval<=0 && g2_eval<=0
fprintf('(%f,%f) is feasible \n',sol_x,sol_y);
solutions(solution_counter,:)=[sol_x,sol_y];
solution_counter=solution_counter+1;
else
fprintf('(%f,%f) is infeasible \n',sol_x,sol_y);
end

fprintf('case 3: l1~=0 and l2==0 -> condition 1 & 2 part of condition 3 are in
play \n');
%display('case 3: l1~=0 and l2==0 -> condition 1 & 2 part of condition 3 are in
play \n');
[sol_x,sol_y,sol_l1]=solve(subs(grad(1),l2,0)==0,subs(grad(2),l2,0)==0,g1==0,x>0
,x,y,l1);
g1_eval=subs(g1,[x,y],[sol_x(),sol_y(1)]);
g2_eval=subs(g2,[x,y],[sol_x,sol_y]);
if sol_x>=0 && sol_y>=0 && g1_eval<=0 && g2_eval<=0
fprintf('(%f,%f) is feasible \n',sol_x,sol_y);
solutions(solution_counter,:)=[sol_x,sol_y];
solution_counter=solution_counter+1;
else
fprintf('(%f,%f) is infeasible \n',sol_x,sol_y);
end

fprintf('case 4: l1~=0 and l2/~=0 -> condition 1 and 3 are in play \n');
%display('case 4 l1~=0 and l2~=0 -> condition 1 and 3 are in play \n');

[sol_x,sol_y,sol_l1,sol_l2]=solve(grad(1)==0,grad(2)==0,g1==0,g2==0,0<x<3,x,y,l1
,l2);

if sol_x>=0 && sol_y>=0 && sol_l1<=0 && sol_l2<=0


fprintf('(%f,%f) is feasible \n',sol_x,sol_y);
solutions(solution_counter,:)=[sol_x sol_y];
solution_counter=solution_counter+1;
else
fprintf('(%f,%f) is infeasible \n',sol_x,sol_y);
end

Fgbest = subs(f,[x,y],[solutions(1,1),solutions(1,2)]);
gbest=[solutions(1,1) solutions(1,2)];
for i=1:size(solutions,1)
new=subs(f,[x,y],[solutions(i,1),solutions(i,2)]);
if new>Fgbest
Fgbest=new;
gbest = [solutions(i,1) solutions(i,2)];
end
end

fprintf('solution is : %s at (%f,%f)\n',char(Fgbest),gbest(1),gbest(2));
%display(['solution is : ',char(Fgbest)]);

ASSIGNMENT 4

QUESTION 1

syms x

f= 0.65 - (0.75/(1+x^2)) - 0.65*x*atan(1/x);


fplot(f,'-- ')
a=0.1;
b=3;
n=floor(1/0.05-1);

m=linspace(a,b,n);
i_length = b-a;
while abs(i_length>0.0001)
for i=1:n
fn_values(i)=subs(f,x,m(i));
end
[mini,mini_index]=min(fn_values);
optimal=m(mini_index)
if optimal==b
region=[m(mini_index-1),m(mini_index)];
elseif optimal==a
region=[m(mini_index),m(mini_index+1)];
else
region=[m(mini_index-1),m(mini_index+1)];
end
a= region(1);
b=region(2);
m=linspace(a,b,n);
i_length=b-a;
end
optimal
optimal_value=eval(subs(f,x,optimal))
region

QUESTION 2

syms x
f = 0.65 - (0.75/(1+x^2)) - 0.65*x*atan(1/x) ;
fplot(f,'--')
a = 0.1;
b = 3;

i_length = b-a ;
while abs(i_length>0.0001)

x1 = a + (i_length/4) ;
x2 = b - (i_length/4) ;
xm = (a+b)/2 ;
f1 = subs(f,x,x1) ;
f2 = subs(f,x,x2) ;
fm = subs(f,x,xm) ;
if (f1>fm && fm>f2)
region = [xm,b] ;
a = xm ;
elseif (f1<fm && fm<f2)
region = [a,xm] ;
b = xm ;
else
region = [x1,x2] ;
a = x1 ;
b = x2 ;
end
i_length = b-a;
end
optimal = (a+b)/2 ;
optimal_value = eval(subs(f,x,optimal))
region

QUESTION 3

clc
clear all
syms x
f = 0.65 - (0.75/(1+x^2)) - 0.65*x*atan(1/x) ;
fplot(f,'--')
a = 0.1;
b = 3;
n=8;
i_length = b-a ;
d=0.0001;
while abs(i_length)>0.0002
x1 = a+ (i_length-d)/2;
x2 = a+ (i_length+d)/2 ;
f1 = eval(subs(f,x,x1)) ;
f2 = eval(subs(f,x,x2)) ;
if f1>f2
region = [x1,b] ;
a = x1 ;
else
region = [a,x2] ;
b = x2 ;
end
i_length = b-a;
end
region=[a,b];
optimal = (a+b)/2 ;
optimal_value = eval(subs(f,x,optimal))
region

ASSIGNMENT 5

QUESTION 1 USING FIBONACCI

syms x
f = 0.65 - (0.75/(1+x^2)) - 0.65*x*atan(1/x) ;
fplot(f,'--')
a = 0;
b = 3;
accu = 8;
iter =1;
n=0;
while 1~=2
if fibonacci(iter)>= 100/(2*accu)
n=iter;
break;
end
iter = iter+1;
end
n
k=2;

i_length =b-a;
for k =2:n
Lk= (fibonacci(n-k+1)/fibonacci(n+1))*i_length
x1 = a+Lk;
x2 = b-Lk;

f1 = eval(subs(f,x,x1)) ;
f2 = eval(subs(f,x,x2)) ;
if f1>f2
region = [x1,b] ;
a = x1 ;
else
region = [a,x2] ;
b = x2 ;

end
a=region(1);
b=region(2);
optimal=(a+b)/2;
end

optimal
optimal_value = eval(subs(f,x,optimal))
region

QUESTION 1 USING GOLDEN SECTION

syms x
f = 0.65 - (0.75/(1+x^2)) - 0.65*x*atan(1/x);
fplot(f,'--')
a = 0.1;
b = 3;
gamma=1.618;
accu = 8;
iter =0;
n=0;
while 1~=2
if (gamma)^(iter-1)>= 100/(2*accu)
n=iter;
break;
end
iter = iter+1;
end
n
i_length =b-a;
for i =2:n-1
Lk= ((1/(gamma))^i)*(i_length)
x1 = a+Lk;
x2 = b-Lk;

f1 = eval(subs(f,x,x1)) ;
f2 = eval(subs(f,x,x2)) ;
if f1>f2
region = [x1,b] ;
a=x1;
else
region = [a,x2] ;
b=x2;
end
a=region(1);
b=region(2);
optimal=(a+b)/2;
region;
end

optimal
optimal_value = eval(subs(f,x,optimal))
region

27 AUG LAB

QUESTION 1

syms x
f = x^2 - 4*x + 1;
fx = diff(f,x)
% sol = solve(fx==0,x)
t= 9;
a=0.1;
l = 1;
while abs(l)>0.00001
b=subs(fx,t);
t_new = t-a*b;
f1 = subs(f,x,t);
f2 = subs(f,x, t_new);
l = f2-f1;
t = t_new;
end
t;
fprintf('%f',t);
u = subs(f,x,t);
fprintf('%f',u);

QUESTION 2

syms x
f= x^4 - 2*x^3+2;
fx = diff(f,x)
% sol = solve(fx==0,x)
t= 2;
a=0.1;
l = 1;
while abs(l)>0.00001
b=subs(fx,t);
t_new = t-a*b;
f1 = subs(f,x,t);
f2 = subs(f,x, t_new);
l = f2-f1;
t = t_new;
end
t;
fprintf('%f',t);
u = subs(f,x,t)
fprintf('%f',u);

LINEAR PLOTTING

x = hwydata(:,14)
y = hwydata(:,4)
x = normalize(x);
y= normalize (y);
m= 0.1;
a=0.01;
c = 0.5;
loss = 100;
n= size(x,1);
while(loss>0.0001)
y_pred = m*x(i)+c;
m_deri=0;
c_deri=0;
for i=1:n
m_deri = m_deri +x(i)*(y(i)-y_pred);
end
m_deri = (-2/n)*(m_deri);
m= m - a*m_deri;

for i=1:n
c_deri = c_deri + (y(i)-y_pred);
end
c_deri = (-2/n)*c_deri;
c = c - a*c_deri;

loss =0;
for i=1:n
loss = loss +(y(i)-y_pred);
end
end

u = (loss^2)^(0.5)
u

You might also like