Index in position 4 exceeds array bounds (must not exceed 1).

1 次查看(过去 30 天)
for j = 2:ny-1
for i = 2:nx-1
Ex(i,j,n,1) = 150; %initial guess
Ey(i,j,n,1) = 200; %initial guess
for g = 1:gfinal-1
M11(i,j,n,g) = (1/(4*dt))*(g1+g2+g3)+(eps/dt)*(1+(alpha/kai0)*(3*(Ex(i,j,n+1,g))^2+(Ey(i,j,n+1,g))^2)+S(i,j,n+1));
M22(i,j,n,g) = (1/(4*dt))*(g1+g2+g3)+(eps/dt)*(1+(alpha/kai0)*((Ex(i,j,n+1,g))^2+3*(Ey(i,j,n+1,g))^2)+S(i,j,n+1));
M12(i,j,n,g) = ((2*eps)/dt)*alpha*kai0*Ex(i,j,n+1,g)*Ey(i,j,n+1,g);
M21(i,j,n,g) = ((2*eps)/dt)*alpha*kai0*Ex(i,j,n+1,g)*Ey(i,j,n+1,g);
M(:,:) = ([M11(i,j,n,g),M12(i,j,n,g); M21(i,j,n,g),M22(i,j,n,g)]);
Minv(:,:) = inv([M11(i,j,n,g),M12(i,j,n,g); M21(i,j,n,g),M22(i,j,n,g)]);
Ex(:,:,n+1,g+1) = Ex(:,:,n+1,g)- Minv(1,1)*X(i,j,n,g) - Minv(1,2)*Y(i,j,n,g);
if (abs(X(i,j,n)) <= tol && abs(Y(i,j,n)) <= tol)
break;
end
Ex(i,j,n,g+1) = Ex(i,j,n);
end
end
end
Hello. I am trying to solve newton's method problem.
But there's an error after M11(i,j,n,g) line. 'Index in position 4 exceeds array bounds (must not exceed 1).'
Can you help me with that problem?
Thanks!
  2 个评论
James Tursa
James Tursa 2020-1-23
Do this at the command line:
dbstop if error
Then run your code. When the error appears the code will pause will all variables intact. Examine them to see what their actual dimensions are, and then backtrack in your code to figure out why they are not the size that you expect.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2020-1-23
编辑:Walter Roberson 2020-1-23
You define
Ex(i,j,n,1) = 150; %
but we have no reason to expect that on the line
M11(i,j,n,g) = (1/(4*dt))*(g1+g2+g3)+(eps/dt)*(1+(alpha/kai0)*(3*(Ex(i,j,n+1,g))^2+(Ey(i,j,n+1,g))^2)+S(i,j,n+1));
that
Ex(i,j,n+1,g)
exists -- in particular the n+1 is a problem.
  3 个评论
Walter Roberson
Walter Roberson 2020-1-23
You have the same issue for Ey except worse since you do not assign to Ey inside the loop and so are not growing Ey as you go.
You possibly also have problems accessing S.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by