Error message "Index exceeds matrix dimension"
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Napatarong Wannaphaschaiyong
el 30 de Oct. de 2017
Respondida: Walter Roberson
el 30 de Oct. de 2017
I am trying to solve differential equation using Runge Kutta method but I keep getting message "Index exceeds matrix dimension" on line 16. Please let me know what need to be fixed. Thank you.
dt=0.01;
tmin=0;
tmax=10;
nmin = 1+floor(tmin/dt);
nmax = nmin+ceil(tmax/dt);
Y1(nmin)=0;
Y2(nmin)=1;
Y3(nmin)=1;
t(nmin)=0;
t = ((nmin:nmax)-1)*dt;
Y1prime = inline('Y2*Y3*t','t','Y2','Y3');
Y2prime = inline('-Y1*Y3','Y1','Y3');
Y3prime = inline('-0.51*Y1*Y2','Y1','Y2');
hdt = dt/2;
for n = nmin:(nmax-1)
K1 = Y1prime(t(n),Y2(n),Y3(n));
L1 = Y2prime(Y1(n),Y3(n));
M1 = Y3prime(Y1(n),Y2(n));
K2 = Y1prime(t(n)+hdt,Y2(n)+(hdt*L1),Y3(n)+(hdt*M1));
L2 = Y2prime(Y1(n)+(hdt*K1),Y3(n)+(hdt*M1));
M2 = Y3prime(Y1(n)+(hdt*K1),Y2(n)+(hdt*L1));
K3 = Y1prime(t(n)+hdt,Y2(n)+(hdt*L2),Y3(n)+(hdt*M2));
L3 = Y2prime(Y1(n)+(hdt*K2),Y3(n)+(hdt*M2));
M3 = Y3prime(Y1(n)+(hdt*K2),Y2(n)+(hdt*L2));
K4 = Y1prime(t(n)+hdt,Y2(n)+(hdt*L3),Y3(n)+(hdt*M3));
L4 = Y2prime(Y1(n)+(hdt*K3),Y3(n)+(hdt*M3));
M4 = Y3prime(Y1(n)+(hdt*K3),Y2(n)+(hdt*L3));
end
0 comentarios
Respuestas (2)
ANKUR KUMAR
el 30 de Oct. de 2017
You have defined only one value of Y1, Y2, Y3 and t.
Y1(nmin)=0;
Y2(nmin)=1;
Y3(nmin)=1;
t(nmin)=0;
t = ((nmin:nmax)-1)*dt;
The size of these variables are 1*1. and you are using these variables in every loop. Example:
for n = nmin:(nmax-1)
K1 = Y1prime(t(n),Y2(n),Y3(n));
L1 = Y2prime(Y1(n),Y3(n));
.
.
.
for n=1, this loop runs with no error. When it executes the second time, then K1 wants the value t(2). Y2(2) and Y3(2). But the dimension of these variables are 1*1. Try putting these variables also in the loop or remove the looping of these variables in every loop.
NOTE: This solution has given with the reference of MATLAB, not from the view point of Runge - Kutta Method.
0 comentarios
Walter Roberson
el 30 de Oct. de 2017
You have
t(nmin)=0;
t = ((nmin:nmax)-1)*dt;
The first of those initializes t starting at position nmin. However, the second of those overwrites all of t, leaving it (nmax-nmin+1) entries long starting with index 1.
Then you have
for n = nmin:(nmax-1)
K1 = Y1prime(t(n),Y2(n),Y3(n));
which assumes that t is indexed starting from nmin
You should learn from this pattern:
t_vals = ((nmin:nmax)-1)*dt;
num_tvals = length(t_vals);
Y1 = zeros(1, num_tvals);
Y2 = zeros(1, num_tvals);
Y2(1) = 1;
Y3 = zeros(1, num_tvals);
Y3(1) = 1;
K1 = zeros(1, num_tvals);
K2 = zeros(1, num_tvals);
K3 = zeros(1, num_tvals);
... etc
for t_idx = 1 : num_tvals
t = tvals(t_idx);
y1 = Y1(t_idx);
y2 = Y2(t_idx);
y3 = Y3(t_idx);
K1(t_idx) = Y1prime(t, y2, y3);
L1(t_idx) = Y2prime(y1, y3);
etc
end
However, you have the problem that you are using Y1, Y2, Y3 values past the point that you assigned. You defined those at nmin only, and also never update them.
0 comentarios
Ver también
Categorías
Más información sobre Numerical Integration and Differential Equations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!