decide the size of matrix returned by gradient function
이전 댓글 표시
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일
Where do you define psi? That seems the most important part, since the rest of the code doesn't affect gradient.
Sanjana Singh
2020년 5월 27일
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.
Sanjana Singh
2020년 5월 27일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!