Program On Curve Fitting of Straight Line (Y Ax+b)
Program On Curve Fitting of Straight Line (Y Ax+b)
Program On Curve Fitting of Straight Line (Y Ax+b)
TE-B (31)
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
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
x=
2.2500
-1.1250
0.6250
Solver
a\d
ans =
2.2500
-1.1250
0.6250
Amey Patil
TE-B (31)
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
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
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
Output
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
Output
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
n= length (x);
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);
sumxxxx=sumxxxx+x(i)+x(i)*x(i)*x(i);
end
p= [ sumxx, sumx,n;sumxxx,sumxx,sumx;sumxxxx,sumxxx,sumxx ] ;
v=linsolve (p,q) ;
a=v(1);
b=v(2);
c=v(3);
Output
Solver
> polyfit(x,y,2)
ans =
Curve