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

Matlab function

  1. Mar 26, 2008 #1
    1. The problem statement, all variables and given/known data

    Write an algorithm and Matlab function JN, which uses the Composite Trapez-
    ium Rule (CTR) to compute an approximation of the integral f(x) dx for an arbitrary
    function f of one variable. The inputs should be a, b and N (the number of
    subintervals), and f (the name of a Matlab function of one variable). The
    output should be the approximate integral value JNf, i.e. the result of the
    Composite Trapezium Rule for f.

    2. Relevant equations

    3. The attempt at a solution

    function JN=CTR(a,b,N)
    for i=1:N

    Most of it looks right but i'm sure i have done something wrong. is there something missing with defining f.
    Thank you
  2. jcsd
  3. Mar 26, 2008 #2
    Did you define f(x) somewhere? What's the point of f=f(ti) if you end up calling the function again anyway? The idea looks ok, it's just that you don't have all the specifics for MATLAB to me.
  4. Mar 27, 2008 #3
    Oh i think i get it now. no worries.
    i have another question:
    i have a function; like the one above, how do i apply it in another m file. i mean i want to apply this to the function f=3x+2 using feval command. this is what i did:
    but if i type in the command: CTR(0,0.9,20); an error comes up.
    How do we use the feval command?
    Thank you.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook