# Why does the "int" function return different results for two expressions that are mathematically the same in MATLAB R2023a?

조회 수: 6 (최근 30일)
MathWorks Support Team 2024년 2월 23일
편집: MathWorks Support Team 2024년 2월 26일
I am trying to use the "int" function from the Symbolic Math Toolbox to find the integral of two univariate expressions. Here is the code snippet that I am using:
syms x
f = 1/(1-x^2)^(1/2);
g = (1/(1-x^2))^(1/2);
intF = int(f);
intG = int(g);
In the above code snippet, the variables "f" and "g" can be simplified to the same expression mathematically. However, the "int" function returns different results for these two variables:
>> intF
intF =
asin(x)
>> intG
intG =
int((-1/(x^2 - 1))^(1/2), x)
Why does the "int" function return different results for the two expressions that are mathematically the same?

### 채택된 답변

MathWorks Support Team 2024년 3월 14일
Although the variables "f" and "g" can be simplified to the same expression mathematically, the placement of parentheses is causing MATLAB to interpret and simplify these expressions differently. Thus, the "int" function returns different results.
If you run the following code snippet, you can see how MATLAB simplifies these two expressions differently.
syms x
f = 1/(1-x^2)^(1/2);
g = (1/(1-x^2))^(1/2);
simplifiedF = simplify(f)
simplifiedG = simplify(g)
It simplifies "g" to (-1/(x^2 - 1))^(1/2), which is not immediately recognized as the derivative of a standard function. Therefore, when you try to call "int(g)", MATLAB does not simplify it to asin(x) and instead leaves it in an unevaluated integral form.
Here are some potential workarounds:
1) Manually simplify the expressions before passing it to the "int" function. In the code snippet below, the expression is simplified before being passed to the "int" function, and the "int" function can recognize it as the derivative of a standard function.
syms x
g = 1/sqrt(1-x^2);
int(g)
2) Set assumption on the symbolic object by using the "assume" function. In the code snippet below, the "assume" function provides additional information about the domain of the variable "x". It indicates that the variable "x" is within the domain where the function is real and well-defined, and within the principal domain of the asin(x) function.
syms x
assume(x >= -1 & x <= 1)
g = (1/(1-x^2))^(1/2);
g_simplified = simplify(g);
int(g_simplified)
The "fplot" function can be used to graphically explore the domain where a function is real and well-defined. By plotting the function over a range of values, you can visually inspect the graph. Here is a code snippet that uses the "fplot" function to plot the function within the range [-3 3]. The plot shows that the function is well-defined between -1 and 1, you can then use this information to set assumption on the symbolic object.
syms x
g = (1/(1-x^2))^(1/2);
% Use fplot to plot the function
% Adjust the range as needed
fplot(g,[-3, 3])
% Add labels to axes
xlabel('x')
ylabel('g(x)')

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

R2023a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by