# How to fine numerical gradient

조회 수: 9 (최근 30일)
Luqman Saleem 2024년 3월 20일
댓글: Luqman Saleem 2024년 3월 21일
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일
편집: 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