0% found this document useful (0 votes)
103 views

PSLP Lab File 1

pslp lab file

Uploaded by

techgur44
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)
103 views

PSLP Lab File 1

pslp lab file

Uploaded by

techgur44
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/ 50

Probability, Statistics and Linear Programming

EXPERIMENT 1
AIM: Introduction of Matlab and demonstration of simple programming concepts
like matrix multiplication (scalar and vector), loop, conditional statements and
plotting.

INTRODUCTION TO MATLAB

MATLAB is a high-performance language for technical writing and computing. It integrates


computation, visualization and programming in an easy-to-use environment where problems
and solutions are expressed in familiar mathematical notation.
MATLAB stands for Matrix Laboratory. It was originally written to provide easy access to
matrix.

STARTING AND QUITTING MATLAB


To start MATLAB, double-click the MATLAB shortcut on your Windows desktop. You will
know it's running when you see special ">>" prompt in the MATLAB command window.
To end your MATLAB session, select EXIT MATLAB from File menu in Desktop or type
'exit' in Command Window.

DESKTOP TOOLS:
 Command window: Used to enter variables and run functions and m-files.
 Command history: View previously run statements, and copy and execute selected statements.
 Current directory browser: MATLAB file operations use current directory reference point.
Any file you want to run must be in current directory.
 Working space: It consists of the set of variables built up during a MATLAB session and
stored in memory.

BASIC WINDOW COMMANDS:


 'clear' command: Removes all variables from workspace.
 'clc' command: Clears command window and homes the cursor.
 'help' command: help <topic> displays help about the topic.
 'lookfor' command: Provides help by searching through all the first lines of MATLAB,
help topics and returning those containing a keyword you supply.
 'edit' command: Enables to edit any m-file in Editor window.

NOTES:
 A semicolon (;) at the end of a statement suppresses printing of results.
 If a statement does not fit in one line, use "..." followed by 'enter' to indicate that the
statement continues on the next line.
 If no output variable is specified, MATLAB uses variable 'ans' to store the last result.
 Use Up and Down arrow to edit previous commands you entered in Command Window.
 Insert "%" before statement that you want to use as comment. The statement will appear
in green.
Probability, Statistics and Linear Programming

EXPERIMENT 1(A)
MATLAB Basic Examples/Codes
1) Find C.
A = [3,6,7]
B= [1,9,4,5]
C= A(2) + B(4)

CODE:-
clc
clear all
close all
A=[3,6,7];
B=[1,9,4,5];
C=A(2)+B(4);
fprintf('C=%d\n',C);

OUTPUT:-
Probability, Statistics and Linear Programming

2) Find C.
A = [39 ; 16]
B = [52 ; 17]
C = A+B

CODE:-
clc
clear all
close all
A=[39;16];
B=[52;17];
C=(A+B)
fprintf('C=%d\n',C);

OUTPUT:-
Probability, Statistics and Linear Programming

3) Find C.
A = [2,3,5]
B = [2,4,9]
С = А. *B

CODE:-
clc
clear all
close all
A=[2,3,5];
B=[2,4,9];
C=A.*B
fprintf('C=%d\n',C)

OUTPUT:-
Probability, Statistics and Linear Programming

4) Find C and D.
A=[2,3;1,4]
B=[5,1;7,2]
С = А. *B
D = A*B

CODE:-
clc
clear all
close all
A=[2,3;1,4];
B=[5,1;7,2];
C=A.*B
D=A*B
fprintf('C=%d\n',C);
fprintf('D=%d\n',D);

OUTPUT:-
Probability, Statistics and Linear Programming

5) Find C.
A=[4,1,2;0,3,1;0,1,2]
B=[17;19;13]
C=B\A

CODE:-
clc
clear all
close all
A=[4,1,2;0,3,1;0,1,2];
B=[17;19;13];
C=B\A

OUTPUT:-
Probability, Statistics and Linear Programming

6) Find B.
A=[2,3,5]
B=2A2+3A+4

CODE:-
clc
clear all
close all
A=[2,3,5];
B=2.*A.^2+3*A+4

OUTPUT:-
Probability, Statistics and Linear Programming

7) Find X and then display X using disp( ) function.


A=[6,2,3;10,5,14;7,0,9]
B=[10;11;12]
X=A\B;

CODE:-
clc
clear all
close all
A=[6,2,3;10,5,14;7,0,9];
B=[10;11;12];
X=A\B;
disp(X);

OUTPUT:-
Probability, Statistics and Linear Programming

8) Find B and then plot the graph between A and B using plot( ) function.
A=(0:0.5:5)
B=2A2+3A-5

CODE:-
clc
clear all
close all
A=(0:0.5:5);
B=2.*A.^2+3.*A-5;
plot(A,B);
xlabel('A');
ylabel('B');
title('Plot of A vs B');

OUTPUT:-
Plot of A vs B
60

50

40

30
B

20

10

-10
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
A
Probability, Statistics and Linear Programming

9) Find B and C and then plot the graph between A and B and A and C using plot( ) function.
A=(0:0.5:5)
B=2A2+3A-5
C=1.2A2+4A-3

CODE:-
clc
clear all
close all
A=(0:0.5:5);
B=2.*A.^2+3.*A-5;
C=1.2.*A.^2+4.*A-3;
plot(A,B,A,C);
xlabel('A');
ylabel('solution');
title('Plot of A vs B and A vs C');

OUTPUT:-
Plot of A vs B and A vs C
60

50

40

30
solution

20

10

-10
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
A
Probability, Statistics and Linear Programming

10) Find sum of multiples of 3 from 1 to 9 and store it in sum2 and rest of the values from 1
to 9 and store it in sum1.

CODE:-
clc
clear all
close all
sum1=0;
sum2=0;
n=9;
for i=1:n
sum1=sum1+i;
if mod(i,3)==0
sum2=sum2+i;
end
end
fprintf('sum1=%f\n',sum1);
fprintf('sum2=%f\n',sum2);

OUTPUT:-
Probability, Statistics and Linear Programming

EXPERIMENT 1(B)
Basic MATLAB Commands for Plotting
1) Plot Sine and Cosine Curves.

CODE:-
clc
clear all
close all
x=(0:0.01:2*pi);
y=sin(x);
plot(x,y);
z=cos(x);
plot(x,z);
xlabel('x');
ylabel('cos(x)');
title('Cosine Curve');

OUTPUT:-
Cosine Curve
1

0.8

0.6

0.4

0.2
cos(x)

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7
x
Probability, Statistics and Linear Programming

2) Use of hold on function.

CODE:-
clc
clear all
close all
x=(0:0.01:2*pi);
y=sin(x);
plot(x,y);
hold on
z=cos(x);
plot(x,z);
xlabel('x');
ylabel('f(x)');
title('Sine and Cosine Curve');

OUTPUT:-
Sine and Cosine Curve
1

0.8

0.6

0.4

0.2
f(x)

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7
x
Probability, Statistics and Linear Programming

3) Use of figure function.

CODE:-
clc
clear all
close all
x=(0:0.01:2*pi);
y=sin(x);
figure
plot(x,y);
xlabel('x');
ylabel('sin(x)');
title('Sine Curve');
z=cos(x);
figure
plot(x,z);
xlabel('x');
ylabel('cos(x)');
title('Cosine Curve');
Probability, Statistics and Linear Programming

OUTPUT:-
Sine Curve
1

0.8

0.6

0.4

0.2
sin(x)

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7
x

Cosine Curve
1

0.8

0.6

0.4

0.2
cos(x)

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7
x
Probability, Statistics and Linear Programming

4) Use of subplot( ) function.

CODE:-
clc
clear all
close all
x=(0:0.01:2*pi);
y=sin(x);
subplot(2,1,1);
plot(x,y);
xlabel('x');
ylabel('sin(x)');
title('Sine Curve');
z=cos(x);
subplot(2,1,2);
plot(x,z);
xlabel('x');
ylabel('cos(x)');
title('Cosine Curve');

OUTPUT:-
Sine Curve
1

0.5
sin(x)

-0.5

-1
0 1 2 3 4 5 6 7
x
Cosine Curve
1

0.5
cos(x)

-0.5

-1
0 1 2 3 4 5 6 7
x
Probability, Statistics and Linear Programming

5) Use of linewidth, markeredgecolor, etc. in plot( ) function.

CODE:-
clc
clear all
close all
x=(0:0.01:2*pi);
y=sin(x);
plot(x,y,'--rs','LineWidth',2,'MarkerEdgeColor','g','MarkerFaceColor','k','MarkerSize',2);
xlabel('x');
ylabel('sin(x)');
title('Sine Curve');

OUTPUT:-
Sine Curve
1

0.8

0.6

0.4

0.2
sin(x)

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7
x
Probability, Statistics and Linear Programming

EXPERIMENT 1(C)
Power VS Load Resistance
Write a program to plot power supplied to a load as a function of load resistance.

CODE (Without Loop):-


clc
clear all
close all
x=50;
r=(0:5:100);
e=220;
t=x+r;
i=e./t;
S=i.^2;
p=S.*r;
pmax=max(p);
plot(r,p);
xlabel('Resistance');
ylabel('Power');
title('Power relation with load resistance');
axis([0 300 0 300]);
text(50,249,'\downarrow Pmax');
fprintf('The maximum power is %0.2f\n', pmax);
Probability, Statistics and Linear Programming

OUTPUT:-
Power relation with load resistance
300

250  Pmax

200
Power

150

100

50

0
0 50 100 150 200 250 300
Resistance
Probability, Statistics and Linear Programming

CODE (With Loop):-


clc
clear all
close all
r=50;
rl=(10:10:100);
V=220;
n=length(rl);
for i=1:n
I(i)= V/(r+rl(i));
P(i)=(I(i)^2)*rl(i);
end
pmax=max(P);
plot(rl,P);
xlabel('Load resistance');
ylabel('Power');
title('Power relation with load resistance');

OUTPUT:-
Power relation with load resistance
260

240

220

200
Power

180

160

140

120
10 20 30 40 50 60 70 80 90 100
Load resistance
Probability, Statistics and Linear Programming

EXPERIMENT 1(D)
1) Square all the values in the matrix.

CODE:-
clc
clear all
close all
x=(1:5);
m=length(x);
for i=1:m;
y(i)=x(i)^2;
end
y

OUTPUT:-
Probability, Statistics and Linear Programming

2) To check number is even of odd.

CODE:-
clc
clear all
close all
x=input('x= ');
if mod(x,2)==0
disp('Number is Even')
else
disp('Number is Odd')
end

OUTPUT:-
Probability, Statistics and Linear Programming

EXPERIMENT 2
Aim: Program for demonstration of theoretical probability limits.
Write a program to perform different probability distribution:-
1) Tossing a coin once.

CODE:-
clc
clear all
close all
x=rand<0.5

OUTPUT:-
Probability, Statistics and Linear Programming

2) Rolling a dice.

CODE:-
clc
clear all
close all
x=ceil(6*rand)

OUTPUT:-
Probability, Statistics and Linear Programming

3) Rolling a pair of dice.

CODE:-
clc
clear all
close all
x=ceil(6*rand),ceil(6*rand)

OUTPUT:-
Probability, Statistics and Linear Programming

4) Toss a coin bunch of times.

CODE:-
clc
clear all
close all
n=input('Number of time coin is tossed ');
x=rand(1,n)<0.5

OUTPUT:-
Probability, Statistics and Linear Programming

5) Toss a coin bunch of times and count number of heads.

CODE:-
clc
clear all
close all
n=input('Number of time coin is tossed ');
x=rand(1,n)<0.5
H=sum(x) %H is number of heads
T=n-H %T is number of tails

OUTPUT:-
Probability, Statistics and Linear Programming
Probability, Statistics and Linear Programming

EXPERIMENT 3
Aim: Program to plot normal distributions and exponential distributions for
various parametric values.

CODE:-
clc
clear all
close all
load examgrades

x1 = grades(:,1);
mu1 = mean(x1);
sigma1 = std(x1);
A1 = 1/(sigma1*sqrt(2*pi));
B1 = ((x1-mu1)/sigma1).^2;
y1 = A1*exp(-0.5*B1);
z1 = exp(-x1./mu1)./mu1;

x2 = grades(:,2);
mu2 = mean(x2);
sigma2 = std(x2);
A2 = 1/(sigma2*sqrt(2*pi));
B2 = ((x2-mu2)/sigma2).^2;
y2 = A2*exp(-0.5*B2);
z2 = exp(-x2./mu2)./mu2;

x3 = grades(:,3);
mu3 = mean(x3);
sigma3 = std(x3);
A3 = 1/(sigma3*sqrt(2*pi));
B3 = ((x3-mu3)/sigma3).^2;
y3 = A3*exp(-0.5*B3);
z3 = exp(-x3./mu3)./mu3;

x4 = grades(:,4);
mu4 = mean(x4);
sigma4 = std(x4);
A4 = 1/(sigma4*sqrt(2*pi));
B4 = ((x4-mu4)/sigma4).^2;
Probability, Statistics and Linear Programming

y4 = A4*exp(-0.5*B4);
z4 = exp(-x4./mu4)./mu4;

x5 = grades(:,5);
mu5 = mean(x5);
sigma5 = std(x5);
A5 = 1/(sigma5*sqrt(2*pi));
B5 = ((x5-mu5)/sigma5).^2;
y5 = A5*exp(-0.5*B5);
z5 = exp(-x5./mu5)./mu5;

subplot(2,1,1);
plot(x1,y1,'r*');
hold on
plot(x2,y2,'g*');
hold on
plot(x3,y3,'b*');
hold on
plot(x4,y4,'y*');
hold on
plot(x5,y5,'c*');
hold on
xlabel('x')
ylabel('y')
title('Normal distribution')
legend('x1 vs y1','x2 vs y2','x3 vs y3','x4 vs y4','x5 vs y5')

subplot(2,1,2);
plot(x1,z1,'r*');
hold on
plot(x2,z2,'g*');
hold on
plot(x3,z3,'b*');
hold on
plot(x4,z4,'y*');
hold on
plot(x5,z5,'c*');
hold on
xlabel('x')
Probability, Statistics and Linear Programming

ylabel('z')
title('Exponential distribution')
legend('x1 vs z1','x2 vs z2','x3 vs z3','x4 vs z4','x5 vs z5')

OUTPUT:-
Normal distribution
0.08 x1 vs y1
x2 vs y2
0.06
x3 vs y3
0.04 x4 vs y4
y

x5 vs y5
0.02

0
50 55 60 65 70 75 80 85 90 95 100
x
-3
x 10 Exponential distribution
7 x1 vs z1
x2 vs z2
6
x3 vs z3
x4 vs z4
5
z

x5 vs z5
4

3
50 55 60 65 70 75 80 85 90 95 100
x
Probability, Statistics and Linear Programming

EXPERIMENT 4
Aim: Program for fitting of binomial distribution for given n and p.
1) If a coin is tossed 5 times, what is the mean, standard deviation and probability of getting
exactly 2 heads.

CODE:-
clc
clear all
close all
x=2;
n=5;
p=0.5;
mu=n*p;
std=((n*p)*(1-p));
y=binopdf(x,n,p);
fprintf('The mean of getting exactly two head is =%0.4f\n',mu)
fprintf('The standard deviation of getting exactly two head is =%2.4f\n',std)
fprintf('The probability of getting exactly two head is =%0.4f\n',y)

OUTPUT:-
Probability, Statistics and Linear Programming

2) If a coin is tossed 5 times, what is the mean, standard deviation and probability of getting
at least 2 heads.

CODE:-
clc
clear all
close all
x=[2:5];
n=5;
p=0.5;
mu=n*p;
std=((n*p)*(1-p));
y=binopdf(x,n,p);
s=sum(y);
fprintf('The mean of getting at least two head is =%0.4f\n',mu)
fprintf('The standard deviation of getting at least two head is =%2.4f\n',std)
fprintf('The probability of getting at least two head is =%0.4f\n',s)

OUTPUT:-
Probability, Statistics and Linear Programming

3) Using the formula of Binomial Distribution find the mean, standard deviation and
probability of rolling at most 2 sixes in 5 rolls of dice.

CODE:-
clc
clear all
close all
x=[0:2];
n=5;
p=(1/6);
mu=n*p;
std=((n*p)*(1-p));
y=binopdf(x,n,p);
s=sum(y);
fprintf('The mean of getting atmost two sixes is =%0.4f\n',mu)
fprintf('The standard deviation of getting atmost two sixes is =%2.4f\n',std)
fprintf('The probability of getting atmost two sixes is =%0.4f\n',s)

OUTPUT:-
Probability, Statistics and Linear Programming

4) If 8 coins are thrown simultaneously, find the mean, standard deviation and probability of
at least 6 heads.

CODE:-
clc
clear all
close all
x=[6:8];
n=8;
p=0.5;
mu=n*p;
std=((n*p)*(1-p));
y=binopdf(x,n,p);
s=sum(y);
fprintf('The mean of getting atleast six heads is =%0.4f\n',mu)
fprintf('The standard deviation of getting atleast six heads is =%2.4f\n',std)
fprintf('The probability of getting atleast six heads is =%0.4f\n',s)

OUTPUT:-
Probability, Statistics and Linear Programming

EXPERIMENT 5
Aim: Program for fitting of binomial distribution after computing mean and
variance.
Compute the probability distribution function of Binomial Distribution counting the number
of success in 50 trials with the probability of 0.6 in 1 trial.

CODE:-
clc
clear all
close all
n=50;
p=0.6;
x=[1:n];
mu=(n*p);
sigma=(n*p)*(1-p);
sd=sqrt(sigma);
y=binopdf(x,n,p);
figure
bar(x,y)
hold on
x2=[1:n];
y2=normpdf(x2,mu,sd);
plot(x2,y2,'r*')
xlabel('Observation')
ylabel('Probability')
title('Binomial & Normal distribution')
legend('Binomial Distribution','Normal Distribution');
Probability, Statistics and Linear Programming

OUTPUT:-
Binomial & Normal distribution
0.12
Binomial Distribution
Normal Distribution
0.1

0.08
Probability

0.06

0.04

0.02

0
0 10 20 30 40 50 60
Observation
Probability, Statistics and Linear Programming

EXPERIMENT 6
Aim: Program for fitting of Poisson distributions for given value of lambda.
A car distributor in city Y experiences on an average 2.5 car sales per day. Find the
probability that on a random selected day:
(i) They will sell 5 cars
(ii) They will sell no car
(iii) They will sell at most two cars

CODE:-
clc
clear all
close all
lambda = 2.5;
x1 = 5;
y1= poisspdf(x1, lambda);
fprintf('Probability that they sell 5 cars = %2.4f\n', y1);
x2 = 0;
y2 = poisspdf(x2, lambda);
fprintf('Probability that they sell no car =%2.4f\n', y2);
x3 = [0:2];
y3 = poisspdf(x3, lambda);
fprintf('Probability that they sell at most two cars = %2.4f\n',sum(y3));

OUTPUT:-
Probability, Statistics and Linear Programming

EXPERIMENT 7
Aim: Program for fitting of Poisson distributions after computing mean.
Compute the probability distribution function of Poisson distribution with λ=50 and x=0 to
100.

CODE:-
clc
clear all
close all
lambda=50;
x1=[0:100];
y1=poisspdf(x1, lambda);
% compute corresponding normal distribution
x2=[0:0.1:100];
mu=lambda;
sigma=sqrt(lambda);
y2=normpdf(x2,mu,sigma) ;
% plot the pdfs
figure
bar(x1,y1,1) ;
hold on
plot(x2,y2,'r*');
xlabel('Observation');
ylabel('Probability');
title('Poisson & Normal Distribution');
legend('Poisson Distribution','Normal Distribution');
Probability, Statistics and Linear Programming

OUTPUT:-
Poisson & Normal Distribution
0.06
Poisson Distribution
Normal Distribution
0.05

0.04
Probability

0.03

0.02

0.01

0
-20 0 20 40 60 80 100 120
Observation
Probability, Statistics and Linear Programming

EXPERIMENT 8
Aim: Program for fitting of normal distribution when parameters are given.
1) Compute the normal distribution when
a) μ = 0, σ =1
b) μ = 500, σ =100

CODE:-
clc
clear all
close all
x1=[-3:0.1:3];
mu1=0;
sigma1=1;
y1=normpdf(x1,mu1,sigma1);
figure
subplot(1,2,1);
plot(x1,y1);
xlabel('Observations')
ylabel('Probability')
title('Normal distribution')
x2=[150:900];
mu2=500;
sigma2=100;
y2=normpdf(x2,mu2,sigma2);
subplot(1,2,2);
plot(x2,y2);
xlabel('Observations')
ylabel('Probability')
title('Normal distribution')
Probability, Statistics and Linear Programming

OUTPUT:-
-3
Normal distribution x 10 Normal distribution
0.4 4

0.35 3.5

0.3 3

0.25 2.5
Probability

Probability
0.2 2

0.15 1.5

0.1 1

0.05 0.5

0 0
-4 -2 0 2 4 0 500 1000
Observations Observations
Probability, Statistics and Linear Programming

2) Compute the normal distribution using the MATLAB’s example set ‘examgrades’

CODE:-
clc
clear all
close all
load examgrades
[m,n]=size(grades);
c=[1:n];
x=[1:m];
% calculate mean and standard deviation
mu=sum(grades(x,c)/length(grades(x,c)));
sigma=std(grades(x,c));
%plot the pdfs
for i=1:n
for j=1:m
y(j,i)=normpdf(x(j),mu(1,i),sigma(1,i));
end
end
plot(x,y,'r');
xlabel('Observations')
ylabel('Probability')
title('Normal distribution')
Probability, Statistics and Linear Programming

OUTPUT:-
Normal distribution
0.08

0.07

0.06

0.05
Probability

0.04

0.03

0.02

0.01

0
0 20 40 60 80 100 120
Observations
Probability, Statistics and Linear Programming

EXPERIMENT 9
Aim: Program for fitting of linear regression line through given data set and
testing of goodness of fit using mean error.
1) For the given data, fit a linear regression line and compute the mean error.
X=[1 3 2 1 3]
Y=[14 24 18 17 27]

CODE:-
clc
clear all
close all
X=[1 3 2 1 3];
Y=[14 24 18 17 27];
beta1=sum((X-mean(X)).*(Y-mean(Y)))/sum((X-mean(X)).^2);
beta0=mean(Y)-beta1*mean(X);
Y_Cal=beta0+beta1*X;
scatter(X,Y);
hold on
plot(X,Y_Cal) ;
xlabel('X');
ylabel('Y');
title('LINEAR REGRESSION');
grid on;
MSE=sum((Y-Y_Cal).^2)/length(Y);
Probability, Statistics and Linear Programming

OUTPUT:-
LINEAR REGRESSION
28

26

24

22
Y

20

18

16

14
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8
X
Probability, Statistics and Linear Programming

2) Using MATLAB's example library ‘accidents', fit a linear regression line for the total
fatalities and compute the mean error

CODE:-
clc
clear all
close all
load accidents;
X=hwydata(:,14);
Y=hwydata(:,4);
beta1=sum((X-mean(X)).*(Y-mean(Y)))/sum((X-mean(X)).^2) ;
beta0=mean(Y)-beta1*mean(X);
Y_Cal=beta0+beta1*X;
scatter(X,Y);
hold on
plot(X,Y_Cal);
xlabel('Total population');
ylabel('Total fatalities');
title('Linear Regression');
grid on
MSE=sum((Y-Y_Cal).^2)/length(Y);
Probability, Statistics and Linear Programming

OUTPUT:-
Linear Regression
4500

4000

3500

3000
Total fatalities

2500

2000

1500

1000

500

0
0 0.5 1 1.5 2 2.5 3 3.5
Total population 7
x 10
Probability, Statistics and Linear Programming

EXPERIMENT 10
Aim: Program for fitting of Multiple Linear Regression (MLR) curve through
given data set and testing of goodness of fit using mean error.
Using the data given, fit a multiple linear regression plot.
Х1=[60;62;67;70;71;72;75;78]
X2=[22;25;24;20;15;14;14;11]
Y=[140;155;159;179;192;200;212;215]

CODE:-
clc
clear all
close all
% Given data
X1=[60;62;67;70;71;72;75;78];
X2=[22;25;24;20;15;14;14;11];
Y=[140;155;159;179;192;200;212;215];
n=length(Y) ;
% Regression coefficients
Reg_X1=sum(X1.^2)-sum(X1)^2/n;
Reg_X2=sum(X2.^2)-sum(X2)^2/n;
Reg_X1Y=sum(X1.*Y)-sum(X1)*sum(Y)/n;
Reg_X2Y=sum(X2.*Y)-sum(X2)*sum(Y)/n;
Reg_X1X2=sum(X1.*X2)-sum(X1)*sum(X2)/n;
beta1=(Reg_X2*Reg_X1Y-Reg_X1X2*Reg_X2Y)/(Reg_X1-Reg_X2-Reg_X1X2);
beta2=(Reg_X1*Reg_X2Y-Reg_X1X2*Reg_X1Y)/(Reg_X2-Reg_X1-Reg_X1X2);
beta0=mean(Y)-beta1*mean(X1)-beta2*mean(X2);
% Predicted Y values
Y_Cal=beta0+beta1*mean(X1)+beta2*mean(X2);
% Scatter plot
scatter3(X1,X2,Y,'filled','r') ;
hold on
% Generating X1_Fit and X2_Fit grids for plotting
X1_Fit=min(X1):0.1:max(X1);
X2_Fit=min(X2):0.1:max(X2);
[X1_Fit,X2_Fit]=meshgrid(X1_Fit,X2_Fit);
% Generating Y_Fit based on the regression coefficients
Y_Fit=beta0+beta1*X1_Fit+beta2*X2_Fit;
% Plotting the regression surface.
mesh(X1_Fit,X2_Fit,Y_Fit);
Probability, Statistics and Linear Programming

xlabel('X1');
ylabel('X2');
zlabel('Y');
title('MULTIPLE LINEAR REGRESSION');
view(50,10);

OUTPUT:-

You might also like