在Matlab中的《 Fit in Legend》中编写方程式 [英] Write Equation from Fit in Legend, Matlab
问题描述
因此,我已经使用拟合"将指数曲线拟合到一些数据点,现在我想在图中的图例中获得拟合曲线的方程式.我怎样才能做到这一点?我想在图例中使用y = Ce ^ -xt形式的方程式.我可以从拟合曲线中获得系数C和x,然后将方程式放在图例中吗?还是可以某种方式写出整个方程式?我有很多绘制的图,所以如果该方法不太费时,我将不胜感激.
So, I've fitted an exponential curve to some data points using 'fit' and now I want to get the equation of the fitted curve in the Legend in the graph. How can I do that? I want to have an equation on the form y=Ce^-xt in the legend. Can I get the coefficients, C and x from the fitted curve and then put the equation inside the legend? Or can I get the whole equation written out in some way? I have many plotted graphs so I would be grateful if the method is not so much time consuming.
也许我不清楚.主要问题是如何从绘制的拟合线中得出系数.然后,我想在图中的图例中包含方程式.我是否必须手动取出系数(以及如何完成?),或者Matlab可以像例如在excel中那样直接将其写出来?
Perhaps I was unclear. The main problem is how I should get out the coefficients from the fitted line I've plotted. Then I want to have the equation inside the legend in my graph. Do I have to take out the coefficients by hand (and how can it be done?) or can Matlab write it straight out like in, for example, excel?
推荐答案
文档解释说,您可以获得以c = fit(...)
和
The documentation explains that you can obtain the coefficients derived from a fit executed as c = fit(...)
, as
coef=coeffvalues(c)
C=coef(1)
x=coef(2)
您可以创建自己的图例,如以下示例所示.
You can create your own legend as illustrated by the following example.
根据需要将C
和x
定义为参数
Defining C
and x
as the parameters from your fits,
% simulate and plot some data
t= [0:0.1:1];
C = 0.9;
x = 1;
figure, hold on
plot(t,C*exp(-x*t)+ 0.1*randn(1,length(t)),'ok', 'MarkerFaceColor','k')
plot(t,C*exp(-x*t),':k')
axis([-.1 1.2 0 1.2])
% here we add the "legend"
line([0.62 0.7],[1 1],'Linestyle',':','color','k')
text(0.6,1,[ ' ' num2str(C,'%.2f'),' exp(-' num2str(x,'%.2f') ' t) ' ],'EdgeColor','k')
set(gca,'box','on')
示例输出:
您可能需要调整数字格式和文本框的大小以适合您的需求.
You may have to adjust number formatting and the size of the text box to suit your needs.
这篇关于在Matlab中的《 Fit in Legend》中编写方程式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!