Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Interpreting Matlab function simp

  1. Feb 27, 2013 #1
    Hello, guys

    Hey guys,

    Got stuck with function integration using Simpson's rule and need your help.

    Please first refer to picture attached for full idea of my question:

    The Matlab command related to it is:

    for i=1:nr




    I1 is



    Does it mean that I1 is integrated 21 times between boundaries 0 and r0?

    Attached Files:

    • 2.jpg
      File size:
      36.9 KB
  2. jcsd
  3. Feb 27, 2013 #2


    User Avatar
    Gold Member

    Can you post more information? The code you posted references variables that you never define. Make it so that your code block can be copy/pasted into matlab.
  4. Feb 27, 2013 #3
    Yes. Sure

    I have attached all commands with order from Pic.1 to Pic.3

    Attached Files:

  5. Feb 27, 2013 #4


    User Avatar
    Gold Member

    simp() is not a matlab function, so the information about the input arguments is not available in the documentation. I suggest looking at the function file for simp() to find this info.
  6. Feb 27, 2013 #5


    User Avatar
    Gold Member

    EDIT: I found information about this function in the MATLAB file exchange.

    Code (Text):
    function s = simp(f, a, b, h)
    x1 = a + 2 * h : 2 * h : b - 2 * h;
    sum1 = sum(feval(f, x1));
    x2 = a + h : 2 * h : b - h;
    sum2 = sum(feval(f, x2));
    s = h / 3 * (feval(f, a) + feval(f, b) + ...
               2 * sum1 + 4 * sum2);
    It appears that the inputs are:

    f=function, a=initial value, b=end value, h=interval size
    Last edited: Feb 27, 2013
  7. Feb 27, 2013 #6
    I have found it as separate m.file. Here are the commands:

    function uint=simp(xl,xu,n,u)
    for i=3:2:n
    But why here different variables are used such as xl and xu?

    It seems to me that we use r in integration?
  8. Feb 27, 2013 #7


    User Avatar
    Gold Member

    At a glance it looks like

    xl = beginning of interval
    xu = end of interval
    n = number of slices
    u = function

    So in your case of
    Code (Text):
    You are integrating u1d from 0 to r0 with nr intervals
  9. Feb 28, 2013 #8
    So you mean simp can be uniformly used for every type of variable assuming their correct order?
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook