Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8764787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:07:12+00:00 2026-06-13T16:07:12+00:00

I have tried, just for the fun of it, to write a MatLab-code for

  • 0

I have tried, just for the fun of it, to write a MatLab-code for the composite Simpson’s rule. As far as I can see, the code is correct, but my answers are not as accurate as I would like. If I try my code on the function f = cos(x) + e^(x^2), with a = 0, b = 1 and n = 7, my answer is roughly 1,9, when it should be 2,3. If I use the algorithm available at Wikipedia, I get a very close approximation with n = 7, so my code is obviously not good enough. If someone can see any mistakes in my code, I would really appreciate it!

function x = compsimp(a,b,n,f)
% The function implements the composite Simpson's rule

h = (b-a)/n;
x = zeros(1,n+1);
x(1) = a;
x(n+1) = b;
p = 0;
q = 0;

% Define the x-vector
for i = 2:n
    x(i) = a + (i-1)*h;
end

% Define the terms to be multiplied by 4
for i = 2:((n+1)/2)
    p = p + (f(x(2*i -2)));
end

% Define the terms to be multiplied by 2
for i = 2:((n-1)/2)
    q = q + (f(x(2*i -1)));
end

% Calculate final output
x = (h/3)*(f(a) + 2*q + 4*p + f(b));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:07:13+00:00Added an answer on June 13, 2026 at 4:07 pm

    Your interval [a,b] should be split into n intervals. This results in n+1 values for x that form the boundary of each partition. Your vector x contains only n elements. It appears that your code is only dealing with n terms instead of n+1 as required.

    EDIT:: Now you have modified the question based on the above, try this

    % The 2 factor terms
    for i = 2:(((n+1)/2) - 1 )
        q = q + (f(x(2*i)));
    end
    
    % The 4 factor terms
    for i = 2:((n+1)/2)
       p = p + (f(x(2*i -1)));
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried every way I can think of with this, but just not
I have just tried using the NSArchiver but for some reason I am getting
I have just tried to uploaded an app through the Application Loader, but it
I have tried using a stemmer but the words it produces are just not
this is just some pretty standard code I have tried. What I am trying
Iam running WAMP server and just enabled php_memcache extension and have tried the code
I have tried just about everything for getting the Kinect for Windows to actually
I have just tried adding a WebService (asmx based) from a WebForms project to
Update I have just tried Pinning my site to the Taskbar again (after removing
I have tried troubleshooting this problem and just ended up with nothing, I hope

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.