フィルターのクリア

How to fine numerical gradient

3 ビュー (過去 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 件のコメント
Luqman Saleem
Luqman Saleem 2024 年 3 月 21 日
@Chunru It worked. thank you

サインインしてコメントする。

その他の回答 (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
  1 件のコメント
Luqman Saleem
Luqman Saleem 2024 年 3 月 21 日
It worked. thank you

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops 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