MATLAB Why is my integral function not working properly in MATLAB?

  • Thread starter Thread starter MotoPayton
  • Start date Start date
  • Tags Tags
    Integration Matlab
AI Thread Summary
The discussion centers on an issue with MATLAB's integral function returning a negative value for a positive integration region. The user initially receives a negative pi when integrating a specific function, but changing the limits to slightly less than 0 and 2 yields the correct result of pi. It is suggested that the problem may stem from using an older version of MATLAB, specifically 12b, which could have a bug fixed in the upcoming 13a version. Additionally, a user encounters an error due to a script named "integral.m" that conflicts with MATLAB's built-in integral function. The resolution involves renaming the conflicting script to avoid the error.
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:
Physics news 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!
 

Similar threads

Replies
10
Views
3K
Replies
6
Views
2K
Replies
4
Views
1K
Replies
7
Views
2K
Replies
4
Views
5K
Replies
1
Views
2K
Replies
10
Views
4K
Back
Top