필터 지우기
필터 지우기

How to fine numerical gradient

조회 수: 4 (최근 30일)
Luqman Saleem
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
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;
% Gradient
[Fx, Fy] = gradient(fun_values, xs, ys);

추가 답변 (1개)

VBBV
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;
% Gradient
[Fx, Fy] = gradient(fun_values)
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

카테고리

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