Romberg integration method with Matlab

227 vues (au cours des 30 derniers jours)
Fausto Pachecco Martínez
Fausto Pachecco Martínez le 28 Nov 2021
Réponse apportée : Satwik le 25 Sep 2024
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 commentaire
Nidia
Nidia le 20 Nov 2023
You can change the name of the output variable
function r = IntegrationRomberg (expression, a, b, m)

Connectez-vous pour commenter.

Réponses (1)

Satwik
Satwik le 25 Sep 2024
Hi Fausto,
To ensure that only the final result of the Romberg integration is displayed, and not all the intermediate values, you can follow these steps:
1. Remove Intermediate Printing: Eliminate all the 'fprintf' statements inside the loop. These statements are responsible for printing the intermediate values of the Romberg integration table.
2. Return the Result: Adjust the function so that it returns only the final result of the Romberg integration. You can achieve this by including the line:
romberg_value = r(2, m);
3. Use a Separate Function for Display: The result can be displayed using another function. You can create a separate function to handle the calling of the 'IntegrationRomberg' function and the display of its result.
function displayRombergResult(expression, a, b, m)
integralValue = IntegrationRomberg(expression, a, b, m);
fprintf('\nThe integral of %s evaluated from %7.2f to %7.2f is: %7.5f\n\n', expression, a, b, integralValue);
end
This function calls 'IntegrationRomberg' with the required parameters and then prints the final computed integral value.
The incorrect final value was due to the function not correctly returning the refined result from the Romberg integration table. Step 2 addresses this by ensuring the function returns the correct ‘romberg_value’.
Hope this helps!

Catégories

En savoir plus sur Function Creation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by