Index exceeds matrix dimensions.

Hello,
I am working in a programme to solve cauchy's problem. But I had this error "Index exceeds matrix dimensions." in pb_cauchy (line 3) g1=y(2)
The function pb_cauchy:
function g=pb_cauchy(y,A1,A2,B1,B2)
%fonction representing X'=AxX
g1=y(2);
g2=y(3);
g3=A2.*y(2)+A1.*y(4);
g4=y(5);
g5=y(6);
g6=y(7);
g7=B2.*y(2)-B1.*y(4);
g=[g1;g2;g3;g4;g5;g6;g7];
end
Thnks

回答(2 个)

Guillaume
Guillaume 2015-8-20

0 个投票

It sounds like y is just a scalar. For your function to work y must have at least 7 elements.

2 个评论

Yes, I saw that but I don't know how to change it. I call pb_cauchy in the function main by the syntax:
y0=[y0(1) y0(2) y0(3) y0(4) y0(5) y0(6) y0(7)];
[x,ysol]=ode45('pb_cauchy',x,y0);
y0=[y0(1) y0(2) y0(3) y0(4) y0(5) y0(6) y0(7)];
either does nothing at all if y0 already has just 7 elements or could be replaced with simply putting
y0(1:7)
into the od45 function call.
That won't solve the problem you are having though, just make your code a little less redundant.
The bug you are getting would probably be found trivially using the debugger with a breakpoint in your pb_cauchy function.

请先登录,再进行评论。

A1 = randn() * 5; A2 = randn() * 15; %I had to invent _some_ value for them
B1 = randn() * 7; B2 = randn() * 21;
y0 = rand(1,7); %appropriate non-zero initial condition
[x, ysol] = ode45(@(t,y) pb_cauchy(t,y,A1,A2,B1,B2),y0);
with
function g=pb_cauchy(t, y, A1, A2, B1, B2)
%function representing X'=AxX
g1=y(2);
g2=y(3);
g3=A2.*y(2)+A1.*y(4);
g4=y(5);
g5=y(6);
g6=y(7);
g7=B2.*y(2)-B1.*y(4);
g=[g1;g2;g3;g4;g5;g6;g7];
end
Notice the "t" before the "y".

3 个评论

Thank you. When I added u re modification i get this errors
Error using odearguments (line 19)
When the first argument to ode45 is a function handle, the tspan
and y0 arguments must be supplied.
Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs,
odeFcn, ...
Error in main_NR (line 82)
[x,ysol]=ode45(@(t,y)pb_cauchy(t,y,A1,A2,B1,B2),y0);
Then just do what the error message says:
A1 = randn() * 5;
A2 = randn() * 15; %I had to invent _some_ value for them
B1 = randn() * 7;
B2 = randn() * 21;
y0 = rand(1,7); %appropriate non-zero initial condition
tspan = [0 1];
[x,ysol] = ode45(@(t,y) pb_cauchy(t,y,A1,A2,B1,B2),tspan,y0);
with
function g=pb_cauchy(t, y, A1, A2, B1, B2)
%function representing X'=AxX
g1=y(2);
g2=y(3);
g3=A2.*y(2)+A1.*y(4);
g4=y(5);
g5=y(6);
g6=y(7);
g7=B2.*y(2)-B1.*y(4);
g=[g1;g2;g3;g4;g5;g6;g7];
end
Best wishes
Torsten.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by