필터 지우기
필터 지우기

Plot multivariable function, find critical points

조회 수: 5 (최근 30일)
PJ
PJ 2018년 1월 18일
댓글: Star Strider 2018년 1월 19일
i need to plot a multivariable (x1,x2) function f_a in matlab, and find its critical points. I plotted it, but in order to find the critical points, i need to set the partial derivatives to zero then solve. when i try setting the partial derivatives to zero using diff(f_a,x1),it gives me an error.
[x1,x2] = meshgrid(-5:.2:5);
f_a = x1.^2 + x2.^2 +2.*x1.*x2;
figure(1)
surf(x1,x2,f_a)
fa1=diff(f_a,x1)

채택된 답변

Star Strider
Star Strider 2018년 1월 18일
Use the gradient function to calculate the derivative.
Try this:
[x1,x2] = meshgrid(-5:.2:5);
f_a = x1.^2 + x2.^2 +2.*x1.*x2;
fa1 = gradient(f_a, 0.2, 0.2); % Derivative
zv = contour(x1,x2,fa1, [0; 0]); % Critical Points
figure(1)
surf(x1,x2,f_a)
hold on
plot3(zv(1,2:end), zv(2,2:end), zeros(1,size(zv,2)-1), 'r', 'LineWidth',2)
hold off
  댓글 수: 4
PJ
PJ 2018년 1월 19일
I tried it for another function and i'm not sure if it is giving me correct figures because there seems to be 3 red lines as contour lines, and I added another contour plot and found the critical points after, but the contour plot of figure 2 did not match the red lines of figure 1. I'm sure this is repetitive, but could you please take a look:
[x1,x2] = meshgrid(-5:.2:5);
f_c = x1.^4 + x2.^4 +1 -x1.^2+x2.^2;
figure(1)
surf(x1,x2,f_c)
[fc1,fc2] = gradient(f_c,0.2,0.2); %what do the 0.2s represent?
zv = contour(x1,x2,fc1, [0; 0]); %what do the 0s represent?
figure(1)
surf(x1,x2,f_c)
hold on
plot3(zv(1,2:end), zv(2,2:end), zeros(1,size(zv,2)-1), 'r', 'LineWidth',2 )
hold off
figure (2)
contour(x1,x2,f_c)
hold on
quiver(x1,x2,fc1,fc2)
hold off
syms x1 x2
fc1=diff(x1.^4 + x2.^4 +1 -x1.^2+x2.^2,x1);
fc2=diff(x1.^4 + x2.^4 +1 -x1.^2+x2.^2,x2);
[x1cr,x2cr]=solve(fc1,fc2);
critical_points=[x1cr,x2cr]
Star Strider
Star Strider 2018년 1월 19일
‘what do the 0.2s represent?’
They are the differences between the consecutive values of the meshgrid values in each direction.
‘what do the 0s represent?’
They specify that I want the contour function to trace out and return only the contours at 0.
I cannot follow what you are doing in the code you posted.

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

추가 답변 (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