How to loop intermidpoint statement until the absolute error falls within the tolerance of?

2 views (last 30 days)
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-->

Answers (1)

Geoff Hayes
Geoff Hayes on 10 Oct 2015
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 Comments
Tyw7
Tyw7 on 11 Oct 2015
Edited: Tyw7 on 11 Oct 2015
Edit:
reverting back to old code. It doesn't seem to print the final statement that says that data is within tolerance.
function [integral] = intmidpoint(N)
clear
clc
%INTMIDPOINT numerical integration using the composite midpoint rule
clc
%define the integration limits
a=0;
b=2;
%this is the aim of absolute error required
aim = 0.001;
%define the number of sub-intervals to be used
if nargin == 1
n=N;
else
n=1;
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
abs_err = realmax;
while abs_err>aim;
h=(b-a)/n;
integral=0.0;
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;
abs_err=abs(4-integral);
end;
fprintf('2 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);
n = n + 1;
if abs_err>aim;
continue;
fprintf('test 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);
end;
end;
end
%subfunction defines the integrand
function [f_value] = f(x)
f_value = (x)^3;
end
Geoff Hayes
Geoff Hayes on 12 Oct 2015
Edited: Geoff Hayes on 12 Oct 2015
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.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!