how can i find the gradient of this function

1 次查看(过去 30 天)
hi everyone, I am trying to calculate the gradient of this function (in X and Y direction): Lw=sum(w(x,y) * l (x, y)); where l = (sqrt( (x(i+1)-x(i))^2-(y(i+1)-y(i))^2 ));
and what I do is for the X direction: coef=( w*(x(i+1)-x(i))/( (x(i+1)-x(i))^2+(y(i+1)-y(i))^2 ) ); Gx(i)=gwx* (sqrt( (x(i+1)-x(i))^2-(y(i+1)-y(i))^2 )) + ( w*(x(i+1)-x(i))/( (x(i+1)-x(i))^2+(y(i+1)-y(i))^2 ) ); Gx(i)=Gx(i)+coef;
For the y direction: coef2=( w*(y(i+1)-y(i))/( (x(i+1)-x(i))^2+(y(i+1)-y(i))^2 ) ); Gy(i)=gwy* (sqrt( (x(i+1)-x(i))^2-(y(i+1)-y(i))^2 )) + ( w*(y(i+1)-y(i))/( (x(i+1)-x(i))^2+(y(i+1)-y(i))^2 ) ); Gy(i)=Gx(i)+coef2;

回答(1 个)

Star Strider
Star Strider 2016-1-7
I cannot follow the code in your post that you want to work with. There are Symbolic Math Toolbox functions that can calculate the gradient of a symbolic function if you want to do that.
To calculate a numerical gradient, I would start by defining the x and y ranges of values using the meshgrid function, then use the gradient function to do a numerical gradient calculation. See the documentation for those functions for the details. (Both meshgrid and gradient are core MATLAB functions so you do not need any additional Toolboxes to use them.)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by