decide the size of matrix returned by gradient function
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Rik
2020-5-27
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.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!