How to loop intermidpoint statement until the absolute error falls within the tolerance of?
조회 수: 4 (최근 30일)
이전 댓글 표시
This is my code. I am trying to loop, increasing "nargin" until the error falls within the tolerance of 0.001. But my code is broken.
<!-- start of code-->
function [integral] = intmidpoint2(N)
%INTMIDPOINT numerical integration using the composite midpoint rule
n=1
%this is the aim of absolute error required
aim = 1e-4;
%define the integration limits
a=0;
b=2;
%define the number of sub-intervals to be used
if nargin == 1
n=N;
else
n=45;
end
h=(b-a)/n;
%approximate integral using the composite mid-point rule
integral=0.0;
%add the contribution of each sub-interval in turn
for i=1:n
x_left = a+(i-1)*h;
x_right = a+i*h;
x_half = (x_left+x_right)/2.0;
f_half = f(x_half);
integral = integral+h*f_half;
end
abs_err=abs(4-integral);
while aim < abs_err
goto 2
if abs_err>aim
fprintf('Midpoint approximation to integral using %g subintervals is %g. \n Data outside of tolerance. Abosolute error is %g, which is greater than 0.001. Increase the number of steps. \n',n,integral,abs_err);
else if abs_err<aim
fprintf('Midpoint approximation to integral using %g subintervals is %g. \n Data within tolerance. Absolute error is %g, which is less than 0.001. \n',n,integral,abs_err);
n=n+1;
break
end
end
end
end
%subfunction defines the integrand
function [f_value] = f(x)
f_value = (x)^3;
end
<!--End of code-->
댓글 수: 0
답변 (1개)
Geoff Hayes
2015년 10월 10일
Tyw7 - I notice that there is the following couple of lines of code
while aim < abs_err
goto 2
What is the intent behind this line? There is no goto statement in MATLAB so if you are trying to repeat the block of code that precedes this, you will have to find an alternative.
If you want to change the number of subintervals n until the error falls within the desired tolerance, then you could do something like the following. First, set n as you have done already
%this is the aim of absolute error required
aim = 1e-4;
%define the integration limits
a=0;
b=2;
%define the number of sub-intervals to be used
if nargin == 1
n=N;
else
n=45;
end
Now set up the while loop that will continue approximating the definite integral until the condition has been satisfied
abs_error = realmax;
while abs_error > aim
h=(b-a)/n;
integral=0.0;
for k=1:n
% etc.
end
abs_err=abs(4-integral);
n = n + 1;
end
So on the first iteration of the while loop, we estimate the integral given the chosen n. We calculate the abs_err and increment n so that on subsequent iterations of the loop (if necessary) we can increase the number of subintervals in our interval.
Note that a problem with the above code is that what happens if we never converge to aim. Then we get stuck in the loop and never exit. When using while loops in this manner, it is good practice to include a maximum allowed iterations count so that eventually the loop will terminate when this count has been exceeded.
댓글 수: 3
Geoff Hayes
2015년 10월 12일
편집: Geoff Hayes
2015년 10월 12일
To put a maximum number of iterations for your while loop to execute, you could do something like
maxIterations = 100;
atIter = 0;
while abs_err>aim && atIter <= maxIterations
atIter = atIter + 1;
% etc.
end
I'm not clear on what your question as to whether abs_error is a valid MATLAB command. In the above code, abs_error is a local variable.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!