フィルターのクリア

To take the partial derivative of a function using matlab

375 ビュー (過去 30 日間)
Pranjal Pathak
Pranjal Pathak 2013 年 2 月 11 日
編集済み: Sergio E. Obando 2024 年 6 月 15 日
Here is a particular code. Can anyone please help me in taking the analytical (partial) derivative of the function 'F' along X (i.e., w.r.t. X) along Y (i.e., w.r.t. Y) and along the diagonal (i.e., w.r.t. X plus w.r.t. Y) using matlab command.
[X, Y]=meshgrid(-1:2/511:+1, -1:2/511:+1);
F=sqrt(3).*(2.*(X.^2+Y.^2)-1);
Thanking You!

採用された回答

Walter Roberson
Walter Roberson 2013 年 2 月 11 日
If you have the symbolic toolkit
syms X Y
F=sqrt(3).*(2.*(X.^2+Y.^2)-1);
diff(F,X)
diff(F,Y)
diff(F,X,Y)
  5 件のコメント
Olivar Luis Eduardo
Olivar Luis Eduardo 2023 年 4 月 25 日
I think that numerical calculation is being requested, not the symbolic one. In other words, the surface is a matrix
Sergio E. Obando
Sergio E. Obando 2024 年 6 月 15 日
For clarification, the numerical and symbolic calculations are fairly similar code wise. Walter has provided the symbolic gradients (as requested), while Youssef has provided the numerical ones. One correction is that dX = matlabFunction(diff(F,x)) is only a function of x, which is why it generates an error when calling dX(x,y).
To go in a bit more detail on Walter's suggested solution:
clear
% Define Mesh
[ X,Y] = meshgrid(-1:2/10:1,-1:2/10:1); % Using 2/10 as spacing
% Analytical Solution (i.e. symbolic var and fnc)
syms F(x,y)
F(x,y) = sqrt(3).*(2.*(x.^2+y.^2)-1);
fsurf(F) % You don't have to pass X or Y
% Analytical Gradients and Hessian
dFdx = diff(F,x)
dFdx(x, y) = 
dFdy = diff(F,y)
dFdy(x, y) = 
gradF = gradient(F)
gradF(x, y) = 
HessF = hessian(F)
HessF(x, y) = 
% or Second Order Derivatives
dFdxdy = diff(F,x,y)
dFdxdy(x, y) = 
0
% Evaluate gradients on a given range
dFdX = double(dFdx(-1:2/10:1,y))
dFdX = 1x11
-6.9282 -5.5426 -4.1569 -2.7713 -1.3856 0 1.3856 2.7713 4.1569 5.5426 6.9282
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Substitute numerical values in symbolic expression
U = subs(dFdx,[x y],{X,Y}); % call double to convert to numeric representation
V = subs(dFdy,[x y],{X,Y});
figure
fcontour(F,[-1 1],"Fill","on")
hold on
quiver(X,Y,U,V,'r')
hold off
And now compare with the numerical approach:
clear
% Numerical Solution
F= @(x,y) sqrt(3).*(2.*(x.^2+y.^2)-1);
fsurf(F,[-1 1])
[X, Y]=meshgrid(-1:2/10:1,-1:2/10:1);
figure
Z = F(X,Y);
surf(X,Y,Z)
% Numerical Gradient
[Fx,Fy] = gradient(Z);
figure
contourf(X,Y,Z)
hold on
quiver(X,Y,Fx,Fy,'r')
hold off

サインインしてコメントする。

その他の回答 (4 件)

Youssef  Khmou
Youssef Khmou 2013 年 2 月 11 日
編集済み: Youssef Khmou 2013 年 2 月 11 日
hi , you can use "gradient" :
[dF_x,dF_y]=gradient(F);
subplot(1,2,1), imagesc(dF_x), title(' dF(x,y)/dx')
subplot(1,2,2), imagesc(dF_y), title(' dF(x,y)/dy')
  2 件のコメント
Walter Roberson
Walter Roberson 2013 年 2 月 11 日
If you do not use the symbolic toolbox, gradient is numeric rather than analytic.
Youssef  Khmou
Youssef Khmou 2013 年 2 月 11 日
編集済み: Youssef Khmou 2013 年 2 月 11 日
True, but he has two sides because his example is numerical, you answered to the theoretical side ,while i answered to the numerical one,

サインインしてコメントする。


rapalli adarsh
rapalli adarsh 2019 年 1 月 9 日
syms c(x,y);
c(x,y)=input('enter cost Rs=\n');
cx=diff(c,x);
cy=diff(c,y);
s1=double(cx(80,20));
s2=double(cy(80,20));
if s1>s2 disp('fire standind stores')
else disp('fire standing stores')
end

Santhiya S
Santhiya S 2023 年 3 月 19 日
Using MATLAB, find the partial derivative with respect to ‘x’ and ‘y’ of the function f(x) = tan−1(x/y)
  1 件のコメント
Sergio E. Obando
Sergio E. Obando 2024 年 6 月 15 日
Replace your function in Walter's code:
syms f(x,y)
f(x,y) = atan(x/y)
f(x, y) = 
dFdx = diff(f,x)
dFdx(x, y) = 
dFdy = diff(f,y)
dFdy(x, y) = 

サインインしてコメントする。


Olivar Luis Eduardo
Olivar Luis Eduardo 2023 年 4 月 25 日
Good morning, I also have the same question, I have consulted a lot on the web, but they always give answers as if the surface were symbolic, but it is numerically and the calculation of the partial derivative of a matrix of order mxn remains.
  1 件のコメント
Sergio E. Obando
Sergio E. Obando 2024 年 6 月 15 日
編集済み: Sergio E. Obando 2024 年 6 月 15 日
Please take a look at my comment above. The surface values are found by substituting/evaluating the symbolic expression at the grid points. Assuming you are using R2021b or later, you may find symmatrix useful for manipulation of matrix expressions, e.g. gradient of matrix multiplication

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCalculus についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by