Clear Filters
Clear Filters

How to Make Simpsons Rule UDF

3 views (last 30 days)
Aren Arslanyan
Aren Arslanyan on 4 Oct 2020
Answered: Mohith Kulkarni on 7 Oct 2020
function output = Simpsons(f,a,b,h)
% f - funtion
% a - This is the initial x value
% b - This is the final x value
% h - This is the step-size
x=[a:h:b]; %need to create a vector of n+1 evenly spaced points
I made this simpsons rule UDF and it works can someone just explain wihat the last line reads? I don't fully understand it. Also please let me know if this is a valid UDF for Simpsons if you do not see any errors.

Answers (1)

Mohith Kulkarni
Mohith Kulkarni on 7 Oct 2020
It is a valid simpsons rule implementation. Let me break the line down.
This expression above is equivalent to (Δx/3)*[f(x0)+4f(x1)+2f(x2)+4f(x3)+2f(x4)+⋯+4f(xn−1)+f(xn)].
x(3:2:end-2) %j:i:k creates a regularly-spaced vector using i as the increment between elements.
So here indexing into the elements of x at odd places. We left out the end as f(xn) is multiplied with 1 and not 2. Similarly, access elements of x at even positions using
Since we are passing a vector to f, the ouput is a vector and sum is used to find the sum of the output vector.


Find more on Images 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!