Tutorial 4
Curve Fitting
Duration: 2 hours
1. Write a computer program for fitting of a straight line using method of least squares.
Solution: Scilab program
// Display mode
mode(0);
// Display warning for floating point exception
ieee(1);
// Method of least squares// Fitting a straight line
clc
clear
xdata1 = input("Enter the x data =")
ydata1 = input("Enter the y data =")
[M,N] = size(xdata1);
for i = 1:N
xdata(i)=log(xdata1(i));
ydata(i)=log(ydata1(i));
end
sum_x = 0;
sum_y = 0;
sum_xx = 0;
sum_xy = 0;
for i = 1:N
sum_x = sum_x+xdata(i);
sum_y = sum_y+ydata(i);
sum_xx = sum_xx+xdata(i)^2;
sum_xy = sum_xy+xdata(i)*ydata(i);
end;
A = [N,sum_x;sum_x,sum_xx]
B = [sum_y,sum_xy]'
X = A\B
disp("Output results")
disp("coefficient a0 =")
a0 = exp(X(1))
disp("coefficient a1 =")
a1 = X(2)
for i = 1:N
ydata_cal(1,i) = a0*xdata1(i)^a1;
end;
output = [xdata1;ydata1;ydata_cal]'
scf(1);clf;
plot(xdata1,ydata1,"k*")
xlabel("x")
ylabel("y")
set(gca(),"auto_clear","off")
plot(xdata1,ydata_cal,"k")
Matlab program
% Method of least squares
% Fitting a straight line
clc
clear
xdata = input('Enter the x data =')
ydata = input('Enter the y data =')
[M,N]= size(xdata);
sum_x = 0;
sum_y = 0;
sum_xx = 0;
sum_xy = 0;
for i = 1:N
sum_x = sum_x+xdata(i);
sum_y = sum_y+ydata(i);
sum_xx = sum_xx+xdata(i)^2;
sum_xy = sum_xy+xdata(i)*ydata(i);
end
A = [N sum_x; sum_x sum_xx]
B = [sum_y sum_xy]'
X = A\B
disp('Output results')
disp('coefficient a0 =')
a0 = X(1)
disp('coefficient a1 =')
a1 = X(2)
for i = 1:N
ydata_cal(i) = a0+a1*xdata(i);
end
output=[xdata; ydata; ydata_cal]'
figure(1)
plot(xdata, ydata,'k*')
xlabel('x')
ylabel('y')
hold on
plot(xdata,ydata_cal,'k')
% Statistical Analysis
disp('residual sum of squares errors Sr')
sum_Sr = 0;
for i = 1:N
sum_Sr = sum_Sr+(ydata(i)-ydata_cal(i))^2;
end
Sr = sum_Sr
disp('Standard deviation of the regression line Sy/x')
Sy_x = sqrt(Sr/(N-2))
disp('Arithmatic mean ')
ym = sum_y/N
disp('Total sum of squares error St')
sum_St = 0;
for i = 1:N
sum_St = sum_St + (ydata(i)-ym)^2;
end
St = sum_St
disp('Coefficient of determination r2')
r2 = (St-Sr)/St
disp('Correlation coefficient r')
r = sqrt(r2
2. Write a general program for the Lagrange interpolation method.
Solution
Scilab program
// Display mode
mode(0);
// Display warning for floating point exception
ieee(1);
// Lagrange Interpolation
clc
clear
disp("Input data")
x_data = input("Enter the x data =")
fx_data = input("Enter the f(x) data =")
x = input("Enter the value of x in which the f(x) value to be calculated =")
[nr,nc] = size(x_data);
// Lagrange coefficient polynomial
L = zeros(nc,nc);
for k = 1:nc
Lnu = 1;
Lde = 1;
for j = 1:nc
if j~=k then
Lnu = Lnu*(x-x_data(j));
Lde = Lde*(x_data(k)-x_data(j));
end;
end;
L(nc,k) = Lnu/Lde;
end;
disp("Lagrange coefficient polynomial")
L
// Lagrange polynomial
sum_px = 0;
for k = 1:nc
sum_px = sum_px +fx_data(k)*L(nc,k);
end;
disp("Calculated function value")
P_x = sum_px
Matlab program
Main program
% Lagrange Interpolation
% Main program
clc
clear
disp('Input data')
x_data = input('Enter the x data =')
fx_data = input('Enter the f(x) data =')
x = input('Enter the value of x in which the f(x) value to be calculated =')
% Solver
[Px, L,L1,C]=Lagrange_interpl(x_data,fx_data,x);
disp('Output')
%disp('Lagrange interpolating coefficients are =')
n = length(x_data);
L = L(n,:)
disp('Calculation f(x) value using Lagrange interpolating polynomial is =')
Px
disp('Coefficients of Lagrange interpolating polynomial is')
C
function [P_x, L,L1,C] = Lagrange_interpl(x,fx,xv)
[nr, nc] = size(x);
% Lagrange coefficient polynomial
L = zeros(nc,nc);
for k = 1:nc
Lnu = 1;
Lde = 1;
for j = 1:nc
if j ~= k
Lnu = Lnu*(xv-x(j));
Lde= Lde*(x(k)-x(j));
end
end
L(nc,k) = Lnu/Lde;
end
% Lagrange polynomial
sum_px = 0;
for k = 1:nc
sum_px = sum_px +fx(k)*L(nc,k);
end
P_x = sum_px ;
%
L1 = zeros(nc,nc);
for k = 1:nc
V = 1;
for j = 1:nc
if j ~= k
V = conv(V, poly(x(j)))/(x(k)-x(j));
end
end
L1(k,:) = V;
end
L1;
C = fx*L1;
3. The relationship between the pressure P and volume V of the air in a cylinder during the upstroke of a
piston in an air compressor can be expressed as
PV k C
where k and c are constants. During a compression test, the following data are taken.
P (mm Hg)
760
1140
1520
2280
3040
3800
V (cm3)
48.3
37.4
31.3
24.1
20.0
17.4
Estimate the values of k and C using methods of least squares.
Solution:
Linearized equation is
ln P k ln V ln C
y a1 x a 0
k a1
C exp a 0
Ans:
C = 3.3985×105
k = 1.573
PV 1.573 3.3985 105
Exercises
1. Write a general program for the Newton interpolation method.
2. The velocity distribution of a fluid near a flat surface is given below.
x
0.1
0.3
0.5
0.7
0.9
v
0.72
1.81
2.73
3.47
3.98
Where, x is the distance from the surface (cm) and v is the velocity (cm s-1). Use Newton’s interpolation
formula to determine the velocity at x = 0.2, 0.6 and 0.8.
3. The shear stresses (kPa) of nine specimens taken at various depths in a clay stratum are listed below.
Find the shear stress at a depth of 7.2 m using (a) Newton’s interpolation, (b) Lagrange interpolation,
and (c) natural cubic splines.
Depth
m
Stress
kPa
1.9
3.1
4.2
5.1
5.8
6.9
8.1
9.3
10.0
14.4
28.7
19.2
43.1
33.5
52.7
71.8
62.2
76.6
4. The current in a wire is measured with great precision as function of time. Determine the current i at t
= 0.2 using (a) Lagrange interpolation, and (b) Newton interpolation formulas.
t
0
0.125
0.25
0.375
0.5
i
0
6.24
7.75
4.85
0
5. The rate at which a substance passes through a semi permeable membrane is determined by the
diffusing D (cm2/s) of the gas. The coefficient D varies with the membrane temperature T (K) according
to the Arrhenius equation
E
D D0 exp
RT
where D0 is the preexponential factor, E is the activation energy for diffusion, R is gas law constant
(1.987 cal/mol K).
Diffusivities of SO2 in a fluoro Silicone rubber tube are measured at several temperatures, with the
following results.
T (K)
347.0
374.2
396.2
420.7
447.7
471.2
D(cm2/s) (×106)
1.34
2.5
4.55
8.52
14.07
19.99
Find the values for D0 and E using method of least squares.
6. The friction factor f for the turbulent flow in a smooth air duct is calculated from the Blasius equation,
which is given by
b
f aN Re
NRe
2300
2900
3500
4100
4700
5300
5900
6500
7100
7700
f
0.0114
0.0108
0.0103
0.0099
0.0096
0.0093
0.0090
0.0088
0.0086
0.0084
Compute the coefficients involved in the Blasius equation using linear regression.
© Copyright 2025 Paperzz