필터 지우기
필터 지우기

How do I plot contours of a symbolic function at a specific heigh?

조회 수: 2 (최근 30일)
Devanjith Fonseka
Devanjith Fonseka 2018년 2월 4일
댓글: Karan Gill 2018년 2월 12일
So I'm trying to implement Newton's method for optimization and I don't know how to plot the contour at each step vector. I think I have to use ezplot but I keep on getting errors when I try to specify the contour height. Attached is my code. Thank you!
EDIT: I realized my arrows dont touch the points either.
function myNewton(x01,x02)
syms x1 x2
f=symfun((2-x1).^2+200*(x2-x1.^2).^2,[x1,x2]);
f1p=gradient(f,[x1,x2]);
f2p=hessian(f,[x1,x2]);
i=1;
% x01=45
% x02=33
x(1,1)=x01;
x(2,1)=x02;
ax1=linspace(0,20,100);
ax2=linspace(0,20,100);
[X1,X2]=meshgrid(ax1,ax2);
figure
z1=double(f(X1,X2))
contour(X1,X2,z1,[double(f(x01,x02)),double(f(x01,x02))])
axis([0 20 0 20])
hold on
plot(x01,x02,'*')
hold on
while norm(f1p(x(1,i),x(2,i)))>=10^-5
delx=-inv(f2p(x(1,i),x(2,i)))*f1p(x(1,i),x(2,i));
x(1,i+1)=x(1,i)+delx(1,1);
x(2,i+1)=x(2,i)+delx(2,1);
ax1=linspace(0,.1,100);
ax2=linspace(0,.1,100);
[X1,X2]=meshgrid(ax1,ax2);
z=double(f(X1,X2));
contour(X1,X2,z,[double(f(x(1,i+1),x(2,i+1))),double(f(x(1,i+1),x(2,i+1)))])
hold on
quiver(x(1,i),x(2,i),delx(1,1),delx(2,1))
hold on
i=i+1;
end
fprintf('%d %d %d',x(1,i),x(2,i),i)
  댓글 수: 1
Karan Gill
Karan Gill 2018년 2월 12일
Why do you need symbolic here? Is this possible with just numeric calculations? Also, for contours of sym expressions, use fcontour in 16a+ and ezcontour pre-16a.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by