Matlab - Normal equation for linear least squares model fitting

In summary, the conversation discusses constructing a function in MATLAB to model linear least squares model fitting using normal equations. The necessary equations and variables are also provided, including the normal equation, A and y matrices, and the coefficients for the best fit line. The attempt at a solution includes a function called mregress and an error that may be caused by incorrect representation of the x and 1 column vectors. The suggested solution is to use A=[x,ones(n,1)] where n is the number of elements in x.
  • #1
jmher0403
22
0

Homework Statement



Im trying to construct a function in MATLAB that models linear least squares model fitting through the use of normal equations.

Homework Equations



Normal equation

(A'*A*c)=(A'*y)

A= [column vector of all x ; column vector of all 1]
y= [column vector of all y]
c = [b;a] where a&b are coefficients of the best fit line

The Attempt at a Solution

function c=mregress(x,y)
A=[x 1]
B=[y]

c=(A'*A)\(A'*B);

Error in ==> mregress at 2
A=[x 1]


I am very new to MATLAB and don't know much about it.
I think the problem is representing x a column vector and 1 is also a column vector of 1.

How do I represent this?
 
Physics news on Phys.org
  • #2
A=[x,ones(n,1)] where n is the number of elements in x.
 

What is the normal equation for linear least squares model fitting in Matlab?

The normal equation for linear least squares model fitting in Matlab is a mathematical formula used to find the optimal values for the coefficients of a linear regression model. It is used to minimize the sum of squared errors between the predicted values and the actual values of the dependent variable.

How do you implement the normal equation for linear least squares model fitting in Matlab?

To implement the normal equation for linear least squares model fitting in Matlab, you first need to define your data points and the dependent variable. Then, you can use the built-in function "pinv" to calculate the pseudo-inverse of the matrix of data points. Finally, you can use this result to calculate the coefficients of the linear regression model using the formula: b = (X'*X)^-1 * X' * y, where b is the vector of coefficients, X is the matrix of data points, and y is the vector of the dependent variable.

What are the advantages of using the normal equation for linear least squares model fitting in Matlab?

One of the main advantages of using the normal equation for linear least squares model fitting in Matlab is that it provides a closed-form solution, meaning that there is no need for iterative algorithms to find the optimal values of the coefficients. Additionally, it is a simple and efficient method for finding the best-fit line for a linear regression model.

What are the limitations of using the normal equation for linear least squares model fitting in Matlab?

One limitation of using the normal equation for linear least squares model fitting in Matlab is that it can be computationally expensive for large datasets, as it involves calculating the inverse of a matrix. Additionally, it may not be suitable for nonlinear relationships between the variables, as it assumes a linear relationship between the dependent and independent variables.

How can you evaluate the performance of a linear regression model using the normal equation in Matlab?

To evaluate the performance of a linear regression model using the normal equation in Matlab, you can calculate the residual sum of squares (RSS) and the R-squared value. The RSS measures the difference between the predicted values and the actual values of the dependent variable, while the R-squared value represents the proportion of variation in the dependent variable that is explained by the independent variable. A higher R-squared value indicates a better fit of the model to the data.

Similar threads

  • Calculus and Beyond Homework Help
Replies
6
Views
637
  • Calculus and Beyond Homework Help
Replies
1
Views
253
  • Calculus and Beyond Homework Help
Replies
14
Views
578
  • Calculus and Beyond Homework Help
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • Linear and Abstract Algebra
Replies
4
Views
846
  • Calculus and Beyond Homework Help
Replies
1
Views
596
  • Calculus and Beyond Homework Help
Replies
3
Views
556
  • Calculus and Beyond Homework Help
Replies
2
Views
969
  • Set Theory, Logic, Probability, Statistics
Replies
4
Views
1K
Back
Top