Matlab - Normal equation for linear least squares model fitting

Click For Summary
SUMMARY

This discussion focuses on implementing linear least squares model fitting in MATLAB using normal equations. The normal equation is defined as (A'*A*c)=(A'*y), where A is constructed from the input vector x and a column of ones. The user attempts to create a function named mregress to compute the coefficients but encounters an error due to incorrect vector representation. The correct representation is A=[x,ones(n,1)], where n is the number of elements in x.

PREREQUISITES
  • Understanding of linear algebra concepts, specifically normal equations.
  • Familiarity with MATLAB syntax and functions.
  • Knowledge of matrix operations in MATLAB.
  • Basic understanding of linear regression modeling.
NEXT STEPS
  • Explore MATLAB's matrix manipulation functions, particularly ones and matrix concatenation.
  • Learn about linear regression techniques in MATLAB, focusing on fitlm function.
  • Study the implementation of normal equations in different programming languages.
  • Investigate error handling in MATLAB to troubleshoot common coding issues.
USEFUL FOR

Students learning MATLAB, data analysts performing linear regression analysis, and anyone interested in implementing statistical modeling techniques using MATLAB.

jmher0403
Messages
21
Reaction score
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
A=[x,ones(n,1)] where n is the number of elements in x.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
1K
Replies
6
Views
1K
Replies
1
Views
2K
Replies
4
Views
2K
Replies
15
Views
2K
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
3K