You are on page 1of 2

Name: Aditya Kumar

Roll no.: 3914

Group: A+B

Scilab Code 6

Aim: Least Square fitting of data points in a


line
1 2 3 4 5 6 7 8
-0.8 -0.6 -0.5 -0.1 0 0.3 0.5 0.7
clc
clear

x=[1,2,3,4,5,6,7,8]
y=[-0.8,-0.6,-0.5,-0.1,0,0.3,0.5,0.7]
$x=0
$y=0
$xy=0
$x2=0
n=length(x)
m=length(y)
if n~=m then
disp("Dimensions of x and y are not same, ABORTING PROGRAM !")
end
$x=sum(x)
$y=sum(y)
$xy=sum(x.*y)
$x2=sum(x^2)
v=[$x2 $x; $x n]
w=[$xy; $y]
r=inv(v)*w
disp(r)
c=r(1)
d=r(2)
disp('The least square fitting curve is= '+string(c)+'*x'+string(d))
plot(x,y,'or')
y=c*(x)+d
plot(x,y)
legend(['Data points','Least square fitted curve'])
xtitle('LEAST SQUARE FITTING','x-axis','y-axis')

Output:

You might also like