use the result of the fit function as a legend in a plot
44 次查看(过去 30 天)
显示 更早的评论
Hello everyone, I wanted to ask how I can make the legend of the graph be the result of the linear regression that is done with the command 'fit', and that polynomial represent it in the legend, for example y = ax + b, there will be some way to do it automatically or is it done manually?. Thanks.
reg=load('regression.txt')
ft=fit(reg(:,1),reg(:,2),'poly1')
plot(ft,'-r',reg(:,1),reg(:,2),'ob')
0 个评论
回答(1 个)
KSSV
2021-7-29
编辑:KSSV
2021-7-29
load census;
f=fit(cdate,pop,'poly2') ;
h = plot(f,cdate,pop) ;
legendinfo = legend(h) ;
f
legendinfo.String{2} = formula(f) ;
2 个评论
KSSV
2021-7-29
load census;
f=fit(cdate,pop,'poly2') ;
eq = formula(f); %Formula of fitted equation
parameters = coeffnames(f); %All the parameter names
values = coeffvalues(f); %All the parameter values
for idx = 1:numel(parameters)
param = parameters{idx};
l = length(param);
loc = regexp(eq, param); %Location of the parameter within the string
while ~isempty(loc)
%Substitute parameter value
eq = [eq(1:loc-1) num2str(values(idx)) eq(loc+l:end)];
loc = regexp(eq, param);
end
end
h = plot(f,cdate,pop) ;
legendinfo = legend(h) ;
legendinfo.String{2} = eq;
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!