functionmatlabsubstitutionsymbolic-math

Substitute Values into Symbolic Function Matlab


I have the following:

syms As(s) F0 alpha beta m1 m3
As(s) = ((F0*(alpha-beta)/(2*(s^2+(alpha-beta)^2)))-(F0*(alpha+beta)/(2*(s^2+(alpha+beta)^2))))/(m1+m3);
m1 = 1; % [kg]
m3 = 1; % [kg]
famp = 0.5; % [N]
scale = 0.01;
F0 = famp*scale; % [N]
alpha = 0.2;
beta = 0.15;
subs(As,{m1,m3,F0,alpha,beta},{m1,m3,F0,alpha,beta});

After substituting scalar values into As(s), As(s) still remains as: enter image description here

Why isn't MATLAB substituting the values I specified into As(s)?


Solution

  • Define your symbolic variable using syms.

    The syms function in MATLAB is used to create symbolic variables. These variables are used for symbolic math operations.

    syms s m c k m1 m3 F0 alpha beta
    
    As = ((F0*(alpha-beta)/(2*(s^2+(alpha-beta)^2)))-(F0*(alpha+beta)/(2*(s^2+ 
    (alpha+beta)^2))))/(m1+m3);
    
    m_val = 1; % [kg]
    c_val = 2; % [kg/s]
    k_val = 2; % [N/m]
    m1_val = 1; % [kg]
    m3_val = 1; % [kg]
    famp = 0.5; % [N]
    scale = 0.01;
    F0_val = famp * scale; % [N]
    alpha_val = 0.2;
    beta_val = 0.15;
    
    As_subs = subs(As, {m, c, k, m1, m3, F0, alpha, beta}, {m_val, c_val, 
    k_val, m1_val, m3_val, F0_val, alpha_val, beta_val});
    
    As_simplified = simplify(As_subs);
    disp(As_simplified);