Effacer les filtres
Effacer les filtres

How to stop internal simplification on symbolic expression? (Examples in Description)

13 vues (au cours des 30 derniers jours)
Alex W
Alex W le 31 Mar 2022
Modifié(e) : Vaibhav le 25 Sep 2023
Hey awesone friends, say in Live Editor, how to have an expression like below to be printed as exactly typed? Right now, by default, it auto "simplifies" to 1/((a+1)^n), which is not what I wanted. Wondering anyway to stop it.
syms a n
expr = (a+1)^(-n)

Réponses (1)

Vaibhav
Vaibhav le 25 Sep 2023
Modifié(e) : Vaibhav le 25 Sep 2023
Hi Alex,
I understand that you want to print the expression exactly as it's typed in the Live Editor without it being automatically simplified. To achieve this, you can use the following workaround:
1. Define the expression as a string from the beginning, enclosing it in single quotes.
2. Use the `disp` function to display the expression in the Live Editor exactly as it is typed.
3. If you need to evaluate this expression later, you can convert it back to a symbolic expression using the `str2sym()` function.
Here's the code snippet for reference:
% Declare symbolic variables 'a' and 'n'
syms a n
% Define the expression as a string, enclosed in single quotes
expr = '(a+1)^(-n)';
% Display the expression exactly as it is typed
disp(expr);
% Convert the string back to a symbolic expression using str2sym for
% further calculations
ans = str2sym(expr);
This approach ensures that the expression appears exactly as you have typed it in the Live Editor, preserving its form.
Please refer to the following documentations for more information:
Hope this helps!
Thanks,
Vaibhav

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by