Lab4 Ch22b023 CTC
Lab4 Ch22b023 CTC
LAB 4
Yuvraj Ramesh Mhatre
CH22B023
Date :- 28-08-2024
SOLVED
Problem Statement/Aim :-
Using linear regression fit a straight line for the given data points .
Sr.no xi yi
1 1 2
2 2 4.5
3 3 5
4 4 9
Theory :-
• Linear regression aims to fit a straight line to a set of data points such that
the sum of the squared differences between the observed values and the
values predicted by the line is minimised.
• The goal is to fit a straight line that best represents the data points.
• The idea behind linear regression is to establish a linear equation that
describes the relationship between the variables. This equation is
characterized by two parameters: the slope and the intercept, which
define the line's angle and position, respectively.
• To find the best-fit line, we minimize the sum of the squared differences
between the observed values and the values predicted by the line. These
differences, known as residuals, indicate how far off the predictions are
from the actual data points.
xi =[1 2 3 4] ;
% Initial conditions
Defineasthe
coeff
coefficient matrix A with
zeros . Run a For loop taking the values
coeff = [2.8 ; 1];
of A as follows
v2 = T(:,2);
% The eigenvalues are on the diagonal Solve using inverse
B = [ v1 v2 ]; of D method inv(A)*B ;
lambda1 = D(1,1);
% The eigenvalues are on the
% Solve for the constants
lambda2 C1 and
= D(2,2); diagonal of D
C2 using initial conditions Using the solution of A
lambda1 = D(1,1); ad B matrix create a
C =inv(B)*coeff ; equation of the straight
lambda2 are
% The corresponding eigenvectors = D(2,2);
line
the columns of V
% Define the time vector
v1 = T(:,1);
% The corresponding
t = 0:0.01:0.1;v2 = T(:,2); eigenvectors are the columns Plot the
of V graphs of
B = [ v1 v2 ]; given data and
% Calculate x(t) and y(t) using v1 = T(:,1); fitted line
the general solution v2 = T(:,2);
% Solve for the constants C1 and C2
x_t = C(1) * v1(1)
using* initial
exp(lambda1
conditions B = [ v1 v2 ];
* t) + C(2) * v2(1) * exp(lambda2
Matlab code :-
clear all
xi =[1 2 3 4] ;
%defining matix A
A = zeros(2,2);
n = length(xi);
for i=1:n
A(2,2)= A(2,2) + 1;
end
%defining matix B
B = zeros(2,2);
for i =1:n
end
coeff = inv(A)*B ;
a = coeff(1,1);
b = coeff(2,1);
plot(xi ,yi ,'o');
hold on ;
x = 0:1:5 ;
y = 0:1:10 ;
y = a*x +b ;
plot (x,y,'b')
ylabel('y values');
Problem Statement/Aim :-
Theory :-
• The goal is to fit line that best represents the data points.
• To find the best-fit line, we minimize the sum of the squared differences
between the observed values and the values predicted by the line. These
differences, known as residuals, indicate how far off the predictions are
from the actual data points.
• We performed Linear regression using matrix operations. This involves
representing the observed data and coefficients in matrix form and
applying linear algebra techniques to solve for the parameters that
minimize the error. This method is computationally efficient and can
handle large datasets effectively.
xi =[1 2 3 4 5] ;
yi =[ 10 17 32 47 70
];
clear all
xi =[1 2 3 4 5];
yi =[10 17 32 47 70];
%defining matix A
A = zeros(3,3);
n = length(xi);
for i=1:n
A(3,3)= A(3,3) + 1;
end
%defining matix B
B = zeros(3,3);
for i =1:n
end
coeff = inv(A)*B ;
coeff = inv(A)*B ;
a = coeff(1,1);
b = coeff(2,1);
c = coeff(3,1);
figure(2)
hold on ;
x = 0:1:6;
y = a*(x.^2) +b*x +c ;
plot (x,y,'r')
ylabel('y values');