Is there a way to display variables followed by numerical solution in the Command Window?
11 次查看(过去 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 个)
Umang Pandey
2024-10-29,9:27
Hi Sam,
If your goal is to compute values based on certain expressions, I recommend using a MATLAB script or live script. This approach can simplify formulating your problem and make tasks like debugging, maintaining, and modifying your code more straightforward.
However, if you have a specific use case for working directly in the Command Window that I might not fully understand, you can achieve this by creating separate variables: one for the symbolic expression and another for its numeric value. This method allows you to verify expressions whenever you encounter unexpected values. Here is a demonstration of this approach:
syms a b
a_value = 10;
b_value = 5;
k_expression = "a + b";
k = a_value + b_value;
displayFormula(k_expression)
For more details on creating symbolic expressions, refer to the following MATLAB documentation:
Best,
Umang
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Variables, Expressions, Functions, and Preferences 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!