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

Matlab integration trouble

  1. Feb 28, 2013 #1
    syms y
    int(2*y*(sqrt(1-(y-1)^2)),y,0,2)

    I plug this into matlab and I get the answer is negative pi. The entire integration region is positive. How is this possible!! what the heck is going on????????????????????????????????

    Just noticed that if I set the limits to 0.00001 and 1.99999
    it works perfect. Can someone explain this
     
    Last edited: Feb 28, 2013
  2. jcsd
  3. Mar 1, 2013 #2

    kreil

    User Avatar
    Gold Member

    I couldn't reproduce this. I got pi as the answer below with two different methods (the first is yours):

    via symbolic:
    Code (Text):
    syms y
    int(2*y*(sqrt(1-(y-1)^2)),y,0,2)

    ans =
     
    pi
     
    via function handle:
    Code (Text):
    fcn = @(y) 2.*y.*(sqrt(1-(y-1).^2))
    integral(fcn,0,2)

    ans =

       3.141592653589793
     
  4. Mar 1, 2013 #3
    What version are you using?
     
  5. Mar 1, 2013 #4

    kreil

    User Avatar
    Gold Member

    I work at MathWorks, so I was using 13a (due out in a month or two).

    If you got this answer using 12b, it may have been a bug that got fixed in 13a. Let me know what version you're using and I can check.
     
  6. Mar 8, 2013 #5
  7. Mar 8, 2013 #6

    kreil

    User Avatar
    Gold Member

  8. Mar 10, 2013 #7
    I'm trying to replicate this to modify the problem and numerically calculate an integral, but I get the error:
    "Attempt to execute SCRIPT integral as a function"

    Any ideas what that means?
     
  9. Mar 10, 2013 #8
    At some point you've written a script, and called it: integral. It's probably in your root Matlab directory, or in your active directory. Matlab has a built-in function called integral, which is what the code is meant to execute. However, your version is "covering" it up. A script can't be run as a function, however, hence the error.
     
  10. Mar 10, 2013 #9
    Thanks! That's exactly it. I just created a file to experiment with integrals and called it "integral.m". I never would have thought of that. I was reading the error message as if there were separate "script integrals" and "function integrals" or something. Thanks a lot!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab integration trouble
  1. Matlab solve trouble (Replies: 2)

Loading...