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

25 Ansichten (letzte 30 Tage)
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)

Antworten (1)

Vaibhav
Vaibhav am 25 Sep. 2023
Bearbeitet: Vaibhav am 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

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by