Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

Lecture 5

Uploaded by

23mt0377
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lecture 5

Uploaded by

23mt0377
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Module 2: Linear Regression

Linear Regression: models a linear relationship between the input variables or independent variables (X)
and the output variables or dependent variables (Y).

Fit the best straight line through the given data

Department of Chemical Engineering


Linear Regression and Interpolation

Linear Regression Linear Interpolation

Department of Chemical Engineering


Least-Squares Method

Department of Chemical Engineering


Least-Squares Method

System of Linear Equations

Department of Chemical Engineering


Least-Squares Method (Multi-variable)

Department of Chemical Engineering


Least-Squares Method in MATLAB

System of Linear Equations

3 Methods for solving in MATLAB –

1. linsolve
2. solve
3. mldivide symbol (\)

Department of Chemical Engineering


Least-Squares Method: Example
Example: Given experimental data (𝒙𝒊 , 𝒚𝒊 ).

𝒙𝒊 𝒚𝒊
10 0.716 Fit a linear model to the data using
15 0.806 Least-Squares method.
20 0.869
𝑦 = 𝑎0 + 𝑎1 𝑥
25 0.943
30 1.013
35 1.096
40 1.160

Department of Chemical Engineering


Least-Squares Method: Example

clc
clear all
X = [10, 15, 20, 25, 30, 35, 40]';
y = [0.716, 0.806, 0.869, 0.943, 1.013, 1.096, 1.160]';
X = [ones(length(X),1),X];
a = X\y;

a =
0.5761 𝒚 = 𝟎. 𝟓𝟕𝟔𝟏 + 𝟎. 𝟎𝟏𝟒𝟕𝒙
0.0147

Department of Chemical Engineering


Linear Regression in MATLAB
• Use the keyword fitlm(X,y) where X is the independent variable data
(or inputs) and y is the dependent variable (or outputs).

• Returns a linear regression model.

• The model display includes the model formula, estimated coefficients, and
model summary statistics.

• The model formula in the display, y ~ 1 + x1 + x2 + x3, corresponds


to y = β0 + β X1+ β2X2 + β3X3 + ϵ
1

Department of Chemical Engineering


Linear Regression: Example
Example: Given experimental data (𝒙𝒊 , 𝒚𝒊 ).

𝒙𝒊 𝒚𝒊
10 0.716 Fit a linear model to the data using fitlm
15 0.806 in MATLAB.
20 0.869
𝑦 = 𝑎0 + 𝑎1 𝑥
25 0.943
30 1.013
35 1.096
40 1.160

Department of Chemical Engineering


Linear Regression: Example

clc
clear all
X = [10, 15, 20, 25,
30, 35, 40]';
y = [0.716, 0.806,
0.869, 0.943, 1.013,
1.096, 1.160]’;
mdl = fitlm(X,y)

𝒚 = 𝟎. 𝟓𝟕𝟔𝟏𝟒 + 𝟎. 𝟎𝟏𝟒𝟔𝟖𝟔𝒙

Department of Chemical Engineering


Linear Regression: Reading text file in MATLAB
• Using the given values for (𝒙𝒊 , 𝒚𝒊 ), create a text file, for example, “data.txt”.

10 0.716
15 0.806 Syntax: load(‘data.txt’)
20 0.869
25 0.943
30 1.013 Name of the file (with extension)
35 1.096
40 1.160

• Keep the text file in the folder where the code (or script file) for calling it is saved.

Department of Chemical Engineering


Linear Regression (Multi-variable): Example
Example: Given data (𝐗, 𝐘). X

Fit a linear model to the data using


Least squares method in MATLAB.

A N

Department of Chemical Engineering


Linear Regression (Multi-variable): Example

clc
clear all
z = load(‘data.txt’);
X = z(:,1:2);
y = z(:,3);
X = [ones(length(X),1),X];
a = X\y;

a =
38.1773
𝒚 = 𝟑𝟖. 𝟏𝟕𝟕𝟑 + 𝟏. 𝟏𝟔𝟑𝟕𝑨 + 𝟎. 𝟐𝟎𝟖𝟗𝑵
1.1637
0.2089
Linear Regression (Multi-variable): Example
Repeat the same example using fitlm in MATLAB.

clc
clear all
z = load(‘data.txt’);
X = z(:,1:2);
y = z(:,3);
mdl = fitlm(X,y)

𝒚 = 𝟑𝟖. 𝟏𝟕𝟕 + 𝟏. 𝟏𝟔𝟑𝟕𝑨 + 𝟎. 𝟐𝟎𝟖𝟖𝟔𝑵

You might also like