matlabsymbolic-mathfunction-handle

Vector inputs when converting symbolic expression to function handle


I have a symbolic ODE:

syms x1 x2 cs ks ms t2 real
xx=[x1 x2];   
fun_sym=[xx(2); (cs/ms)*(xx(1)^2-1)*xx(2) - (ks/ms)*xx(1)];

I want to solve it using ODE function, but first I need to convert it into function handle:

v=matlabFunction(fun_sym,'vars', [t2,xx,cs,ks,ms]);
[T,x]= ode15s(@(t2,xx) v,t,[1 0]); 

where t=[0:0.1:1]. Matlab gives an error:

@(T2,XX)V returns a vector of length 1, but the length of initial conditions vector is 2. 

I think this is because it interpret v as: @(t2,x1,x2,cs,ks,ms), so it expects the second argument to be x1, which is a 1-by-1 element, even though I have defined v as [t2,xx,...], where xx is a 1-by-2 element. How can I solve this issue?


Solution

  • This works, but you need to give values to the other parameters as well (cs, ks, and ms)

    v=matlabFunction(fun_sym,'vars', [t2,x1,x2,cs,ks,ms]);
    [T,x]= ode15s(@(t2,xx) v(t2,xx(1),xx(2),cs,ks,ms),t,[1 0]);