Compute the partial derivative numerically

Hi, I want to compute the first and second partial derivative with respect to x, y for this function
x0=0
y0=0
x=[-1:0.1:1];
y=[-2:0.1:2];
v=x+exp(-((x-x0).^2+(y-y0).^2))

 채택된 답변

Star Strider
Star Strider 2017년 12월 21일

0 개 추천

See if the gradient (link) function will do what you want:
Example
x0=0;
y0=0;
x=[-1:0.1:1];
y=[-2:0.1:2];
[X,Y] = meshgrid(x, y);
v = @(x,y) x+exp(-((x-x0).^2+(y-y0).^2));
[dX,dY] = gradient(v(X,Y));
figure(1)
surf(X, Y, v(X,Y), 'FaceAlpha',0.5, 'EdgeColor',[0.3 0.3 0.7])
hold on
surf(X, Y, dX, 'EdgeColor','g')
surf(X, Y, dY, 'EdgeColor','r')
hold off
grid on
legend('v(x,y)', 'dX', 'dY')
xlabel('\bfX')
ylabel('\bfY')
view(40, 20)
figure(2)
contour(X, Y, v(X,Y))
hold on
quiver(X, Y, dX, dY)
hold off
legend('v(x,y)', 'Gradient')

댓글 수: 4

F.O
F.O 2017년 12월 21일
편집: F.O 2017년 12월 21일
and i f i want the second derivative then i just write ?????
[dXX,dYY]=gradient(gradient (v(x,y))
The easiest way is to use the related del2 (link) function. Remember to multiply it by 4 to get the second derivative:
d2Z = 4*del2(v(X,Y));
referencing my earlier code for the function call. See the del2 documentation for details, since it works a bit differently than gradient.
F.O
F.O 2017년 12월 21일
Thanks for the answer
Star Strider
Star Strider 2017년 12월 21일
My pleasure.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

질문:

F.O
2017년 12월 21일

편집:

F.O
2017년 12월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by