0% found this document useful (0 votes)
15 views15 pages

Lab4 Ch22b023 CTC

Uploaded by

ch22b023
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)
15 views15 pages

Lab4 Ch22b023 CTC

Uploaded by

ch22b023
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/ 15

COMPUTATIONAL TECHNIQUES FOR CHEMICAL ENGINEERS

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.

• 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.

• The linear regression technique is widely used in various fields, such as


economics, engineering, and social sciences, to predict outcomes, identify
trends, and make decisions based on data. It provides a straightforward
approach to understanding and quantifying relationships between
variables.
• The coefficients of the best-fit line are found by solving the matrix
equation:
A* coeff = B
The coefficients (a and b) are calculated by finding the inverse of matrix
A and multiplying it by matrix B:
coeff = inv(A)*B
The result gives us the slope and the y-intercept of the best-fit line.
Algorithm :-
Define the variables and insert
the given data

xi =[1 2 3 4] ;

yi =[2 4.5 5 9];

% Initial conditions
Defineasthe
coeff
coefficient matrix A with
zeros . Run a For loop taking the values
coeff = [2.8 ; 1];
of A as follows

A(1,1)= A(1,1) + xi(i)*xi(i);


% Compute eigenvalues and
eigenvectors A(1,2)= A(1,2) + xi(i);

[T, D] = eig(A); A(2,1)= A(2,1) + xi(i);


A(2,2)= A(2,2) + 1;

% The eigenvalues are on the


diagonal of D

lambda1 = D(1,1); Define the coefficient matrix


% Initial conditions as coeff
B with zeros . Run a For loop
lambda2 = D(2,2);
coeff = [2.8 ; 1]; taking the values of B as
follows

% The corresponding B(1,1) = B (1,1) + xi(i)*yi(i);


% Compute eigenvalues and
eigenvectors are the columns of
eigenvectors B(2,1) = B (2,1) + yi(i);
V
[T, D] = eig(A);
v1 = T(:,1);

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

%entering the given data

xi =[1 2 3 4] ;

yi =[2 4.5 5 9];

%defining matix A

A = zeros(2,2);

n = length(xi);

for i=1:n

A(1,1)= A(1,1) + xi(i)*xi(i);

A(1,2)= A(1,2) + xi(i);

A(2,1)= A(2,1) + xi(i);

A(2,2)= A(2,2) + 1;

end

%defining matix B

B = zeros(2,2);

for i =1:n

B(1,1) = B (1,1) + xi(i)*yi(i);

B(2,1) = B (2,1) + yi(i);

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')

xlabel ('x values');

ylabel('y values');

legend('Given Data', 'Fitted line');


Result :-
Graph:-
Values of A after substituting the values :-

Values of B after substituting the values :-

Values of a which is one of the coefficient after solving A and B :-

Values of b which is one of the coefficient after solving A and B :-

o Linear regression is a powerful tool for modelling and understanding the


linear relationship between variables.
o It gives us the idea about the trend of the data .
o By minimizing the error and using matrix techniques, it allows for
efficient and accurate fitting of a straight line to data, making it a
fundamental method in statistical analysis and predictive modelling.
UNSOLVED

Problem Statement/Aim :-

Find a , b, c by the linear regression method


Given :- xi =[1 2 3 4 5]
yi =[10 17 32 47 70]

Theory :-

• Linear regression aims to fit a quadratic trendline 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 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 3 parameters: a , b , c 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.
• 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.

• The linear regression technique is widely used in various fields, such as


economics, engineering, and social sciences, to predict outcomes, identify
trends, and make decisions based on data. It provides a straightforward
approach to understanding and quantifying relationships between
variables.
• The coefficients of the best-fit line are found by solving the matrix
equation:
A*coeff = B
The coefficients (a and b) are calculated by finding the inverse of matrix
A and multiplying it by matrix B:
coeff = inv(A)*B .
Algorithm :-

Define the variables and


insert the given data

xi =[1 2 3 4 5] ;

yi =[ 10 17 32 47 70

];

Define the coefficient matrix A (3*3)with


zeros . Run a For loop taking the values of
A as follows
A(1,1)= A(1,1) +xi(i)*xi(i)*xi(i)*xi(i);
A(1,2)= A(1,2) + xi(i)*xi(i)*xi(i);
A(1,3)= A(1,3) + xi(i)*xi(i);
A(2,1)= A(2,1) + xi(i)*xi(i)*xi(i);
A(2,2)= A(2,2) + xi(i)*xi(i);
A(2,3)= A(2,3) + xi(i);
A(3,1)= A(3,1) + xi(i)*xi(i);
A(3,2)= A(3,2) + xi(i);
A(3,3)= A(3,3) + 1;

Define the coefficient matrix B


(3*3)with zeros . Run a For loop
taking the values of B as follows

B(1,1) = B (1,1) + xi(i)*xi(i)*yi(i);

B(2,1) = B (2,1) + yi(i)*xi(i);

B(3,1) = B (3,1) + yi(i);

Using the solution of A ad B


we will get value of a , b , c
And we can create an
quadratic equation

Plot the graphs of


given data and
quadratic trendline
Matlab code :-

clear all

%entering the given data

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(1,1)= A(1,1) + xi(i)*xi(i)*xi(i)*xi(i);

A(1,2)= A(1,2) + xi(i)*xi(i)*xi(i);

A(1,3)= A(1,3) + xi(i)*xi(i);

A(2,1)= A(2,1) + xi(i)*xi(i)*xi(i);

A(2,2)= A(2,2) + xi(i)*xi(i);

A(2,3)= A(2,3) + xi(i);

A(3,1)= A(3,1) + xi(i)*xi(i);

A(3,2)= A(3,2) + xi(i);

A(3,3)= A(3,3) + 1;

end

%defining matix B

B = zeros(3,3);

for i =1:n

B(1,1) = B (1,1) + xi(i)*xi(i)*yi(i);

B(2,1) = B (2,1) + yi(i)*xi(i);

B(3,1) = B (3,1) + yi(i);

end

coeff = inv(A)*B ;
coeff = inv(A)*B ;

a = coeff(1,1);

b = coeff(2,1);

c = coeff(3,1);

figure(2)

plot(xi ,yi ,'o');

hold on ;

x = 0:1:6;

y = a*(x.^2) +b*x +c ;

plot (x,y,'r')

xlabel ('x values');

ylabel('y values');

legend('Given Data','quadratic trendline');


Result :-
Graph:-

Values of A after substituting the values :-

Values of B after substituting the values :-


Values of a which is one of the coefficient after solving A and B:-

Values of b which is one of the coefficient after solving A and B :-

Values of c which is one of the coefficient after solving A and B:-

o Linear regression is a powerful tool for modelling and understanding the


relationship between variables.

o It gives us the idea about the trend of the data .

o By minimizing the error and using matrix techniques, it allows for


efficient and accurate fitting of a Quadratic Trendline to data, making it a
fundamental method in statistical analysis and predictive modelling.

You might also like