Is there a way to display variables followed by numerical solution in the Command Window?
1 次查看(过去 30 天)
显示 更早的评论
Apologies in advance if this has been covered by other posts, or is very simple problem; I am very new to Matlab but have not been able to find any topics that speak on this fully. In my editior, I'm trying to have variables displayed as a formula before solving for the numerical value in the Command Window when I run my script.
Example:
a = 10
b = 5
k = a + b
where after running I would like the line containing 'k' in the Command Windows to show "k = a + b = 15" or anything similar to that. Right now it simply displays:
"k =
15"
This would allow for debugging and to follow along with large complex formulas in my work moving forward rather than simply a numerical value. It would be fine if it even needed to be done in two lines where I have a line that displays 'k' symbolically, then another line that displays 'k' as a numical solution. I've played around with 'syms a b' but am not sure that's the right approach and cannot get a numerical solution later on. Any help or direction that could point me achieving something simliar to this would be very appreciated.
0 个评论
回答(1 个)
Avni Agrawal
2025-2-27
Hi Sam,
To compute values from expressions, consider using a MATLAB script or live script. This method simplifies problem formulation and eases debugging and maintenance. If you prefer working in the Command Window, create separate variables for symbolic expressions and numeric values to verify expressions easily.
Please take a look at this example:
syms a b
a_value = 10;
b_value = 5;
k_expression = "a + b";
k = a_value + b_value;
displayFormula(k_expression)
For more on symbolic expressions, please refer to this documentation: https://www.mathworks.com/help/symbolic/create-symbolic-numbers-variables-and-expressions.html
I hope this helps!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!