Matlab Index out of bounds

560 Views Asked by At

I'm trying to call a function using ode45 from another script file and I am passing in an matrix A. Matlab throws an error and says it can't access the matrix because it is empty. So here's the function code:

function out = test(t, E, A)

global N1 N2 N3;

out = zeros(1,1);

out(1) = N1*(A(1,1)*N1 + A(1,2)*N2 + A(1,3)*N3 + E(1));

And here's the script file:

global N1 N2 N3;

N1 = 10;
N2 = 10;
N3 = 10;

E = [1; 1; 1;];
A = [[0,-1,-1];[1,-1,-0.5];[1,0,-1]];

[t, L] = ode45('test', [0:.01:5], E, A);

When I run this Matlab throws the following error:

Attempted to access A(1,1); index out of bounds because size(A)=[0,0].

Error in ppsystem (line 7)
out(1) = N1*(A(1,1)*N1 + A(1,2)*N2 + A(1,3)*N3 + E(1));

Error in odearguments (line 88)
f0 = feval(ode,t0,y0,args{:});   % ODE15I sets args{1} to yp0.

Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...

Error in ppsystem_script (line 11)
[t, L] = ode45('test', [0:.01:5], E, A);

I am so confused on why I'm getting this error, any ideas?

1

There are 1 best solutions below

0
On BEST ANSWER

Does this work for a modified code?

function out = test(t, E)

    A = [0 -1 -1;1 -1 -0.5;1 0 -1];
    global N1 N2 N3;

    o1 = N1*(A(1,1)*N1 + A(1,2)*N2 + A(1,3)*N3 + E(1));
    o2 = N1*(A(2,1)*N1 + A(2,2)*N2 + A(2,3)*N3 + E(2));
    o3 = N1*(A(3,1)*N1 + A(3,2)*N2 + A(3,3)*N3 + E(3));
    out = [o1;o2;o3];
end

and the script:

clear all;

global N1 N2 N3;

N1 = 10;
N2 = 10;
N3 = 10;

E = [1;1;1];

[t, L] = ode45('test', [0:.01:5], E);