Index exceeds matrix dimensions.

5 Ansichten (letzte 30 Tage)
Nouhayla EL GHANI
Nouhayla EL GHANI am 20 Aug. 2015
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

Antworten (2)

Guillaume
Guillaume am 20 Aug. 2015
It sounds like y is just a scalar. For your function to work y must have at least 7 elements.
  2 Kommentare
Nouhayla EL GHANI
Nouhayla EL GHANI am 20 Aug. 2015
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);
Adam
Adam am 20 Aug. 2015
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.

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 20 Aug. 2015
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 Kommentare
Torsten
Torsten am 21 Aug. 2015
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.
Nouhayla EL GHANI
Nouhayla EL GHANI am 21 Aug. 2015
Thnks a lot :)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by