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
Click For Summary

Discussion Overview

The discussion revolves around issues encountered while performing integration in MATLAB, specifically regarding the output of an integral function that yields unexpected results. Participants explore the behavior of the integral function under different conditions and versions of MATLAB.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant reports that the integral of the function 2*y*(sqrt(1-(y-1)^2)) from 0 to 2 returns a negative value, which seems incorrect given the positive integration region.
  • Another participant successfully computes the same integral using two methods, both yielding a positive result of pi.
  • A question is raised about the version of MATLAB being used, suggesting that discrepancies may arise from different software versions.
  • A participant mentions working at MathWorks and indicates that a potential bug in an earlier version (12b) may have been fixed in the upcoming version (13a).
  • One participant encounters an error when trying to execute the integral function, which they attribute to having named their own script "integral," thus overshadowing MATLAB's built-in function.
  • Another participant confirms this issue and expresses gratitude for the clarification regarding the script name conflict.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the original issue with the integral function, as some are able to reproduce the expected result while others are not. The discussion includes multiple viewpoints regarding the potential causes of the discrepancies.

Contextual Notes

Limitations include the dependence on specific MATLAB versions and the potential for user-defined scripts to interfere with built-in functions, which may not be universally applicable to all users.

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
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K