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

A problem in Matlab ,code doesn't work

  1. Dec 17, 2009 #1
    Hi guys ,

    I need to write a code in order to calculate Simpson's Rule.
    Here is my code , what's wrong with it ?

    PHP:
    function [integral]=simp(f,a,b,n)
       if  mod(n,2)~=0
           warning('n must be even!')
       else
        h=(b-a)/n;
        integral=0;
        for i=1:1:n-1
            x=a+i*h;
            if (mod(i,2)==0)
                integral=integral+2*eval(f);
            else
                integral=integral+4*eval(f);
            end
        end
        x=a;
        integral=integral+eval(f);
        x=a+n*h;
        integral=integral+eval(f);
        integral=(h/3)*integral;
      end    
    Here is the link to the actual formula
    "[URL [Broken]


    Thanks a lot :)
     
    Last edited by a moderator: May 4, 2017
  2. jcsd
  3. Dec 17, 2009 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    What is "eval(f)" supposed to do? What does the Matlab documentation have to say about the function eval?
     
  4. Dec 17, 2009 #3
    for example , when I type in Matlab the following input :

    simp('sin',0,pi/3,40)+simp('cos',0,pi/3,40)

    sin suppose to be used , but it doesn't .

    It says : "??? Error using ==> sin
    Not enough input arguments. "

    "Error in ==> integral=integral+4*eval(f); "
     
  5. Dec 17, 2009 #4

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Read the documentation. What does eval do?
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook