Romberg integration method with Matlab

172 views (last 30 days)
Fausto Pachecco Martínez
Fausto Pachecco Martínez on 28 Nov 2021
Commented: Nidia on 20 Nov 2023
I have the following code that uses the Romberg method to give the result of a definite integral, but I only want it to display the last result and not all the values of the table, how can I do this using another function to call Romberg's function?
function romberg_value = IntegrationRomberg (expression, a, b, m)
% This function evaluates an integral by the Romberg method
% Receives as parameter the variables expression, a, b, m
% Expression => function to evacuate
% a = lower limit of the interval
% b = upper limit of the interval
% m = number of subintervals
% Convert expression to a function
f = inline (expression);
h = b-a;
r = zeros (2, m + 1);
r (1,1) = (f (a) + f (b)) / 2 * h;
fprintf('\nRomberg integration table:\n')
fprintf ('\n %7.2f \n\n', r (1,1));
for i = 2: m
romberg_value = 0;
for k = 1: 2 ^ (i-2)
romberg_value = romberg_value + f (a + (k-0.5) * h);
end
r(2,1) = (r(1,1) + h * romberg_value) / 2;
for j = 2: i % Apparently this "for" displays the other values of the table, if you comment it,
l = 2 ^ (2 * (j-1)); % only the first column of values appears and the others are only zeros.
r (2, j) = r (2, j-1) + (r (2, j-1) -r (1, j-1)) / (l-1);
end
for k = 1: i
fprintf ('% 7.5f', r (2, k));
end
fprintf ('\n\n');
h = h / 2;
for j = 1: i
r (1, j) = r (2, j);
end
end
Here is the program where you can call the code above introducing the values of your variables:
% This program asks the user for the data necessary for a numerical integration
%Clear all
clc %Clean command window
clear all %Clears or removes variables from main memory
close all %Close all graphics windows
%Ask the user for the expression of the function
expression = input ('Enter the expression of the function:', 's'); %Read from keyboard converting to string
%Ask the user for the values of a, b and m
a = input ('Enter the value of the lower limit a: ');
b = input ('Enter the value of the upper limit b: ');
m = input ('Enter the number of intervals m: ');
%Calculate the integral
disp ('%%%%%% Romberg method %%%%%%%%%%')
integralValue = IntegrationRomberg (expression, a, b, m);
%Show result on screen
fprintf ('\n The integral of %s evaluated from %7.2f to %7.2f is: %7.5f \n\n', expression, a, b, integeralValue')
Oh and there is also another error at this code, and it's that when the fprintf does the printing, the value given is wrong.
It should be 41.66667, not 136533.33321.
The values I used to get this answers were:
expression = x^2
a = 0
b = 5
m = 16
  1 Comment
Nidia
Nidia on 20 Nov 2023
You can change the name of the output variable
function r = IntegrationRomberg (expression, a, b, m)

Sign in to comment.

Answers (0)

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!