フィルターのクリア

How to "automatically" show an equation, written in the matlab editor, as a text on a plot

79 ビュー (過去 30 日間)
%Input
x = 1 : 10;
a = 0.5;
y = exp(-a*x); % equation to show inside the plot
plot(x,y)
% Desired Output
Note 1: my goal would be to have a command that, if I change my equation here
y = exp(-a*x);
the text in the plot changes automatically!
Note 2: Currently, I use a "manual" command that I sould update if I change my original equation (y = exp(-a*x)), i.e.
text(2,0.5,sprintf('$$y = e^{-ax}$$'),'Interpreter', 'latex', 'fontsize', 20);
  2 件のコメント
John D'Errico
John D'Errico 2022 年 8 月 24 日
編集済み: John D'Errico 2022 年 8 月 24 日
Sorry. You cannot set up a link to a symbolic variable, that will then automatically change annotations on a plot, with nothing done by you.
Could you create a new custom class, that would do the work for you? Probably. It would take some work, and I am sure things would go crazy some of the time.
You would probably spend way more time writing that code to do this than you currently spend just calling text.
Sim
Sim 2022 年 8 月 24 日
So... Does it mean that what I asked is currently impossible with Matlab? :-)

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2022 年 8 月 26 日
I apologise for the delay.
Re-posting —
syms x
% x = 1 : 10 ;
a = 0.5 ;
y = exp(-a*x); % equation to show inside the plot
figure
fplot(y, [0 10 ])
text(2,0.5, ['$y = ' latex(y) '$'], 'Interpreter','latex', 'FontSize',16)
I am not certain what you mean by ‘Note: I do not know if it is possible, but the best for me would be to use your solution (or something very similar), but showing the variable name "a", instead of its value "0.5", in the plot's text’ however it is possible to put the text object in a location determined by the data:
figure
hfp = fplot(y, [0 10]);
Xv = hfp.XData; % Get Data
Yv = hfp.YData; % Get Data
YL = ylim;
Ytxt = median(YL); % This Can Be Any Appropriate 'Y' Value
Xtxt = interp1(Yv, Xv, Ytxt); % Corresponding 'X' Value For Curve
text(Xtxt,Ytxt, ['$y = ' latex(y) '$'], 'Interpreter','latex', 'FontSize',16, 'Horiz','left', 'Vert','bottom')
To use an ‘x’ value (instead of a ‘y’ value) to initially determine the position, reverse the ‘Xtxt’ and ‘Ytxt’ code.
It is also possible to put it in the 'best' location as determined by the legend function (providing the legend itself is not also desired, since if it is, that approach will obviously fail). See Best Text/number location on the plot for that approach.
.

その他の回答 (4 件)

Sim
Sim 2022 年 8 月 26 日
@Star Strider why did you delete your solution ?
syms x
% x = 1 : 10 ;
a = 0.5 ;
y = exp(-a*x); % equation to show inside the plot
figure
fplot(y, [0 10 ])
text(2,0.5, ['$y = ' latex(y) '$'], 'Interpreter','latex', 'FontSize',16)
  2 件のコメント
Star Strider
Star Strider 2022 年 8 月 26 日
@Sim — I wasn’t certain that it provided the desired result as to ‘automatically’ presenting the function. It requires a text call whose coding and placement is not automatic, and although it uses the latex function to provide the correct formatting, does nothing ‘automatically’.
I also saved the code, however you saved it as well. If you want me to re-post it as an answer, I wlll.
Sim
Sim 2022 年 8 月 26 日
編集済み: Sim 2022 年 8 月 26 日
Thanks a lot @Star Strider! I try to reply with bullet points....
  1. Your code did what I needed. Indeed, by writing just one time a certain equation (in this case: "y = exp(-a*x); "), that certain equation was "automatically" written in the plot's text, as desired, just through a combination of the latex and text functions....
  2. Yes please, if you re-post your solution I will accept it..
  3. Note: I do not know if it is possible, but the best for me would be to use your solution (or something very similar), but showing the variable name "a", instead of its value "0.5", in the plot's text...

サインインしてコメントする。


Matt J
Matt J 2022 年 8 月 24 日
Does the text have to be situated inside the plot? ezplot() will automatically create a plot title displaying the mathematical form of the function.
syms x
a=0.5;
ezplot(exp(-a*x))
  1 件のコメント
Sim
Sim 2022 年 8 月 26 日
編集済み: Sim 2022 年 8 月 26 日
thanks for your reply @Matt J ... well, I would like to have the text near the curve (maybe exporting the string and use it with the text function)...Other way around... Is there a way to extract the text "exp(-x/2)" from ezplot (not recommended) or fplot ?

サインインしてコメントする。


vamshi sai yele
vamshi sai yele 2022 年 8 月 26 日
Hi there,
My understanding is that you wish to display a matlab-written equation as text on a graphic and as per my knowledge, MATLAB currently does not support this but the work around would be
You can accomplish this by utilising the "text" function. The code for the same is displayed below:
x = linspace(0,10,50);
y = sin(x);
plot(x,y)
u = '\leftarrow sin(\pi) = 0';
text(pi,sin(pi),u)
In the above code, first and second arguments of ‘text’ function are (x,y) coordinates respectively on the plot where the equation starts.
To address specifically about the equation you mentioned, last line should be modified as:
text(2.5,0.5,'\leftarrow y=e^-^a^x')
or
text(2.5,0.5,'\leftarrow y=e^-^a^x')
Make changes in the arguments accordingly to achieve the desired results…!!
For your reference and to learn more about this, kindly refer to this link
  1 件のコメント
Sim
Sim 2022 年 8 月 26 日
Thanks @vamshi sai yele, but what you wrote is what I did already to show my desired output (please read my "Note 2")..

サインインしてコメントする。


Walter Roberson
Walter Roberson 2022 年 8 月 26 日
Write your "master" equation as a character vector or string instead of as a symbolic expression.
Then have the code use str2sym() to convert the character vector or string into something symbolic that you could then calculate with and plot. You can then text() the character (or string) form of it.
In this way you only have to change the character (or string) form of the equation and re-run in order to get the description updated.

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by