Index in position 4 exceeds array bounds (must not exceed 1).
2 次查看(过去 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
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
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
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 Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!