1

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?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
student1
  • 860
  • 3
  • 11
  • 22

1 Answers1

1

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]);
David
  • 8,449
  • 1
  • 22
  • 32
  • Perfect. Is there any way I can pass a vector instead of having x1, x2? This didn't work: `v=matlabFunction(f,'vars', {t2,xx,cs,ks}); [T,x]= ode15s(@(t2,xx) v(t2,xx,1,1),t,[1 0]);` – student1 Nov 09 '14 at 04:05
  • 1
    No. I don;t think there's any way of doing that in Matlab, due to the way symbolic arrays are treated. But I can't see any reason why it could be a problem not to be able to do so. – David Nov 09 '14 at 04:43