Practical 8

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Practical No.

– 8
Regression
8.1 AIM: Program for Linear Regression.
Problem Statement: Write and execute scilab code for the following: Fit a
straight line for the following data.
X 1 2 3 4 5 6 7
Y 0.5 2.5 2.0 4.0 3.5 6.0 5.5

Scilab Code:
x=[1,2,3,4,5,6,7];
y=[0.5,2.5,2,4,3.5,6,5.5];
n=7;
s=0;
xsq=0;
xsum=0;
ysum=0;
for i=1:7
s=s+(det(x(1,i)))*(det(y(1,i)));
xsq=xsq+(det(x(1,i))^2);
xsum=xsum+det(x(1,i));
ysum=ysum+det(y(1,i));
end
disp("sum of product of x and y=",s)
disp("sum of square of x=",xsq)
disp("sum of all the x=",xsum)
disp("sum of all the y=",ysum)
a=xsum/n;
b=ysum/n;
a1=(n*s-xsum*ysum)/(n*xsq-xsum^2);
a0=b-a*a1;
disp("a1=",a1)
disp("a0=",a0)
disp("The equation of the line obtained is y=a0+a1x")
Output:

8.2 AIM: Program for Polynomial Regression.


Problem Statement: Write and execute scilab code for the following: Fit a
second-degree polynomial to the following data.
X 0 1 2 3 4 5
Y 2.1 7.7 13.6 27.2 40.9 61.1

Scilab Code:
x=[-2,-1,0,1,2];
y=[3 1 1 3 16];
n=length(x);
sx=sum(x);
sy=sum(y);
sx2=sum(x.*x);
sx3=sum(x.^3);
sx4=sum(x.^4);
sx2y=sum(x.*x.*y);
sxy=sum(x.*y);
disp("sum of product of x and y=",sxy)
disp("sum of square of x=",sx2)
disp("sum of cube of x=",sx3)
disp("sum of x^4=",sx4)
disp("sum of x^2*y=",sx2y)
disp("sum of all the x=",sx)
disp("sum of all the y=",sy)
A=[sx2 sx n(1);sx3 sx2 sx;sx4 sx3 sx2]
B=[sy;sxy;sx2y]
sol=A\B
z=[x y x.*x x.^3 x.^4 x.*y x.*x.*y]
disp(z)
a=(sol(1))
b=(sol(2))
c=(sol(3))
xn=-3:0.1:3
yfit=a.*xn.*xn+b*xn+c
disp(a)
disp(b)
disp(c)

Output:

You might also like