Program On Curve Fitting of Straight Line (Y Ax+b)

You are on page 1of 12

Amey Patil

TE-B (31)

Program On curve Fitting of straight line (y=ax+b)

Program
x=input('enter the value of x');
y=input('enter the value of y');
n=length(x);
sumx=0;sumy=0;sumxy=0;sumxx=0;
for i=1:1:n
sumx=sumx+x(i);
sumy=sumy+y(i);
sumxx=sumxx+x(i)*x(i);
sumxy=sumxy+x(i)*y(i);
end
ds=[sumx,n;sumxx,sumx];
da=[sumy,n;sumxy,sumx];
db=[sumx,sumy;sumxx,sumxy];
a=det(da)/det(ds);
b=det(db)/det(ds);
fprintf('\n y=%f*x+%f',a,b);

Output
enter the value of x[1,2,3,4,5,6,7]
enter the value of y[0.5,2.5,2,4,3,6,5.5]

y=0.821429*x+0.071429>>

Solver
>> polyfit(x,y,1)

ans =

0.8214 0.0714

>>
Amey Patil
TE-B (31)

Curve
Amey Patil
TE-B (31)

Program on Gauss Elimination Method

Program
a=input('enter matrix a');
d=input('enter matrix d');
n=length(d);
for i=1:n
for k=i+1:n
f=a(k,i)/a(i,i);
a(k,:)=a(k,:)-f*a(i,:);
d(k)=d(k)-f*d(i);
end
end
x=zeros(n,1);
for i=n:-1:1
x(i)=(d(i)-a(i,i+1:n)*x(i+1:n))/a(i,i);
end
x

Output

enter matrix a[3,2,4;2,1,1;1,3,5]


enter matrix d[7;4;2]

x=

2.2500
-1.1250
0.6250

Solver

a\d

ans =

2.2500
-1.1250
0.6250
Amey Patil
TE-B (31)

Program on Gauss Seidal Method


Program

fx=inline('(5-x2-x3)/4');
fy=inline('(19-x1-2*x3)/6');
fz=inline('(-10-x1-2*x2)/5');
n=input('\n Enter the required number of iterations n= ');
x1=0;
x2=0;
x3=0;
for i=1:1:n
x1=fx(x2,x3);
x2=fy(x1,x3);
x3=fz(x1,x2);
end
fprintf('\n x1=%f',x1);
fprintf('\n x2=%f',x2);
fprintf('\n x3=%f',x3);

Output

Enter the required nimber of iterations n= 5

x1=1.165338
x2=4.288821
x3=-3.948596>>

Solver
[x1,x2,x3]=solve(‘4*x1+x2+x3=5’,’x1+6*x2+2*x3=19’,’-x1-2*x2-5*x3=10’)
x1=1.164944
x2=4.288708
x3=-3.948596
Amey Patil
TE-B (31)

Program On Curve Fitting of Power Equation


(y=ab^x)

Program
x=input(' Enter the values of x = ');
y=input(' Enter the values of y = ');
n=length(x);
sumX=0;sumY=0;sumXY=0;sumXX=0;
for i=1:1:n
Y(i)=log(y(i));
X(i)=x(i);
sumX=sumX+X(i);
sumY=sumY+Y(i);
sumXY=sumXY+X(i)*Y(i);
sumXX=sumXX+X(i)*X(i);
end
ds=[sumX,n;sumXX,sumX];
dA=[sumY,n;sumXY,sumX];
dB=[sumX,sumY;sumXX,sumXY];
A=det(dA)/det(ds);
B=det(dB)/det(ds);
b=exp(A);
a=exp(B);
fprintf('\n y=(%f) * (%f) ^x', a,b);

Output
SR1
Enter the values of x = [1;2;3;4]
Enter the values of y = [4;11;35;100]

y=(1.326650) * (2.948829) ^x

Solver
polyfit(X,Y,1)

ans =
1.0814 0.2827

>> b=exp(A)
b = 2.9488
>> a=exp(B)
a = 1.3266
Amey Patil
TE-B (31)

Curve
Amey Patil
TE-B (31)

Program On Curve Fitting of Power Equation


(y=ax^b)

Program

x=input(' Enter the values of x = ');


y=input(' Enter the values of y = ');
n=length(x);
sumX=0;sumY=0;sumXY=0;sumXX=0;
for i=1:1:n
Y(i)=log(y(i));
X(i)=log(x(i));
sumX=sumX+X(i);
sumY=sumY+Y(i);
sumXY=sumXY+X(i)*Y(i);
sumXX=sumXX+X(i)*X(i);
end
ds=[sumX,n;sumXX,sumX];
dA=[sumY,n;sumXY,sumX];
dB=[sumX,sumY;sumXX,sumXY];
A=det(dA)/det(ds);
B=det(dB)/det(ds);
b=A;
a=exp(B);
fprintf('\ny=(%f)*x^%f', a,b);

Output

Enter the values of x = [25,20,12,9,7,5]


Enter the values of y = [0.22,0.2,0.15,0.13,0.12,0.1]

y=(0.045107)*x^0.491869>>

Solver
>> polyfit(X,Y,1)

ans =
0.4919 -3.0987
>> exp(B)
ans =
0.0451
Amey Patil
TE-B (31)

Curve
Amey Patil
TE-B (31)

Program On Curve Fitting of Exponential Equation


(y=ae^bx)

Program

x=input(' Enter the values of x = ');


y=input(' Enter the values of y = ');
n=length(x);
sumX=0;sumY=0;sumXY=0;sumXX=0;
for i=1:1:n
Y(i)=log(y(i));
X(i)=x(i);
sumX=sumX+X(i);
sumY=sumY+Y(i);
sumXY=sumXY+X(i)*Y(i);
sumXX=sumXX+X(i)*X(i);
end
ds=[sumX,n;sumXX,sumX];
dA=[sumY,n;sumXY,sumX];
dB=[sumX,sumY;sumXX,sumXY];
A=det(dA)/det(ds);
B=det(dB)/det(ds);
b=A;
a=exp(B);
fprintf('\n y=%f*e^(%f*x)',a,b);

Output

Enter the values of x = [2,4,6,8]


Enter the values of y = [25,38,56,84]
y=16.814818*e^(0.201179*x)

Solver
>> polyfit(X,Y,1)
ans =
0.2012 2.8223
>> exp(B)

ans =

16.8148
Amey Patil
TE-B (31)

Curve
Amey Patil
TE-B (31)

Program On Curve Fitting of Quadratic Equation


(y=ax^2 + bx + c)

Program

x=input (' Enter the values of x ') ;

y= input (' Enter the values of y') ;

n= length (x);

sumx=0; sumy=0; sumxx=0;sumxy=0; sumxxx=0; sumxxy =0; sumxxxx=0;

for i=1:1:n

sumx=sumx+x(i);

sumy=sumy+y(i);

sumxx=sumxy+x(i)*x(i);

sumxy=sumxy +x(i)*y(i);

sumxxx=sumxxx + x(i)*x(i)*x(i);

sumxxy=sumxxy +x(i)* x(i)*y(i);

sumxxxx=sumxxxx+x(i)+x(i)*x(i)*x(i);

end

p= [ sumxx, sumx,n;sumxxx,sumxx,sumx;sumxxxx,sumxxx,sumxx ] ;

q=[ sumy;sumxy ;sumxxy ];

v=linsolve (p,q) ;

a=v(1);

b=v(2);

c=v(3);

fprintf ('\n y =%f * x^2 +%f * x + %f ', a , b,c );


Amey Patil
TE-B (31)

Output

Enter the values of x [0.1,0.2,0.3,0.4,0.5]

Enter the values of y[1.4,1.56,1.76,2,2.28]

y =2.000000 * x^2 +1.000000 * x + 1.280000 >>

Solver
> polyfit(x,y,2)

ans =

2.0000 1.0000 1.2800

Curve

You might also like