# Simpson method on f(x)

3 views (last 30 days)
Lama Hamadeh on 21 Jun 2022
Commented: Torsten on 22 Jun 2022
Hi,
I am trying to code the integration of a simple function using Simpson method. I have managed to get the right solution using very primitive attmept, but I'd like to change that using a loop to generalise the code. Here is my code:
% Defining function
f = @(x) (1/(1+x));
%lower bound (a)
a = 2;
%upper bound (b)
b = 3;
%number of subintervals
n = 4;
%define the step size
h = (b-a)/n;
%define the x values
x = linspace(a,b,n);
%from here downwards, I'd like to construct a loop instead.
%calculate individual integrals
f1 = f(a);
f2 = f(a+h);
f3 = f(a+2*h);
f4 = f(a+3*h);
f5 = f(a+4*h); % the same as f5 = f(b);
%calculate the final integration
ftot = (h/3)*(f1 + 4*f2+ 2*f3 + 4*f4 + f5) %the result is 0.287
Any help would be appreicted. Thanks.
##### 2 CommentsShow 1 older commentHide 1 older comment
Lama Hamadeh on 21 Jun 2022
Thanks for this but I can't read German, I'm afraid! :) and I don't quite get what you mean by variant 1 and 2.
Thanks.

Edward Tomanek on 21 Jun 2022
Edited: Edward Tomanek on 21 Jun 2022
Hi,
My unerstanding is that you want to be able to change the function f at successive iterations of the program, taking a user input for the function. Actually there is quite an easy way to do this in MATLAB. I've just put your program in a perpetual loop. MATLAB takes anonymous functions as user inputs. So when prompted for the function, the user would just need to enter @(x) 1/(1+x) or whatever the function is, and press enter. The program would then execute normally until the next iteration, where you enter the function again. Sometimes a simple problem requires a simple solution! Note that if you wanted to enable the user to specify the upper and lower bounds, as well as the number of subintervals, you would do the same thing, replacing each one with a = input("Please enter the lower bound: ") etc.
while true
% Defining function
f = input("Please enter the function: ");
%lower bound (a)
a = 2;
%upper bound (b)
b = 3;
%number of subintervals
n = 4;
%define the step size
h = (b-a)/n;
%define the x values
x = linspace(a,b,n);
%from here downwards, I'd like to construct a loop instead.
%calculate individual integrals
f1 = f(a);
f2 = f(a+h);
f3 = f(a+2*h);
f4 = f(a+3*h);
f5 = f(a+4*h); % the same as f5 = f(b);
%calculate the final integration
ftot = (h/3)*(f1 + 4*f2+ 2*f3 + 4*f4 + f5) %the result is 0.287
end
I'd also like to add that you could equally well make this into a function as follows, and this may even be better for ease of use:
function ftot = funcc(f,a,b,n)
%define the step size
h = (b-a)/n;
%define the x values
x = linspace(a,b,n);
%from here downwards, I'd like to construct a loop instead.
%calculate individual integrals
f1 = f(a);
f2 = f(a+h);
f3 = f(a+2*h);
f4 = f(a+3*h);
f5 = f(a+4*h); % the same as f5 = f(b);
%calculate the final integration
ftot = (h/3)*(f1 + 4*f2+ 2*f3 + 4*f4 + f5) %the result is 0.287
end
Here you are simply making a function that can be run from the command line in MATLAB by typing ftot = funcc(@(x) 1/(1+x),2,3,4), assuming that you are in the same directory as the function is saved. This would enable the function to be integrated into other scripts that you make without directly calling for certain values to be entered by hand (it's better for automation). In general, it is usually better to make functions for relatively simple tasks like this.
##### 2 CommentsShow 1 older commentHide 1 older comment
Torsten on 22 Jun 2022
Note that n has to be an even number for your method to work.
That's why I referred to the different variants.
Variant 1 is independent of n even or odd.