How to use jacobian after odeToVectorField

7 次查看(过去 30 天)
good evening, i don't understand what's the variables name of the output vector of the function odeToVectorField, the problem is that both the documentation and the vector have this notation Y[1] which is not a possible name for a variable as far as i know. If i try to use Y_1 as a variable it dosen't work but i can't use the real name either because it gives me an error, does anyone have an idea?
syms theta_1(t) theta_2(t) L_1 L_2 m_1 m_2 g
x_1 = L_1*sin(theta_1);
y_1 = -L_1*cos(theta_1);
x_2 = x_1 + L_2*sin(theta_2);
y_2 = y_1 - L_2*cos(theta_2);
vx_1 = diff(x_1);
vy_1 = diff(y_1);
vx_2 = diff(x_2);
vy_2 = diff(y_2);
ax_1 = diff(vx_1);
ay_1 = diff(vy_1);
ax_2 = diff(vx_2);
ay_2 = diff(vy_2);
syms T_1 T_2
eqx_1 = m_1*ax_1(t) == -T_1*sin(theta_1(t)) + T_2*sin(theta_2(t));
eqy_1 = m_1*ay_1(t) == T_1*cos(theta_1(t)) - T_2*cos(theta_2(t)) - m_1*g;
eqx_2 = m_2*ax_2(t) == -T_2*sin(theta_2(t));
eqy_2 = m_2*ay_2(t) == T_2*cos(theta_2(t)) - m_2*g;
Tension = solve([eqx_1 eqy_1],[T_1 T_2]);
eqRed_1 = subs(eqx_2,[T_1 T_2],[Tension.T_1 Tension.T_2]);
eqRed_2 = subs(eqy_2,[T_1 T_2],[Tension.T_1 Tension.T_2]);
L_1 = 1;
L_2 = 1.5;
m_1 = 2;
m_2 = 1;
g = 9.8;
eqn_1 = subs(eqRed_1);
eqn_2 = subs(eqRed_2);
[V,S] = odeToVectorField(eqn_1,eqn_2);
jacob = jacobian(V,sym('Y',[1 4]))
jacob = 
subs(jacob,sym('Y',[1 4]),[0 0 0 0])
ans = 
sym('Y[1]')
Error using sym>convertChar
Character vectors and strings in the first argument can only specify a variable or number. To evaluate character vectors and strings representing symbolic expressions, use 'str2sym'.

Error in sym>tomupad (line 1624)
S = convertChar(x);

Error in sym (line 397)
S.s = tomupad(x);
  1 个评论
Paul
Paul 2022-11-28
编辑:Paul 2022-11-28
Hi Gabriele,
It's a mystery to me as well: link to question.
It seems like the only real use of the outputs are as inputs to matlabFunction for numerical solution of the ODE via the suited of ode solvers.

请先登录,再进行评论。

回答(1 个)

Sakshay
Sakshay 2022-12-1
Hello Gabriele,
As per my understanding you want to use the output of the "odeToVectorField" for further processing using symbolic expressions, like the "jacobian" function.
Currently, MATLAB only supports generating a "matlabFunction" from the output of "odeToVectorField" function. The symbolic variable used to construct the output of “odeToVectorField” is “Y”. This variable allows for the generation of a MATLAB function handle.
However, you can use a workaround for the same to convert the “Y[i]” to “yi”, in the output of “odeToVectorField” function. This would enable to use this output in the “jacobian” function for further processing. A sample code would look like:
% Convert the Y[i] in odeToVectorField output V to yi
V_c = feval(symengine,'evalAt',V,'Y=[y1,y2,y3,y4]');
% Evaluate jacobian with respect to yi
jacob = jacobian(V_c, sym('y', [1 4]));
For more information on "odeToVectorField" function, you can refer to the following documentation:

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by