MATLAB: Can’t get the subs command to work after Jacobian

#jacobian #symbolic #scripts

Hello everyone, thank you for taking the time to read my post.
I am trying to change a symbolic variable to a double but when using the command subs() it does nothing in return. First, i tried defining my variables in the following form:
u1=110.9875 u2=20.8102 m1=10;m2=3 r=1;g=9.81 x1=pi/4 x2=2 x3=0 x4=0; syms m1 m2 r g x1 x2 x3 x4 x1_SS x2_SS x3_SS x4_SS u1 u2 u
and run my code to compute the Jacobian. Once the Jacobian was completed, i would do:
subs(m1,m2,r,g,x1,x2,x3,x4,x1_SS,x2_SS,x3_SS,x4_SS,u1,u2,u)
to change the symbolic variables to double. Since none of that worked, this is the code that I am running right now and still can get the script to change syms to double. The code runs and the Jacobian is calculated.
This is my code:
clear all;clc;
syms m1 m2 r g x1 x2 x3 x4 x1_SS x2_SS x3_SS x4_SS u1 u2 u
x=[x1;x2;x3;x4];
u=[u1,u2];
%creating SS representation: x1_SS=x3;
x2_SS=x4;
x3_SS=(-2*m2*x3*x2*x4-g*cos(x1)*(m1*r+m2*x2)+u1)/(m1*r^2+m2*x2^2);
x4_SS=x3^2*x2-g*sin(x1)+((u2))/(m2);
x_SS=[x1_SS;x2_SS;x3_SS;x4_SS];
A_jacobi=jacobian(x_SS,x);
B_jacobi=jacobian(x_SS,u);
subs(u1,110.9875);subs(u2,20.8102);subs(m1,10);subs(m2,3); subs(r,1);subs(g,9.81);subs(x1,pi/4);subs(x2,2);subs(x3,0);subs(x3,0)
A_jacobi
B_jacobi
and this is my output:
A_jacobi = [ 0, 0, 1, 0] [ 0, 0, 0, 1] [ (g*sin(x1)*(m1*r + m2*x2))/(m1*r^2 + m2*x2^2), (2*m2*x2*(g*cos(x1)*(m1*r + m2*x2) – u1 + 2*m2*x2*x3*x4))/(m1*r^2 + m2*x2^2)^2 – (g*m2*cos(x1) + 2*m2*x3*x4)/(m1*r^2 + m2*x2^2), -(2*m2*x2*x4)/(m1*r^2 + m2*x2^2), -(2*m2*x2*x3)/(m1*r^2 + m2*x2^2)] [ -g*cos(x1), x3^2, 2*x2*x3, 0]
B_jacobi =
[ 0, 0] [ 0, 0] [ 1/(m1*r^2 + m2*x2^2), 0] [ 0, 1/m2]
Only syms :/

Best Answer

You have to tell subs the expression in which to do the substitution:
A_jacobi_n = subs(A_jacobi, {u1,u2,m1,m2,r,g,x1,x2,x3}, {110.9875,20.8102,10,3,1,9.81,pi/4,2 0})
B_jacobi_n = subs(B_jacobi, {u1,u2,m1,m2,r,g,x1,x2,x3}, {110.9875,20.8102,10,3,1,9.81,pi/4,2 0})
A_jacobi_n =
[ 0, 0, 1, 0]
[ 0, 0, 0, 1]
[ (981*2^(1/2))/275, (61803*2^(1/2))/48400 - 26637/9680, -(6*x4)/11, 0]
[ -(981*2^(1/2))/200, 0, 0, 0]
B_jacobi_n =
[ 0, 0]
[ 0, 0]
[ 1/22, 0]
[ 0, 1/3]
Note that ‘x4’ remains a symbolic variable. You could also use vpa to get more tractable constants.