MATLAB: ODE45 for a system of diff eq with a vector eq

ode45

Hi,
I am trying to solve a 2 equations system of ODE where the first function is a 2×1 vector and the second a scalar. I tried a simple example but I get an error such as: "in an assignment A(I)=B, the number of elements in B and I must be the same"
also before I got an error claiming that the outcome of @rigid is a 2×1 and my input as initial conditions is a 3×1 and thus is wrong. So this means that matlab is not recognizing that my outcome y should be a 3×1. I dont know how to fix this.
My code is:
function dy = rigid(t,y);
rho1=zeros(2,1)) rho1(1,1)=0.5; rho1(2,1)=2; rho0=1; mu1=zeros(2,2); mu1(1,1)=2.2; mu1(1,2)=1.2; mu1(2,1)=0.4; mu1(2,2)=2; mu0=zeros(2,1); mu0(1,1)=1.2; mu0(2,1)=3.2;
dy=zeros(3,1);
dy(1)=rho1-mu1*y(1:2); dy(2)=rho0-mu0'*y(1:2);
[t,y] = ode45(@rigid,[0 12],[0 0 0]);
Any help with this?
thanks in advance.

Best Answer

Barbara, check out
function odetest()
[t,y] = ode45(@rigid,[0 12],[0 0 0]);
plot(t,y)
end
function dy = rigid(t,y)
rho1 = zeros(2,1);
rho1(1,1) = 0.5; rho1(2,1) = 2; rho0 = 1;
mu1 = zeros(2,2); mu1(1,1) = 2.2; mu1(1,2) = 1.2;
mu1(2,1) = 0.4; mu1(2,2) = 2; mu0 = zeros(2,1); mu0(1,1) = 1.2;
mu0(2,1) = 3.2;
dy = zeros(3,1);
dy(1:2) = rho1 - mu1*y(1:2); % Note 1
dy(3) = rho0 - mu0'*y(1:2); % Note 2
  1. The right-hand side is a 2-by-1 vector, which you are assigning to a scalar. Not sure what you need to do, but in my code the vector get's assigned to a 2-by-1, dy(1:2).
  2. The second statement is syntactically correct (scalar to scalar), but since I assigned the 2-by-1 to the first two elements of dy, I assigned the scalar right-hand side to dy(3). Again, only you know what exactly needs to happen.