Index exceeds matrix dimensions.
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (2)
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
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.
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
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.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!