0% found this document useful (0 votes)
54 views2 pages

Regressions in Matlab

This document discusses using least squares fits in Matlab to fit curves to data. It provides examples of Matlab code to calculate the best fit curve for linear (y=ax), linear (y=ax+b), and power (y=ax^2) curves. The code uses vectors of x and y values as input and calculates coefficients by minimizing the sum of squared residuals between the data and fitting curve. It also explains how to get the number of data points using the size command.

Uploaded by

aleksandarpmau
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views2 pages

Regressions in Matlab

This document discusses using least squares fits in Matlab to fit curves to data. It provides examples of Matlab code to calculate the best fit curve for linear (y=ax), linear (y=ax+b), and power (y=ax^2) curves. The code uses vectors of x and y values as input and calculates coefficients by minimizing the sum of squared residuals between the data and fitting curve. It also explains how to get the number of data points using the size command.

Uploaded by

aleksandarpmau
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

MA 422

Lia Vas

Least-Squares Fits in Matlab


If we need to fit the data (xi, yi ), i=1,2,...,m to a curve y=f(x) using least-square fit, recall that
we need to minimize the function
If M-file for calculating the parameters of the curve f(x) is to be written, the input should be a
vector X whose entires are values xi,for i=1,2,...,m and a vector Y whose entries are the
values yi for i=1,2,...,m. Then the length m needs to be calculated.
The command size(X, 2) calculates the length of a vector X. In general if A is an mxn matrix,
the command size(A, 1) returns the number of rows m and the command size(A, 2) returns
the number of columns n.
For example, if X=[2 5 6 0], Matlab registers it as a 1x4 matrix. Thus, the outcome of the
command size(X, 2) would be 4.
Example 1. Let us consider the Matlab code for calculating the best fit curve of the form y=ax.
Recall that then

and

Solving for a gives

us

The following Matlab code calculates a for a data set (xi, yi ), i=1,2,...,m. Before executing this
code, x-values should be entered as a vector X and y-values should be entered as a vector Y.
X=[x1, x2, ... xm] and Y=[y1, y2, ... ym]. Note that the size of X and Y need to be the same.
The command X(i) returns the i-th coordinate xi .
function a=proportion(X, Y)
sxsq=0;
(sxsq is a variable for sum of squares of x-values)
sxy=0;
(sxy is a variable for sum of products of x and y-values)
m=size(X, 2);
(m is the number of x and y values)
for i=1:m
sxsq=sxsq+X(i)^2;
sxy=sxy+X(i)*Y(i);
end
a=sxy/sxsq;
Example 2. If we need to calculate a linear least-squares fit y=ax+b, for a data set (xi, yi ),
i=1,2,...,m, we are minimizing the function
and

The derivatives are

Solving for a and b gives us equations


and
The following Matlab code calculates a and b for a data set (xi, yi ), i=1,2,...,m.
function [a, b]=linear_fit(X, Y)
sx=0;
(sx is a variable for sum of x-values)
sy=0;
(sy is a variable for sum of y-values)
sxy=0;
(sxy is a variable for sum of products of x and y-values)
sxsq=0;
(sxsq is a variable for sum of squares of x-values)
sysq=0;
(sysq is a variable for sum of squares of y-values)
m=size(X, 2);
(m is the number of x and y values)
for i=1:m
sx=sx+X(i);
sy=sy+Y(i);
sxsq=sxsq+X(i)^2;
sysq=sysq+Y(i)^2;
sxy=sxy+X(i)*Y(i);
end
a=(m*sxy-sx*sy)/(m*sxsq-sx^2);
b=(sxsq*sy-sxy*sx)/(m*sxsq-sx^2);
Example 3. If a transformed least-squares fit is to be used to find the power curve of the form
y=ax2 for for a data set (xi, yi ), i=1,2,...,m, we need to find the least-squares fit for ln(y)=ln(a)
+2ln(x)=A+2ln(x). In this case we minimize the function
As

this gives us that

The following Matlab code calculates the coefficient a.


function a=transformed_squares(X, Y)
m=size(X, 2);
(m is the number of x and y values)
A=0;
(A is a variable for sum of ln(y)-2ln(x) values)
for i=1:m
A=A+log(Y(i))-2*log(X(i));
end
A=A/m;
a=exp(A);
(as A=ln(a), a=eA)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy