how to define a gradient vector of a given function?

1 回表示 (過去 30 日間)
San Carlo
San Carlo 2020 年 12 月 16 日
編集済み: Ameer Hamza 2020 年 12 月 16 日
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?

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 12 月 16 日
編集済み: Ameer Hamza 2020 年 12 月 16 日
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});

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by