Hi guys, I am learning on how to use MATLAB and currently im learning about making a user defined function for the Simpson's rule. The following is the code that I am looking at from the textbook "Numerical Methods for Engineers and Scientists":
function [ s ] = test( FunName,a,b)
N=2;
h=(b-a)/N;
x=a:h:b;
y=FunName(x);
MaxTol=1.0;
I1=h/3*(y(1)+4*sum(y(2:2:N))+2*sum(y(3:2:(N-1)))+y(N+1));
N=2*N;
check=0;
while check==0;
h=(b-a)/N;
x=a:h:b;
y=FunName(x);
I2=h/3*(y(1)+4*sum(y(2:2:N))+2*sum(y(3:2:(N-1)))+y(N+1));
CurTol=abs(I1-I2)/I1*100;
if CurTol>MaxTol
check=0;
N=N*2;
I1=I2;
elseif CurTol<=MaxTol
check=1;
I1=I2;
end
end
disp(I1)
end
Now i just want to ask on what 'check' does in this code? Presumably that 'check' here tells MATLAB to continually iterate but I dont understand how it stops it. Thanks in advance

댓글 수: 4

David Hill
David Hill 2021년 1월 9일
check is a flag that continues the while loop until it changes to 1 in the elseif statement.
Faris Sulam
Faris Sulam 2021년 1월 9일
So because the code has set that check has to only run when it equals to 0, when the else if condition is fulfilled, check =1 and that discontinues the iterations?
David Hill
David Hill 2021년 1월 9일
yes
Faris Sulam
Faris Sulam 2021년 1월 9일
Thanks

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

질문:

2021년 1월 9일

댓글:

2021년 1월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by