Remove exponentials with positive argument in symbolic MATLAB

4 visualizaciones (últimos 30 días)
Hello!
I am using symbolic MATLAB to obtain an expression of the form:
Where are some complicated expresions of other variables (not known beforehand). To avoid problems with numeric evaluation, I would like to express the expression in the form:
Is there a way to easily implement that on MATLAB?
Thanks!

Respuestas (3)

Vandit
Vandit el 24 de Jul. de 2024
Hello Gustavo,
You can simplify the given expression into the desired form in MATLAB using symbolic computation. Here's how you can do it:
% Define the original expression
expr = (A*exp(x) - B*exp(-x)) / (C*exp(x) + D*exp(-x));
% Multiply numerator and denominator by exp(-x)
numerator = (A*exp(x) - B*exp(-x)) * exp(-x);
denominator = (C*exp(x) + D*exp(-x)) * exp(-x);
% Simplify the transformed numerator and denominator
numerator_simplified = simplify(numerator);
denominator_simplified = simplify(denominator);
% Form the new expression
expr_transformed = numerator_simplified / denominator_simplified;
% Display the transformed expression
disp(expr_transformed);
The above code snippet multiply the numerator and the denominator by (e^{-x}) to transform the expression and then the "simplify" function is used to simplify both the numerator and the denominator after the multiplication.
To know more about "simplify" function, please refer to the documentation below:
Hope this helps.
  1 comentario
Gustavo Chávez Ponce de León
Gustavo Chávez Ponce de León el 24 de Jul. de 2024
Hello! Thank you for your answer.
There is an small issue with this proposal: to define numerator and denominator, I would need to know A, B, C, D and x. My code calculates expr after multiple mathematical operations. I know it has the form that I explained above, but I don't know A,B,C,D or x beforheand. Thus, I cannot simply define numerator and denominator. Is there a way of extracting them from expr?

Iniciar sesión para comentar.


Dheeraj
Dheeraj el 24 de Jul. de 2024
Editada: Dheeraj el 24 de Jul. de 2024
Hi Gustavo Chávez Ponce de León,
I understand you want to create Symbolic expressions using MATLAB's Symbolic Math Toolbox.
To create an expression you have to create variables. This can be achieved using either "syms" or "sym" functions.
The below code demonstrates the implemention of this expression using Symbolic Math Toolbox.
% Here I've assumed that A B C D x are the symbols but you could modify
% them according to the expression.
syms A B C D x
% Define the simplified expression
f = (A - B * exp(-2*x)) / (C + D * exp(-2*x));
% Display the simplified expression
disp(f)
You could refer to the below MATLAB's documentation to know more about creating variables and functions using Symbolic Math Toolbox.
Thank you.

John D'Errico
John D'Errico el 24 de Jul. de 2024
Very often symbolic computations are less easy than you want them to be, because the computer does not see one thing as simple versus another. It really does not care about any concepts of simplicity. Beauty and elegance are nothing to a computer.
syms A B C D x
expr = (A*exp(x) + B*exp(-x))/(C*exp(x) + D*exp(-x))
expr = 
Now, what can you do? You can extract the numerator and denominator. Use numden.
[N,D] = numden(expr)
N = 
D = 
However, note that when I extracted the numerator and denominator, it actually gave me a different pair of expressions than I may have expected.
Even more amusing is that it may decide to do a simplify on the final result, and give you something you did not want. It can be frustrating.
expr2 = (N/exp(2*x))/(D/exp(2*x))
expr2 = 
Yep. It can be stubborn. And it seems to prefer positive exponents. We can trick it a bit though, by doing this:
expr3 = simplify(N/exp(2*x))/simplify(D/exp(2*x))
expr3 = 
Computers can sometimes be both amazingly smart and amazingly dumb.
  2 comentarios
Gustavo Chávez Ponce de León
Gustavo Chávez Ponce de León el 24 de Jul. de 2024
Hello! Thank you for your answer.
This seems to be going in the right direction. However, I don't know beforhand what is the value of x. Thus, I cannot define expression expr2. Is there a way of extracting x from either N or D?
John D'Errico
John D'Errico el 24 de Jul. de 2024
Um, no. You cannot tell the symbolic toolbox to only use negative powers in exp. So, without looking at the expression there, and then knowing you need to divide by exp(2*x), you cannot hope the toolbox will know what you want.

Iniciar sesión para comentar.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by