mahsa
mahsa

Reputation: 1

Time varying input ODE solving

I'm trying to solve a set of ODE equations using ode45. A number of my parameters are already a function of time but I keep getting errors.

function odo
dx(1,1) = (vl+vr)/2*cos(x(3));
dx(2,1) = (vl+vr)/2*sin(x(3));
dx(3,1) = obz

where obz, vr and vl are each vectors e.g:

obz = sin(t),    t = 0:dt:tf;

I use the following syntax:

[t, x1] = ode45(@(t,x) odo(t,x,b,obz,vr,vl), 0:dt:tf, [0;0;0]);

with R15 but keep getting the error:

Assignment has more non-singleton rhs dimensions than non-singleton subscripts

How to solve this issue?

Upvotes: 0

Views: 1111

Answers (1)

Karls
Karls

Reputation: 751

You have some syntax errors here. First You can't just type function odo and let the MATLAB guess what it should do here. The second, you call t and x twice in your solver expressions. Be consistent, call function in ode solver this way:

t_span=1:1:12;
b=0.2;
[t, x] = ode45(@odo, t_span, [0;0;0], b);

You don't need to pass t and x manually, solver do it itself. Only the additional parameter b (however it is not in use, in your example). Also in declaration of function do as follow:

function dx = odo(t,x,b)

vr=sin(t); %Non-OD equations here
vl=cos(t);
obz=0.05*sin(t);

n=numel(x); %this generate a holder for results
dx=zeros(n,1);

dx(1) = (vl+vr)/2*cos(x(3)); %All ODEs formatted like this
dx(2) = (vl+vr)/2*sin(x(3));
dx(3) = obz;

As you can see, vr, vl, and obz are time-depended values, so need to be re-calculated in every step of solver. You have to place it in your solver function.

Upvotes: 0

Related Questions