필터 지우기
필터 지우기

use the result of the fit function as a legend in a plot

조회 수: 35 (최근 30일)
ignacio bobadilla tapia
ignacio bobadilla tapia 2021년 7월 29일
댓글: KSSV 2021년 7월 29일
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')

답변 (1개)

KSSV
KSSV 2021년 7월 29일
편집: KSSV 2021년 7월 29일
load census;
f=fit(cdate,pop,'poly2') ;
h = plot(f,cdate,pop) ;
legendinfo = legend(h) ;
f
f =
Linear model Poly2: f(x) = p1*x^2 + p2*x + p3 Coefficients (with 95% confidence bounds): p1 = 0.006541 (0.006124, 0.006958) p2 = -23.51 (-25.09, -21.93) p3 = 2.113e+04 (1.964e+04, 2.262e+04)
legendinfo.String{2} = formula(f) ;
  댓글 수: 2
ignacio bobadilla tapia
ignacio bobadilla tapia 2021년 7월 29일
@KSSV I want the coefficients to appear in the legend, that is, that p1, p2 and p3 are the numerical values obtained with the fit function, how can it be done?
KSSV
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 CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by