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

13 次查看(过去 30 天)
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)

回答(1 个)

Vaibhav
Vaibhav 2023-9-25
编辑:Vaibhav 2023-9-25
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

类别

Help CenterFile Exchange 中查找有关 Symbolic Variables, Expressions, Functions, and Preferences 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by