Question

Please use matlab for the following problem

I. For the function. /(x) -X-4X+2XY+2Y+2Y+14 a. Plot the surface function for X over [5 6]. and Y over [-4, -2]. ecolour plot

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a.Matlab Code

[X,Y] = meshgrid(5:0.1:6,-4:.1:-2);
Z = X.^2-4*X+2*X.*Y+2*Y.^2+2*Y+14;
surf(X,Y,Z)

Output

6 5 3 654321-2

b. Matlab Code

x = linspace(0,10);
y = linspace(-4,-2);
[X,Y] = meshgrid(x,y);
Z = X.^2-4*X+2*X.*Y+2*Y.^2+2*Y+14;
v = [1 1.25 1.5 2 2.5 3];

Output

2.2 -2.4 -2.6 -2.8 -3.2 -3.4 -3.6 -3.8 0 1 234 5 6 78 9 10

(c)Gradient Descent Method

Matlab Code

function file steep.m code

function [f]=steep(x)
f=(x(1))^2-4*x(1)+2*x(1)*x(2)+2*(x(2))^2+2*(x(2))+14;
end

Steepest descent main m file

clc
clear
n=input('input n');

y=input('y');
tic

B=zeros(n,n);
x=sym('x', [n 1],'real');
f=steep(x);
J=jacobian(f,x);
fun_ev=1;
NORM=1;
k=0;
%H=eye(n);
time=0;
while(1~=2)
L1=vpa(subs(J,x,y),7);
NORM=vpa(norm(L1),7);
if(NORM<10^-6)
display('flag NORM');
break;
end
if(time>3000)
display('flag TIME');
break;
end
% p=-H*vpa((subs(J,x,y))',7);
p=-vpa((subs(J,x,y))',7);
alpha=1;
beta=0.5; %sigma in given problem
c1=0.1;%s in given problem
%c2=0.9;
fun_ev=fun_ev+1;
while(vpa(subs(f,x,y+alpha*p),7)> vpa(subs(f,x,y),7)+c1*alpha*p'*vpa(subs(J,x,y)',7))
alpha=beta*alpha;
end
z=vpa(y+alpha*p,7);
fun_ev=fun_ev+1;

k=k+1;

y=z;
time=toc;

end
k
toc
NORM
y
f1=steep(y)
fun_ev

Output

input n2
y[4;-4]
flag NORM

k =

38

Elapsed time is 16.544480 seconds.

NORM =

0.0000009536743


y =

4.999999 (Minimizer)
-3.0


f1 =

1.000000000000454747350886464119 (Minimum Value)

fun_ev =

77

Add a comment
Know the answer?
Add Answer to:
I. For the function. /(x) -X-4X+2XY+2Y+2Y+14 a. Plot the surface function for X over [5 6]. and Y...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT