0

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?

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
mahsa
  • 1
  • 1
    You have no in- or outputs declared to `odo`, why is that? – Adriaan Apr 13 '19 at 14:01
  • What do you mean with "obz, vr and vl are each vectors"? Do you mean like in the code after that that they are functions of t that are given as a function table? Or that they are functions that are given as a formula? – Lutz Lehmann Apr 13 '19 at 21:24

1 Answers1

0

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.

Karls
  • 731
  • 7
  • 17
  • Hi karls.Thanks. I did as you said but now it can't recognize x and t because I guess the way you described it, both t and x will be considered as parameters of the equation and not what needs to be solved.`Undefined function or variable 'x'.` the following info can be used :` t=1:1:12;` `vr=sin(t)`,`vl=cos(t)`;`obz=0.05*sin(t)` b=0.2 – mahsa Apr 14 '19 at 05:33
  • Ok, now it's clear. I edited my answer, now it should run without errors. Good luck! – Karls Apr 14 '19 at 09:36