Simpson method on f(x)
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
Torsten
2022-6-21
Which of the variants do you want to use under
Variant 1 or Variant 2 (restricted to n even) ?
回答(1 个)
Edward Tomanek
2022-6-21
编辑:Edward Tomanek
2022-6-21
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 个评论
Torsten
2022-6-22
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!