Why is my integral function not working properly in MATLAB?

In summary: 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!
  • #1
MotoPayton
96
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
  • #2
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
 
  • #3
What version are you using?
 
  • #4
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.
 
  • #7
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?
 
  • #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.
 
  • #9
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!
 

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

What is Matlab integration?

Matlab integration is the process of incorporating Matlab code or functions into another software or programming language. This allows for the use of Matlab's advanced mathematical and scientific computing capabilities in a different environment.

Why am I having trouble integrating Matlab?

There could be several reasons for difficulties with Matlab integration. Some common issues include incompatible versions, errors in the code, or missing dependencies. It's important to carefully check all of these factors when troubleshooting integration problems.

How can I ensure compatibility when integrating Matlab?

To ensure compatibility, it's important to use the same version of Matlab in both environments. Additionally, checking and updating any necessary dependencies or toolboxes can help to avoid compatibility issues.

What are some tips for successfully integrating Matlab?

Some tips for successful Matlab integration include carefully reviewing the documentation for both the software and the specific functions being used, testing the code in a controlled environment before full integration, and seeking assistance from experienced users or online forums.

Is there a limit to the amount of Matlab code that can be integrated?

There is no specific limit to the amount of Matlab code that can be integrated, but it's important to consider the processing power and memory capabilities of the environment in which the integration is taking place. Large amounts of code may require more resources and could potentially cause performance issues.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
594
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
1K
Back
Top