You are on page 1of 12

Question 4-part A

clear all
[x1,x2]=meshgrid(-20:0.05:20, -20:0.05:6);
f=4*x1.^2+3*x2.^2-5*x1*x2-8;
g1=x1+x2-4;
cla reset
axis ([-0.5 6 -0.5 6])
xlabel('x1'),ylabel('x2')
title('Question Four part A')
hold on
cv1=[0:0.01:1];
const1=contour(x1,x2,g1,cv1,'g');
cv1=[0 0.001];
const1=contour(x1,x2,g1,cv1,'k');
cv2=[0:0.01:0.5];
fv=[1.833 2.166 -3.833 ];
fs=contour(x1,x2,f,fv,'b');
grid
hold off
Question four-part B

clear all

[x1,x2]=meshgrid(-10:0.1:20, -10:0.1:20);

f=4*x1.^2+3*x2.^2-5*x1*x2-8;

g1=x1+x2-4;

cla reset

axis ([-20 10 -20 10])

xlabel('x1'),ylabel('x2')

title('Question four part B')

hold on

cv1=[0:0.0005:0.5];

const1=contour(x1,x2,g1,cv1,'g');

cv1=[0 0.001];

const1=contour(x1,x2,g1,cv1,'k');

fv=[1.8 2.16 -3.88];

fs=contour(x1,x2,f,fv,'b');

hold off
Question 4-part C

clear all

[x1,x2]=meshgrid(-1:0.1:20, -1:0.1:20);

f=(x1-1).^2+(x2-1).^2;

g1=x1+x2-4;

g2=x1-x2-2;

cla reset

axis ([-0.1 10 -1 10])

xlabel('x1'),ylabel('x2')

title('Q4c')

hold on

cv1=[0:0.0005:0.5];

const1=contour(x1,x2,g1,cv1,'g');

cv1=[0 0.001];

const1=contour(x1,x2,g1,cv1,'k');
cv2=[0:0.0005:0.5];
const2=contour(x1,x2,g2,cv2,'g');
cv2=[0 0.001];
const2=contour(x1,x2,g2,cv2,'k');

fv=[3 1 -2];

fs=contour(x1,x2,f,fv,'b');

hold off

Question 4-part D

clear all

[x1,x2]=meshgrid(-1:0.1:20, -1:0.1:20);

f=2*x1+3*x2-x1.^3-2*x2.^2;

g1=5*x1+2*x2-10;

g2=x1-x2-2;
g3=-x1;

g4=-x2;

cla reset

axis ([-0.1 10 -1 10])

xlabel('x1'),ylabel('x2')

title('Q4d')

hold on

cv1=[0:0.0005:0.5];

const1=contour(x1,x2,g1,cv1,'g');

cv1=[0 0.001];

const1=contour(x1,x2,g1,cv1,'k');

cv2=[0:0.0005:0.5];
const2=contour(x1,x2,g2,cv2,'g');
cv2=[0 0.001];
const2=contour(x1,x2,g2,cv2,'k');

cv3=[0:0.0005:0.1];
const3=contour(x1,x2,g3,cv3,'g');
cv3=[0 0.001];
const3=contour(x1,x2,g3,cv3,'k');
cv4=[0:0.0005:0.1];
const4=contour(x1,x2,g4,cv4,'g');
cv4=[0 0.001];
const4=contour(x1,x2,g4,cv4,'k');

fs=contour(x1,x2,f,'b');

hold off
Question Four-part E

clear all

[x1,x2]=meshgrid(-1:0.1:20, -1:0.1:20);

f=(x1-1).^2+(x2-1).^2;

g1=x1+x2-4;

g2=2-x1;
cla reset

axis ([-0.1 10 -1 10])

xlabel('x1'),ylabel('x2')

title('Q4e')

hold on

cv1=[0:0.0005:0.5];

const1=contour(x1,x2,g1,cv1,'g');

cv1=[0 0.001];

const1=contour(x1,x2,g1,cv1,'k');

cv2=[0:0.0005:0.5];
const2=contour(x1,x2,g2,cv2,'g');
cv2=[0 0.001];
const2=contour(x1,x2,g2,cv2,'k');

fs=contour(x1,x2,f,'b');

hold off
Question Three Part A

clear all

[x1,x2]=meshgrid(-1:0.1:20, -1:0.1:20);

f=4*x1.^2+3*x1.^2-5*x1*x2-8*x1;

g1=x1+x2-4;

cla reset

axis ([-0.1 10 -1 10])

xlabel('x1'),ylabel('x2')

title('Q3a')

hold on

cv1=[0:0.0005:0.5];
const1=contour(x1,x2,g1,cv1,'g');

cv1=[0 0.001];

const1=contour(x1,x2,g1,cv1,'k');

fs=contour(x1,x2,f,'b');

hold off

Question Three Part B

clear all

[x1,x2]=meshgrid(-1:0.1:20, -1:0.1:20);

f=(x1-2).^2+(x2+1).^2;

g1=2*x1+3*x2-4;

cla reset

axis ([-0.1 20 -1 20])


xlabel('x1'),ylabel('x2')

title('Q3b')

hold on

cv1=[0:0.0005:0.5];

const1=contour(x1,x2,g1,cv1,'g');

cv1=[0 0.001];

const1=contour(x1,x2,g1,cv1,'k');

fs=contour(x1,x2,f,'b');

hold off
Question Three Part C

clear all

[x1,x2,x3]=meshgrid(-1:0.1:20, -1:0.1:20, -1:0.1:20);

f=(x1-1).^2+(x2+2).^2+(x3-2).^2;

g1=2*x1+3*x2-1;

g2=x1+x2+2*x3-4;

cla reset

axis ([-0.1 20 -1 20])

xlabel('x1'),ylabel('x2')

title('Q3c')

hold on

cv1=[0:0.0005:0.5];

const1=contour(x1,x2,g1,cv1,'g');

cv1=[0 0.001];

const1=contour(x1,x2,g1,cv1,'k');

cv2=[0:0.0005:0.5];
const2=contour(x1,x2,g2,cv2,'g');
cv2=[0 0.001];
const2=contour(x1,x2,g2,cv2,'k');
fs=contour(x1,x2,f,'b');

hold off

You might also like