1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab - Normal equation for linear least squares model fitting

  1. Apr 6, 2012 #1
    1. The problem statement, all variables and given/known data

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



    2. Relevant 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

    3. 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 dont 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?
     
  2. jcsd
  3. Apr 6, 2012 #2
    A=[x,ones(n,1)] where n is the number of elements in x.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab - Normal equation for linear least squares model fitting
  1. Least squares fitting (Replies: 12)

  2. Least-squares fit (Replies: 2)

Loading...