Extract numerical value from gradient

12 ビュー (過去 30 日間)
Baas28
Baas28 2018 年 2 月 14 日
コメント済み: Baas28 2018 年 2 月 14 日
Hi all,
I'm working with a third party optimizer (GPOPS) that interacts with MATLAB. As I need to calculate the root of a 4th order polynomial, I'm using the "roots"-function to solve for these:
a = 1;
b = 2*(UC(i));
c = (((UC(i)).^2) + ((UT(i)).^2));
d = 0;
e = -1;
v_i_norm_roots(:,i) = roots([a b c d e]);
This works at first when the problem is initialized, because at this point the values for UC and UT are doubles. However, after this the optimizer starts the iterative optimization. At this point, UC and UT are not doubles anymore, but a gradient of this value is sent to the "roots"-function. Because of this I cannot get the "roots"-function to work as I get the error:
Error using zeros
CLASSNAME input must be a valid numeric or logical
class name.
Error in roots (line 32)
r = zeros(0,1,class(c));
Error in vinorm_analytical_testwithroots (line 65)
v_i_norm_roots(:,i) = roots([a b c d e]);
When the gradient starts being sent to the roots function b and c equal the following:
b
gradient value b.x =
0.1100
gradient derivative(s) b.dx =
(1,244) 0.1100
(1,325) 0.0019
(1,649) -0.0019
(1,890) -4.2319
(1,1050) 0.0019
c
gradient value c.x =
0.0030
gradient derivative(s) c.dx =
(1,244) 0.0061
(1,325) -0.0000
(1,649) 0.0000
(1,890) -0.2329
(1,1050) -0.0000
To calculate the roots, I'm only interested in the first value of the gradient; 0.1100 in b and 0.0030 in c. How can I 'extract' this value from a gradient, so I can use it to solve for the root?
Many thanks!!!

採用された回答

Matt J
Matt J 2018 年 2 月 14 日
What if you just do things like,
if ~isnumeric(b)
b=b.x;
end
  1 件のコメント
Baas28
Baas28 2018 年 2 月 14 日
Yes! That did the trick. Should've drunke my coffee a bit earlier...

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSystems of Nonlinear Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by