using ODE to solve coupled equations

Hi,
I am trying to solve a set of coupled equations but keep getting the error, "Attempted to access x(5); index out of bounds because numel(x)=1."

 采纳的回答

You need to define ‘x’ as a (7x1) vector of initial conditions in your call to your ODE solver.
Also, you’re going to encounter problems with ‘x’ in these lines, because you have defined it as a vector:
dxdt(1,:) = x*(Vs - vC)/L + y*(vj1 - vC)/L; % diL/dt
dxdt(2,:) = x*(-Vs - vj1)/Lp1 + y*(-vj1 - L*(vj1 - vC) - vC)/(L*Lp1); % diLp1/dt
dxdt(3,:) = x*(-Vs - vj2)/Lp2 + y*(-vj2 - L*(vj1 - vC) - vC)/(L*Lp2); % diLp2/dt
I suspect that will throw one of may possible errors about dimensions not agreeing. I have no idea what you’re doing, so I can’t offer a solution.

4 个评论

Josh
Josh 2016-7-1
编辑:Josh 2016-7-1
Thanks for the quick response. I apologize but I am new to using the Matlab ODE solver. I don't think I am following you. Are you saying that I should set x = zeros(7x1), or anything else, in my main script before I call the ODE solver? I do have initial conditions set which I've defined as x0. Here's what I have in my main script.
My pleasure.
The ‘numel(x)=1’ error usually appears because ‘x0’ wasn’t defined correctly, but you have. Looking at your code again, this is where that problem arises:
if (d >= tri) % switch open
x = 0;
y = 1;
else % switch closed
x = 1;
y = 0;
end
I can’t follow what you’re doing, but it might be best to change the name of the scalar version of ‘x’, that you obviously intend for one purpose, so that it does not conflict the the ‘x’ vector the ODE solver is returning. Perhaps call it ‘xs’ (for ‘x switch’) or something that makes sense in the context of your code.
I found the problem. In my code
if true
if (d >= tri) % switch open
x = 0;
y = 1;
else % switch closed
x = 1;
y = 0;
end
I was setting x to 0 or 1 making it now a 1x1. I changed x in the if statement to a and made the corresponding changes in the equations. I don't get that error anymore.
Thanks for the help.

请先登录,再进行评论。

更多回答(0 个)

类别

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by