how to define a gradient vector of a given function?

8 vues (au cours des 30 derniers jours)
San Carlo
San Carlo le 16 Déc 2020
Modifié(e) : Ameer Hamza le 16 Déc 2020
Hello everybody! I have a function from R^n to R and I have computed its gradient (manually!) now I need to implement an array wich contains in each row the i-th partial derivative that I have manually computed and I don't know how to write it. Every partial derivative is the same and it's equal to x_i^2+2. I could write myself the vector with @(x) [df/dx1(x); df/dx2(x);....] but n is very large so i cannot do like this. What's the best way to do the task i need to do?

Réponses (1)

Ameer Hamza
Ameer Hamza le 16 Déc 2020
Modifié(e) : Ameer Hamza le 16 Déc 2020
You can calculate the analytical expression of gradient using the Symbolic Toolbox. For example,
syms x [10 1]
y = sum(x.^3)/3 + 2*sum(x);
dy = gradient(y)
Result
>> dy
dy =
x1^2 + 2
x10^2 + 2
x2^2 + 2
x3^2 + 2
x4^2 + 2
x5^2 + 2
x6^2 + 2
x7^2 + 2
x8^2 + 2
x9^2 + 2
To convert a numeric function handle, using matlabFunction()
dy_fun = matlabFunction(dy, 'Vars', {x});

Catégories

En savoir plus sur Symbolic Math Toolbox dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by