Getting errors for equations while linear fitting.

43 次查看(过去 30 天)
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.

请先登录,再进行评论。

更多回答(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 中查找有关 Fit Postprocessing 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by