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

4 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 14 Mar 2024
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)')

Più risposte (0)

Categorie

Scopri di più su Symbolic Math Toolbox in Help Center e File Exchange

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by