# How to fine numerical gradient

8 vues (au cours des 30 derniers jours)
Luqman Saleem le 20 Mar 2024
Commenté : Luqman Saleem le 21 Mar 2024
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.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Chunru le 21 Mar 2024
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 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Luqman Saleem le 21 Mar 2024
@Chunru It worked. thank you

Connectez-vous pour commenter.

### Plus de réponses (1)

VBBV le 21 Mar 2024
Modifié(e) : VBBV le 21 Mar 2024
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 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Luqman Saleem le 21 Mar 2024
It worked. thank you

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

R2023b

### Community Treasure Hunt

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

Start Hunting!

Translated by