# How to fine numerical gradient

5 次查看（过去 30 天）
Luqman Saleem 2024-3-20
I have a function f(x,y). Following is just a sample function explaining how I save f(x,y) value in a 2D array.
clear; clc;
xs = linspace(1,2,100);
ys = linspace(1,3,100);
fun_values = zeros(100,100);
for ix = 1:100
x = xs(ix);
for iy = 1:100
y = ys(iy);
fun_values(ix,iy) = x^2+y^2;
end
end
I want to calculate and . I am confused what is the correct way to use gradient() function given the way how I store values in fun_values variable.

### 采纳的回答

Chunru 2024-3-21
clear; clc;
xs = linspace(1,2,100);
ys = linspace(1,3,100)'; % transpose here
fun_values = zeros(100,100);
%{
for ix = 1:100
x = xs(ix);
for iy = 1:100
y = ys(iy);
fun_values(ix,iy) = x^2+y^2;
end
end
%}
% Try use array operation instead of loops
fun_values = xs.^2 + ys.^2;
[Fx, Fy] = gradient(fun_values, xs, ys);
##### 1 个评论显示 -1更早的评论隐藏 -1更早的评论
Luqman Saleem 2024-3-21
@Chunru It worked. thank you

### 更多回答（1 个）

VBBV 2024-3-21

There is another way to find the numerical gradient for the given function
clear; clc;
xs = linspace(1,2,100);
ys = linspace(1,3,100)'; % transpose here
fun_values = zeros(100,100);
[Xs, Ys] = meshgrid(xs,ys);
% Try use array operation instead of loops
fun_values = Xs.^2 + Ys.^2;
Fx = 100×100
0.0203 0.0204 0.0206 0.0208 0.0210 0.0212 0.0214 0.0216 0.0218 0.0220 0.0222 0.0224 0.0227 0.0229 0.0231 0.0233 0.0235 0.0237 0.0239 0.0241 0.0243 0.0245 0.0247 0.0249 0.0251 0.0253 0.0255 0.0257 0.0259 0.0261 0.0203 0.0204 0.0206 0.0208 0.0210 0.0212 0.0214 0.0216 0.0218 0.0220 0.0222 0.0224 0.0227 0.0229 0.0231 0.0233 0.0235 0.0237 0.0239 0.0241 0.0243 0.0245 0.0247 0.0249 0.0251 0.0253 0.0255 0.0257 0.0259 0.0261 0.0203 0.0204 0.0206 0.0208 0.0210 0.0212 0.0214 0.0216 0.0218 0.0220 0.0222 0.0224 0.0227 0.0229 0.0231 0.0233 0.0235 0.0237 0.0239 0.0241 0.0243 0.0245 0.0247 0.0249 0.0251 0.0253 0.0255 0.0257 0.0259 0.0261 0.0203 0.0204 0.0206 0.0208 0.0210 0.0212 0.0214 0.0216 0.0218 0.0220 0.0222 0.0224 0.0227 0.0229 0.0231 0.0233 0.0235 0.0237 0.0239 0.0241 0.0243 0.0245 0.0247 0.0249 0.0251 0.0253 0.0255 0.0257 0.0259 0.0261 0.0203 0.0204 0.0206 0.0208 0.0210 0.0212 0.0214 0.0216 0.0218 0.0220 0.0222 0.0224 0.0227 0.0229 0.0231 0.0233 0.0235 0.0237 0.0239 0.0241 0.0243 0.0245 0.0247 0.0249 0.0251 0.0253 0.0255 0.0257 0.0259 0.0261 0.0203 0.0204 0.0206 0.0208 0.0210 0.0212 0.0214 0.0216 0.0218 0.0220 0.0222 0.0224 0.0227 0.0229 0.0231 0.0233 0.0235 0.0237 0.0239 0.0241 0.0243 0.0245 0.0247 0.0249 0.0251 0.0253 0.0255 0.0257 0.0259 0.0261 0.0203 0.0204 0.0206 0.0208 0.0210 0.0212 0.0214 0.0216 0.0218 0.0220 0.0222 0.0224 0.0227 0.0229 0.0231 0.0233 0.0235 0.0237 0.0239 0.0241 0.0243 0.0245 0.0247 0.0249 0.0251 0.0253 0.0255 0.0257 0.0259 0.0261 0.0203 0.0204 0.0206 0.0208 0.0210 0.0212 0.0214 0.0216 0.0218 0.0220 0.0222 0.0224 0.0227 0.0229 0.0231 0.0233 0.0235 0.0237 0.0239 0.0241 0.0243 0.0245 0.0247 0.0249 0.0251 0.0253 0.0255 0.0257 0.0259 0.0261 0.0203 0.0204 0.0206 0.0208 0.0210 0.0212 0.0214 0.0216 0.0218 0.0220 0.0222 0.0224 0.0227 0.0229 0.0231 0.0233 0.0235 0.0237 0.0239 0.0241 0.0243 0.0245 0.0247 0.0249 0.0251 0.0253 0.0255 0.0257 0.0259 0.0261 0.0203 0.0204 0.0206 0.0208 0.0210 0.0212 0.0214 0.0216 0.0218 0.0220 0.0222 0.0224 0.0227 0.0229 0.0231 0.0233 0.0235 0.0237 0.0239 0.0241 0.0243 0.0245 0.0247 0.0249 0.0251 0.0253 0.0255 0.0257 0.0259 0.0261
Fy = 100×100
0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0408 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0412 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0420 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0429 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0437 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0445 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0453 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0461 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0469 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478 0.0478
##### 1 个评论显示 -1更早的评论隐藏 -1更早的评论
Luqman Saleem 2024-3-21
It worked. thank you

### 类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

R2023b

### Community Treasure Hunt

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

Start Hunting!

Translated by