필터 지우기
필터 지우기

Plot multivariable function,can't get the plot right ?

조회 수: 1 (최근 30일)
Huy Nguyen
Huy Nguyen 2019년 5월 22일
댓글: Huy Nguyen 2019년 5월 23일
I need to plot a multivariable (x,y) function in matlab and find its critical points. I plotted it but can't seem to get the correct plot as shown in the picture.
[x,y] = meshgrid(-5:.2:5);
f=3.*x.*exp(y)-x.^3-exp(3.*y);
fa1 = gradient(f, 0.2, 0.2); % Derivative
zv = contour(x,y,fa1, [0; 0]); % Critical Points
figure(1)
surf(x,y,f)
hold on
plot3(zv(1,2:end), zv(2,2:end), zeros(1,size(zv,2)-1), 'r', 'LineWidth',2)
hold off

채택된 답변

Are Mjaavatten
Are Mjaavatten 2019년 5월 23일
The main problem with your plot is simply that the large values of f for y > 0.5 dwarf the variations at lower values. Changing the plotting range solves this problem, I also rotate the axes to make the graph more similar to the original.
[x,y] = meshgrid(-1.5:.1:1.5,-2:0.1:0.5);
f=3.*x.*exp(y)-x.^3-exp(3.*y);
figure(1)
surf(x,y,f)
xlabel x
ylabel y
view(136,3)
The critical points are where both partial derivatives are zero. You can visually find one solution by plotting the zero contours for bot in the same graph:
[fa1,fa2] = gradient(f, 0.2, 0.2); % Derivative
figure(1);
contour(x,y,fa1, [0; 0]);
hold on;
contour(x,y,fa2, [0; 0]); % maximum where df/dx = df/dy = 0
grid on % we find one such point at (1,0]
hold off
You can also find analytical expressions for the partial derivatives and solve using fsolve from the optimization toolbox. Here I use an anonymous function J. z(1) is x and z(2) is y.
J = @(z) [3*exp(z(2))-3*z(1)^2;3*z(1)*exp(z(2))-3*exp(3*z(2))]
zc = fsolve(J,[2;0])

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by