Extract numerical value from gradient

2 visualizaciones (últimos 30 días)
Baas28
Baas28 el 14 de Feb. de 2018
Comentada: Baas28 el 14 de Feb. de 2018
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!!!

Respuesta aceptada

Matt J
Matt J el 14 de Feb. de 2018
What if you just do things like,
if ~isnumeric(b)
b=b.x;
end
  1 comentario
Baas28
Baas28 el 14 de Feb. de 2018
Yes! That did the trick. Should've drunke my coffee a bit earlier...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Systems of Nonlinear Equations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by