필터 지우기
필터 지우기

Getting errors for equations while linear fitting.

조회 수: 41 (최근 30일)
Davin David
Davin David 2022년 12월 11일
댓글: Davin David 2022년 12월 11일
I have two sets of data x and y. After performing a scatter plot of the data I performed a linear fit on the plot using hsline and obtained the equation y = ax + b. But I want to obtain the errors in a and b such that the equation looks like y = (a+ delta a)*x + (b + delta b). How do I do this? I would also like to display the equation on the top left side of the graph.

채택된 답변

the cyclist
the cyclist 2022년 12월 11일
I assume you meant lsline rather than hsline.
lsline does not compute the confidence intervals of the fit parameters. To do that, I would recommend using the fitlm function.
You have a couple options for adding text to a plot. Take a look at text or annotation. (The latter command is a little more challenging to use, but it the more flexible tool.)
  댓글 수: 3
the cyclist
the cyclist 2022년 12월 11일
Here is an example:
% Set random number seed, for reproducibility
rng default
% Generate some simulated data
N = 10;
x = rand(N,1);
y = 2 + 3*x + 0.05*randn(N,1);
% Fit the model
mdl = fitlm(x,y);
% Extract coefficients and standard errors
coef = mdl.Coefficients.Estimate;
se = mdl.Coefficients.SE;
% Make a string that expresses equation
eqString = sprintf("Fit: y = (%4.2f +/- %4.2f) + (%4.2f +/- %4.2f)*x + error",coef(1),se(1),coef(2),se(2));
% Plot data and fit line
figure
hold on
plot(x,y,'*')
plot([0; 1],predict(mdl,[0; 1]),'r-')
% Annotate the plot with the equation
annotation('textbox',[0.17 0.6 0.5 0.3],'String',eqString,'FitBoxToText','on');
You will undoubtedly need to adjust several of the constants here (e.g. the placement of the textbox), and the format of the string that expresses the equation. Both sprintf and annotation can be a bit tricky to learn and use. But they will be very good tools for you, if you learn them.
Davin David
Davin David 2022년 12월 11일
I see. This is very helpful. Thank You

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

John D'Errico
John D'Errico 2022년 12월 11일
편집: John D'Errico 2022년 12월 11일
What is hsline?
help hsline
hsline not found. Use the Help browser search field to search the documentation, or type "help help" for help command options, such as help for methods.
It is not found in any MATLAB toolbox. So if hsline is code you found somewhere, or whatever, then you will need to write the code to compute the confidence intervals you want to see.
Perhaps you are actually using lsline, not hsline, which does not seem to exist. I also did a quick check on the File Exchange, but found nothing with that name.
help lsline
LSLINE Add least-squares fit line to scatter plot. LSLINE superimposes the least squares line in the current axes for plots made using PLOT, LINE, SCATTER, or any plot based on these functions. Any line objects with LineStyles '-', '--', or '.-' are ignored. LSLINE(AX) plots into AX instead of GCA. H = LSLINE(...) returns the handle to the line object(s) in H. See also POLYFIT, POLYVAL, REFLINE. Documentation for lsline doc lsline
Anyway, lsline does not return the confidence intervals. Simplest is probably to use fit (fro mthe curve fitting toolbox, if you have it), which DOES generate confidence intervals for you easily, and also will allow you to plot the line trivially too. Displaying the equation itself will require you to format it as text, and then you can use a tool like text to place it on the figure. Or you could put it in the title as I often do.

카테고리

Help CenterFile Exchange에서 Model Building and Assessment에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by