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

23 ビュー (過去 30 日間)
Alex W
Alex W 2022 年 3 月 31 日
編集済み: Vaibhav 2023 年 9 月 25 日
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

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by