Least Square Method Output: 2 parameters and residual
2 views (last 30 days)
Show older comments
Hi there,
for an university project we need urgent help.
1) We need to estimate 2 parameters (intercept with y-axes, and the slope of the line) and the residual of a stochastic model by using 2 columns. The problem is if we use lsqr we only get the slope and the residual.
2) We imported some data using xlsread and matlab created a variable as a table. We want to create a Matrice with some data out of the table and don't know the correct syntax for doing that, because the colummns and rows are not named with letters, so we don't how to adress for example row 2 and column 3.
Best wishes and thank you for your help! Steffen
Answers (1)
Chad Greene
on 24 May 2015
You can use polyfit to get the slope and y intercept. Below we create some data that should have a slope of 2, and we'll add some random noise to make it realistic:
% Create and plot some data:
x = 1:10;
y = 2*x + 3*randn(size(x)) + 6;
plot(x,y,'bo')
box off
hold on
% calculate slope and y-intercept of linear fit:
p = polyfit(x,y,1);
slope = p(1);
y_intercept = p(2);
LinearFit = slope*x + y_intercept;
Residuals = y-LinearFit;
% plot linear fit:
plot(x,LinearFit,'k-')
legend('data','linear fit','location','northwest')
legend boxoff
0 Comments
See Also
Categories
Find more on Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!