How to show the variable name instead of its value in a plot's text, when using "syms", "text" and "latex" functions

3 次查看(过去 30 天)
Question: By using both syms, latex and text functions/tools (as in the example here below), how can I show the variable name instead of its value, in a plot's text ?
What I currently have: In this example, if I assign a value to the variable "a", i.e. "a = 0.01", the latex function will show its value, i.e. "0.01" (i.e. "1/100") and not its name, i.e. "a".
syms x
a = 0.01 ;
y = exp(-a*x); % equation to show inside the plot
figure
fplot(y, [0 500])
text(100,0.5, ['$y = ' latex(y) '$'], 'Interpreter','latex', 'FontSize',16)
My desired output: If possible, I would like to still use both syms, latex and text functions/tools (as in my example), and see/show the variable name "a" in the plot's text, instead of its value "0.01" (i.e. "1/100").

采纳的回答

Dyuman Joshi
Dyuman Joshi 2022-8-26
syms y(a,x)
y(a,x) = exp(-a*x); % equation to show inside the plot
figure
fplot(y(0.01,x), [0 500])
text(100,0.5, ['$y = ' latex(y) '$'], 'Interpreter','latex', 'FontSize',16)

更多回答(1 个)

Walter Roberson
Walter Roberson 2022-8-26
You cannot do that, not without reconstructing the formula
syms x
a = 0.01 ;
y = exp(-a*x); % equation to show inside the plot
children(y)
ans = 1×1 cell array
{[-x/100]}
children(ans{1})
ans = 1×2 cell array
{[x]} {[-1/100]}
Notice that there is no "a" anywhere in the breakout of the expression.
  2 个评论
Walter Roberson
Walter Roberson 2022-8-26
Consider this:
a = 1
b = a * 5
a = 2
What is b now? Does it become 10 because a changed to 2 and b is a * 5 ? Or is it 5? Or is it internally a formula "a * 5" that you would somehow be able to extract if you only knew how?
When you build a = 0.01, y = exp(-a*x), it works the same way as the numeric case.
Sim
Sim 2022-8-28
thanks @Walter Roberson! ....but..... what do you think about the @Dyuman Joshi solution ?
It looks like working for my needs.... I mean, in that wasy I do not need to write the equation more than once....

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Assumptions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by