trying to stop my loop with below an error

1 次查看(过去 30 天)
trying to stop my loop when you substract 2 matrix and the error between both is below 0.001
clear
close all
clc
%initialisation
a=0;
b=3;
c=0;
d=3;
h=1;
k=1;
n=(d-c)/k+1;
m=(b-a)/h+1;
u=zeros(n,m);
errmax=0.001;
x=a:h:b;
y= fliplr(c:k:d);
%CL
u(:,1)=0;
u(4,:)=x.^3;
u(:,4)= 27-9*y.^2;
u(1,:)= x.^3 -27*x;
lmax=100;
usup = i-1;
for l=1:lmax
for i=2:m-1
for j=2:n-1
u(i,j)=(1/4)*(u(i-1,j)+u(i,j-1)+u(i+1,j)+u(i,j+1))
end
if max(max(abs(u-usup)))< errmax
break
end
end
end
%end
%plot(u)
%contourf(u)
thanks

采纳的回答

Walter Roberson
Walter Roberson 2021-12-5
u(i,j)=(1/4)*(u(i-1,j)+u(i,j-1)+u(i+1,j)+u(i,j+1))
You always write additional entries into u, never overwriting existing entries.
if max(max(abs(u-usup)))< errmax
You test all of u. But max() over all of u can only ever stay the same or increase because all of the old entries are still there and you are testing all of them.
  3 个评论
Walter Roberson
Walter Roberson 2021-12-5
for j=2:n-1
u(i,j)=(1/4)*(u(i-1,j)+u(i,j-1)+u(i+1,j)+u(i,j+1))
end
That loop writes in into column 2 through to the second last column of u, for the current row.
if max(max(abs(u-usup)))< errmax
If you want to test only the entries you just wrote, then test
if max(max(abs(u(i,2:n-1)-usup)))< errmax
However, I realized that you are doing a Finite Element iteration, and that really you do want to test the entire matrix -- but you want to test it after you have completed the for i as well. Like
for l=1:lmax
for i=2:m-1
for j=2:n-1
u(i,j)=(1/4)*(u(i-1,j)+u(i,j-1)+u(i+1,j)+u(i,j+1))
end
end
if max(max(abs(u-usup)))< errmax
break
end
end
I have to ask about your stopping condition, u - usup . You define
usup = i-1;
and you do that before you have for i, so you are defining usup as a complex constant -1+1i . Then you are comparing abs() of the value minus the complex constant to all of the entries. Does that make sense to do?
u(:,1)=0;
u(4,:)=x.^3;
u(:,4)= 27-9*y.^2;
u(1,:)= x.^3 -27*x;
You initialize column 1 and column 4 of u, and row 1 and row 4 of u. But u is m x n, and you never write into the last row or last column of u unless m and n both happen to be 4... which is true with those particular constants but is not going to be true in general. You should be initializing u(end,:) not u(4,:) and u(:,end) not u(:,4) .
You never write to the outside row or outside column of u, but you are testing all of u when you test abs(u-usup) . But suppose those initial values like 27-9*y.^2 are outside of the range of usup, then the max() comparison over the whole u matrix can never hold.
I do not think you should be comparing all of u to a particular constant -- not unless your situation is something like you having an initial configuration that you are allowing to dampen out.
What might make sense instead is to keep an "old u" and an "updated u" and to compare the two of them, and keep iterating if any location has too large of a difference.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by