decide the size of matrix returned by gradient function

1 visualización (últimos 30 días)
Sanjana Singh
Sanjana Singh el 27 de Mayo de 2020
Comentada: Sanjana Singh el 27 de Mayo de 2020
I want to find gradient of a function (x,y) in a loop and want to get a matrix of 10x10 size as the gradient matrix. I tried using - [___] = gradient(F,h) to set the distance between each point with different values of h. It doesn't seem to work and only returns a 100X100 matrix whatever I try (In the code below I get a 100x100 matrix for vfy1 and vfx1. How do I make the gradient command return only a 10X10 matrix?
Code-
x_grid = linspace(1,3,10);
y_grid = linspace(1,3,10);
for i = 1:1:length(x_grid)%rows
x = x_grid(i);
vpx_0 = 5;% initialize particle velocity in x and y directions
vpy_0 = 0;
for j = 1:1:length(y_grid) %columns
y = y_grid(j);
[vfy1,vfx1] = gradient(psi,10); % here is the gradient command
vfy= -1*vfy1;
vfx = vfx1;
vpx(i,j) = sqrt((vpx_0).^2 + 2*g(1)*x);
vpy(i,j) = -1*sqrt(vpy_0.^2 + abs(2*g(2)*y));
vpx_0 = vpx(i,j);
vpy_0 = vpy(i,j);
vfx_sum = vfx_sum + vfx;% has been pre-allocated (not shown here)
vfy_sum = vfy_sum + vfy;% has been pre allocated (not shown here)
end
end
  4 comentarios
Rik
Rik el 27 de Mayo de 2020
As psi is a 100x100 array, so is the output of gradient. The only thing the second input does is scaling the output. If you want a 10x10, you will have to enter a 10x10 matrix or downsample the result.
Sanjana Singh
Sanjana Singh el 27 de Mayo de 2020
Thank you, I didn't realize that. This worked!

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Graph and Network Algorithms en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by