Lecture 5
Lecture 5
Linear Regression: models a linear relationship between the input variables or independent variables (X)
and the output variables or dependent variables (Y).
1. linsolve
2. solve
3. mldivide symbol (\)
𝒙𝒊 𝒚𝒊
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
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
• The model display includes the model formula, estimated coefficients, and
model summary statistics.
𝒙𝒊 𝒚𝒊
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
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)
𝒚 = 𝟎. 𝟓𝟕𝟔𝟏𝟒 + 𝟎. 𝟎𝟏𝟒𝟔𝟖𝟔𝒙
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.
A N
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)