Why is my integral function not working properly in MATLAB?

  • Context: MATLAB 
  • Thread starter Thread starter MotoPayton
  • Start date Start date
  • Tags Tags
    Integration Matlab
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
8 replies · 4K views
MotoPayton
Messages
96
Reaction score
0
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:
on Phys.org
I couldn't reproduce this. I got pi as the answer below with two different methods (the first is yours):

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

ans =
 
pi

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

ans =

   3.141592653589793
 
What version are you using?
 
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.
 
kreil said:
I couldn't reproduce this. I got pi as the answer below with two different methods (the first is yours):

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

ans =
 
pi

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

ans =

   3.141592653589793

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?
 
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.
 
AIR&SPACE said:
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.

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!