potential() compute scalar potential from gradient vector field
4 次查看(过去 30 天)
显示 更早的评论
I have 2 [m, n] matrices dR_dx, dR_dy and dRho which is a (density) gradient vector field. I want to compute the scalar potential (the density) Rho by means of the matlab-function potential(V,X,Y).
I have a density gradient field:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1024300/image.png)
The total differential reads
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1024305/image.png)
with dx and dy being the size of the pixels in the image
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1024310/image.png)
Now I would like to integrate this total differential to get the actual density. I think it should be doable with the Matlab function potential()
X and Y are my cartesian coordinate vectors (in mm) along the dimensions of the image:
[m, n] = size(dRho); [1, n] = size(X); [m, 1] = size(Y);
dRho = (dR_dx + dR_dy) * dpix
As the function potential() deals with symbolic variables, I convert my quantities to symbolic matrix/vectors with the sym() function, 'f' for floating point precision.
dR_sym = sym(dRho, 'f');
dRdx_sym = sym(dRdx, 'f');
dRdy_sym = sym(dRdy, 'f');
And I create two symbolic vectors x and y:
x = sym('x', [1 n]);
y = sym('y', [1 m]);
Then I call the potential() function inside 2 for loops:
for i = 1:m
for j = 1:n
Rho_sym(i, j) = potential([dRdx_sym(i, j), dRdy_sym(i, j)], [x(j) y(i)], [x(1) y(1)]); % compute the potential of this value as analytical function
rxX = subs(Rho_sym(i, j), x(j), X(j)); % substitute all x symbolic values with the numeric one from X
ryY = subs(rxX, y(i), Y(i)); % substitute all y symbolic values with the numeric one from Y
Rho_pot(i, j) = double(ryY); % convert it to double precision
end
end
I don't know how to call exactly the potential() function/which matrices I should use as input: Can I give as input directly dRho_sym or should I use the the single partial derivatives dR_dx, dR_dy?
This is a more detailed description in the potential() function:
% P = POTENTIAL(V,X,Y) computes the potential of vector field V with
% respect to X using Y as base point for the integration. If Y is not a
% scalar, then Y must be of the same dimension as V and X. If Y is
% scalar, then Y is expanded into a vector of the same size as X with
% all components equal to Y.
%
% Examples:
% syms x y z; potential([x y z*exp(z)], [x y z])
% returns x^2/2 + y^2/2 + exp(z)*(z - 1).
%
% syms x0 y0 z0; potential([x y z*exp(z)], [x y z], [x0 y0 z0])
% returns x^2/2 - x0^2/2 + y^2/2 - y0^2/2 + exp(z)*(z - 1) -
% exp(z0)*(z0 - 1)
%
% potential([x y z*exp(z)], [x y z], [1,1,1])
% returns x^2/2 + y^2/2 + exp(z)*(z - 1) - 1
%
% potential([x y z*exp(z)], [x y z], 1)
% returns x^2/2 + y^2/2 + exp(z)*(z - 1) - 1
%
% potential([y -x], [x y])
% returns NaN since the potential does not exist.
0 个评论
回答(1 个)
Sachin Lodhi
2024-1-8
编辑:Sachin Lodhi
2024-1-8
Hello Philipp,
The ‘potential()’ function in MATLAB is used to compute the scalar potential of a vector field. Given a vector field ‘V’ and its coordinates ‘X’, the function integrates the vector field to find the scalar potential field.
For your scenario, the partial derivatives ‘dR_dx’ and ‘dR_dy’ represent the ‘x’ and ‘y’ components of the density gradient vector field. These should be input to ‘potential()’ function to find the scalar field Rho you seek.
Here's a modified version of your code.
% Assuming dR_dx and dR_dy are the m-by-n matrices of partial derivatives
% X and Y are the vectors representing the coordinates in the image
[m, n] = size(dR_dx); % Assuming dR_dx and dR_dy have the same size
X = linspace(x_start, x_end, n); % Define the X coordinates
Y = linspace(y_start, y_end, m); % Define the Y coordinates
% Convert the matrices to symbolic form for the potential function
dRdx_sym = sym(dR_dx, 'f');
dRdy_sym = sym(dR_dy, 'f');
% Create symbolic variables for x and y
syms x y
% Initialize the matrix to hold the potential values
Rho_pot = zeros(m, n);
% Compute the potential
for i = 1:m
for j = 1:n
% Compute the potential at each point
Rho_sym = potential([dRdx_sym(i, j), dRdy_sym(i, j)], [x, y], [X(1), Y(1)]);
% Substitute the symbolic variables with the actual coordinates
Rho_pot(i, j) = double(subs(Rho_sym, {x, y}, {X(j), Y(i)}));
end
end
Please replace ‘x_start’, ‘x_end’, ‘y_start’, and ‘y_end’ with the actual coordinate limits of x and y components respectively. Also, the code presumes that gradient field is conservative; otherwise, the ‘potential' function may not apply.
Hope this helps.
Best Regards,
Sachin
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!