Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Week 5

Code:
clc
clear
syms x y mkr
f(x,y) = input('Enter the function f(x,y):');
p = diff(f,x);
q = diff(f,y);
[ax, ay] = solve(p,q);
ax = double(ax);
ay=double(ay);
r = diff(p,x);
s = diff(p,y);
t = diff(q,y);
D = r*t-s^2;
figure
fsurf(f);
legstr={'Function Plot'}; % For legend
for i=1:size(ax)
T1=D(ax(i),ay(i));
T2=r(ax(i),ay(i));
T3=f(ax(i),ay(i));
if(double(T1)==0)
sprintf('At (%f,%f) further investigation is required', ax(i),ay(i))
legstr = [legstr, {'Case of Further investigation'}];
mkr ='ko';
elseif (double(T1)<0)
sprintf('The point (%f,%f) is a saddle point', ax(i), ay(i))
legstr = [legstr, {'Saddle Point'}]; % updating Legend
mkr='bv'; % marker
else
if (double(T2) < 0)
sprintf('The maximum value of the function is f(%,%)=%f', ax(i),ay(i),
T3)
legstr = [legstr, {'Maximum value of the function'}];% updating Legend
mkr='g+';% marker
else
sprintf('The minimum value of the function is f(%f,%f)=%f',
ax(i),ay(i), T3)
legstr = [legstr, {'Minimum value of the function'}];% updating Legend
mkr='r*'; % marker
end
end
hold on
plot3(ax(i),ay(i), T3, mkr, 'Linewidth',4);
end
legend (legstr, 'Location', 'Best');

Output:

Q1.
Enter the function f(x,y):
x^4 + y^4 - x^2 - y^2 + 1

ans =

'The maximum value of the function is f('


ans =

'The minimum value of the function is f(-0.707107,-0.707107)=0.500000'


ans =

'The minimum value of the function is f(0.707107,-0.707107)=0.500000'


ans =

'The minimum value of the function is f(-0.707107,0.707107)=0.500000'


ans =

'The minimum value of the function is f(0.707107,0.707107)=0.500000'


ans =

'The point (-0.707107,0.000000) is a saddle point'


ans =

'The point (0.707107,0.000000) is a saddle point'


ans =

'The point (0.000000,-0.707107) is a saddle point'


ans =

'The point (0.000000,0.707107) is a saddle point'


Q2.
Enter the function f(x,y):
x^3 + 3*x*y^2 - 15*x^2 - 15*y^2 + 72*x

ans =

'The maximum value of the function is f('


ans =

'The minimum value of the function is f(6.000000,0.000000)=108.000000'


ans =

'The point (5.000000,-1.000000) is a saddle point'


ans =

'The point (5.000000,1.000000) is a saddle point'


Week 6

Code:

clc
clearvars
syms x y L;

f = input('Enter the function f(x, y): ');


g = input('Enter the constraint function g(x,y): ');

F = f + L*g;
gradF = jacobian(F, [x,y]);
[L,x1,y1] = solve (g, gradF(1), gradF (2), 'Real', true);
x1 = double(x1); y1 = double (y1);

xmx = max(x1); xmn = min(x1);


ymx = max(y1); ymn = min(y1);
range = [xmn-3 xmx+3 ymn-3 ymx+3];

ezmesh(f, range); hold on; grid on;


h = ezplot(g, range); set (h, 'LineWidth',2);
tmp = get(h, 'contourMatrix');

xdt = tmp(1,2: end);


ydt = tmp(2,2: end);
zdt = double(subs (f, {x,y}, {xdt,ydt}));

plot3(xdt,ydt, zdt, '-o', 'Color', 'b', 'MarkerSize', 10); axis (range);


for i = 1:numel(x1)
G(i) = subs(f, [x,y], [x1(1),y1(i)])
plot3(x1(i),y1(i), G(i), '*k', 'MarkerSize', 20);
end

title('Constrained Maxima/Minima')

Output:

Enter the function f(x, y):


x^2 + y^2
Enter the constraint function g(x,y):
x + y - 10

G =

50
Week 7:

Code:

clc
clear
syms x y z;
f = input('Enter f(x,y): ');
g1 = input('Enter g1(x): ');
g2 = input('Enter g2(x): ');

a = input('Enter the value of a: ');


b = input('Enter the value of b: ');

int(int(f,y,g1,g2),x,a,b)
viewSolid(z,0+0*x+0*y,f,y,g1,g2,x,a,b)

Output:

Q1:

Enter f(x,y): 16 - x^2 - 2*y^2


Enter g1(x): 0
Enter g2(x): 2
Enter the value of a: 0
Enter the value of b: 2

ans = 48
Q2:
Week 8

Code:
clear
clc
syms x y z
xa = input("Enter the lower limit of x please: ");
xb = input("Enter the upper limit of x please ");
ya = input("Enter the lower limit of y please ");
yb = input("Enter the upper limit of y please ");
za = input("Enter the lower limit of z please ");
zb = input("Enter the upper limit of z please ");
I = int(int(int(1+0*z, z, za, zb), y, ya, yb), x, xa, xb) ;
viewSolid (z, za, zb, y, ya, yb, x, xa, xb)

Output:

Q1:

Q2:

You might also like