Department of Physics & Applied Mathematics, PIEAS
BS Physics, Session 2022–26
Sixth Semester, Spring, 2025
PAM416 Introduction to Computational Physics
https://drive.google.com/drive/folders/1oN6UzDhnUpTpBF_OZiZ_GAOHP-0JxrHy?usp=sharing
Solution:Assignment-07(b) Week: (6)
Q.1 Carryout least square curve fitting to the following data to: 𝑦 = 𝑎 0 + 𝑎 1 𝑥 + 𝑎 2 𝑥 2 . Find
out the value of Chi-square, and degrees of freedom for the fitted line.
𝑥 1 2 3 4 5
𝑦 17.3 96.8 287.3 580.1 997.5
Given: 𝑓 (𝑥) = 𝑎 0 + 𝑎 1 𝑥 + 𝑎 2 𝑥 2 for curve fitting, we complete the following table:
𝑘 𝑥 𝑦 𝑥2 𝑥3 𝑥4 𝑥.𝑦 𝑥 2 .𝑦
0 1.0 17.3 1.0 1.0 1.0 17.3 17.3
1 2.0 96.8 4.0 8.0 16.0 193.6 387.2
2 3.0 287.3 9.0 27.0 81.0 861.9 2585.7
3 4.0 580.1 16.0 64.0 256.0 2320.4 9281.6
4 5.0 997.5 25.0 125.0 625.0 4987.5 24937.5
Sum: 15.0 1979.0 55.0 225.0 979.0 8380.7 37209.3
So, we can write the Normal Equations(in Matrix Form) as:
5 15.0 55.0 𝑎 0 1979.0
15.0 55.0 225.0 𝑎 1 = 8380.7
55.0 225.0 979.0 𝑎 2 37209.3
and the solution is: 𝑎 0 = 51.74, 𝑎 1 = −89.10, 𝑎 2 = 55.58
The resulting Least Square Fitted Curve is shown in Fig. (1)
Q.2 Carryout least square curve fitting to the following data to: 𝑦 = 𝐴 𝑥 𝐵 . Find out the value
of Chi-square, and degrees of freedom for the fitted line.
𝑥 1 2 3 4 5
𝑦 17.3 96.8 287.3 580.1 997.5
Given: 𝑓 (𝑥) = 𝛼 𝑥 𝛽 for curve fitting, we complete the following table. The given function
is transformed into a linear form. For that we take log of both sides:
𝑦 = 𝛼𝑥 𝛽
ln(𝑦) = ln(𝛼𝑥 𝛽 )
ln(𝑦) = ln(𝛼) + 𝛽 𝑥; Compare it with St. Line
𝑌 = 𝑐+𝑚𝑥
1
1000 Data
Qadratic Fit
800
600
y(x)
400
200
0
1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
x-data
Figure 1: The Least Square Parabola 𝑓 (𝑥) = 51.74 + 𝑓 ”𝑎1 : .2 𝑓 ” 𝑥55.58 𝑥 2 for the given data.
Comparison shows that the transformed linear form has: 𝑐 = ln 𝛼, and 𝑚 = 𝑢𝛽. We do
Streight line fitting to the transformed data and do the least square line fitting. This yields
values of 𝑐, 𝑚 and we find the values of parameters: 𝛼 = exp(𝑐) and 𝛽 = 𝑚.
𝑘 𝑥 𝑦 𝑙𝑛(𝑥) (𝑙𝑛𝑥)2 𝑙𝑛𝑦 𝑙𝑛𝑥.𝑙𝑛𝑦
0 1.0 17.3 0.0 0.0 2.9 0.0
1 2.0 96.8 0.7 0.5 4.6 3.2
2 3.0 287.3 1.1 1.2 5.7 6.2
3 4.0 580.1 1.4 1.9 6.4 8.8
4 5.0 997.5 1.6 2.6 6.9 11.1
Sum: 4.8 6.2 26.4 29.3
So, we can write the Normal Equations (in Matrix Form) as:
5 4.787 𝛼 26.352
=
4.787 6.200 𝛽 29.323
and the solution is: 𝛼 = 17.22, 𝛽 = 2.53
The resulting Least Square Fitted Curve is shown in Fig. (2)
Q.3 Carryout least square curve fitting to the following data to: 𝑦 = 𝐶 𝑒𝑥 𝑝(−𝑘 𝑥). Find out
the value of Chi-square, and degrees of freedom for the fitted line.
𝑥 1 2 3 4 5
𝑦 187.1 96.8 43.2 22.1 9.7
2
1000 Data
Power-Law Fit
800
600
y(x)
400
200
0
1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
x-data
Figure 2: The Least Square Power-Function 𝑓 (𝑥) = 𝛼 𝑥 𝛽 ; 𝛼 = 17.22; 𝛽 = 2.53 for the given data.
Given: 𝑓 (𝑥) = 𝐶 exp(−𝑘𝑥) for curve fitting, we complete the following table. The given
function is transformed into a linear form. For that we take log of both sides:
𝑦 = 𝐶 exp(−𝑘𝑥)
ln(𝑦) = ln(𝐶 exp(−𝑘𝑥))
ln(𝑦) = ln(𝐶) − 𝑘 𝑥; Compare it with St. Line
𝑌 = 𝑐+𝑚𝑥
Comparison shows that the transformed linear form has: 𝑐 = ln 𝐶, and 𝑚 = −𝑘 𝛽. We do
Streight line fitting to the transformed data and do the least square line fitting. This yields
values of 𝑐, 𝑚 and we find the values of parameters: 𝐶 = exp(𝑐) and 𝑘 = −𝑚.
𝑘 𝑥 𝑦 𝑙𝑛(𝑥) (𝑙𝑛𝑥)2 𝑙𝑛𝑦 𝑙𝑛𝑥.𝑙𝑛𝑦
0 1.0 187.1 1.0 1.0 5.2 5.2
1 2.0 96.8 2.0 4.0 4.6 9.1
2 3.0 43.2 3.0 9.0 3.8 11.3
3 4.0 22.1 4.0 16.0 3.1 12.4
4 5.0 9.7 5.0 25.0 2.3 11.4
Sum: 15.0 55.0 18.9 49.4
So, we can write the Normal Equations (in Matrix Form) as:
5 15.000 𝐶 18.938
=
15.000 55.000 𝑘 49.417
and the solution is: 𝐶 = 406.02, 𝑘 = 0.74
3
The resulting Least Square Fitted Curve is shown in Fig. (3)
200
Data
Exponential Fit
175
150
125
100
y(x)
75
50
25
0
1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
x-data
Figure 3: The Least Square Exponential-Function 𝑓 (𝑥) = 𝐶 exp(−𝑘𝑥); 𝐶 = 406.02; 𝑘 = 0.74 for the
given data.
√
Q.4 Carryout least square curve fitting to the following data to: 𝑦 = 𝑎 𝑥 / 𝑥. Find out the value
of Chi-square, and degrees of freedom for the fitted line.
𝑥 1 2 3 4 5
𝑦 1.72 2.06 2.93 4.37 6.73
Given: 𝑓 (𝑥) = 𝐶 exp(−𝑘𝑥) for curve fitting, we complete the following table. The given
function is transformed into a linear form. For that we take log of both sides:
𝑎𝑥
𝑦=√
𝑥
⇒ ln(𝑦) = 𝑥 ln(𝑎) − 0.5 ln(𝑥)
We get the residue as:
∑︁
𝑅= [ln(𝑦𝑖 ) − 𝑥𝑖 ln(𝑎) + 0.5 ln(𝑥𝑖 )]2 𝑢
𝑖
For least sqare fitting, we minimize 𝑅 as:
𝜕𝑅 ∑︁ −𝑥
= 2 [ln(𝑦𝑖 ) − 𝑥𝑖 ln(𝑎) + 0.5 ln(𝑥𝑖 )]2
𝑖
=0
𝜕𝑎 𝑖 𝑎
(P P )
𝑖 𝑥𝑖 ln(𝑦 𝑖 ) + 0.5 𝑖 𝑥𝑖 ln(𝑥𝑖 )
ln(𝑎) = P 2
𝑖 𝑥𝑖
4
So, the value of parameter 𝑎 can easily be found by computing the sums on the right hand
side.
𝑘 𝑥 𝑦 𝑥 𝑙𝑛(𝑥) 𝑥2 𝑥 𝑙𝑛𝑦
0 1.0 187.1 0.0 1.0 0.5
1 2.0 96.8 1.4 4.0 1.4
2 3.0 43.2 3.3 9.0 3.2
3 4.0 22.1 5.5 16.0 5.9
4 5.0 9.7 8.0 25.0 9.5
Sum: 15.0 17.8 18.3 55.0 20.6
and by using the sum values, the solution is: 𝑎 = 1.71856.
The resulting Least Square Fitted Curve is shown in Fig. (4)
7
Data
Fit
6
4
y(x)
0
1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
x-data
√
Figure 4: The Least Square Function 𝑓 (𝑥) = 𝑎 𝑥 / 𝑥; 𝑎 = 1.71856 for the given data.
Q.5 Repeat Q.2 question above, and solve by writing a computer program that carries out
least square curve fitting to the given data, and prints the slope and intercept values for
the fitted line. Also, the program should show the results graphically.
5
Q.5: Least Square Power-Law Fit
1 %--------------------------------------------------------------
2 import numpy as np
3 import matplotlib.pyplot as plt
4
5 xn = np.array([1.,2.,3.,4.,5.])
6 yn = np.array([17.3, 96.8,287.3,580.1,997.5])
7
8 x = np.log(xn); y = np.log(yn)
9 n = len(x)
10 sx = np.sum(x); sx2 = np.sum(x**2);
11 sy = np.sum(y); sxy = np.sum(x*y);
12
13 A = np.array([[n,sx],[sx,sx2]])
14 b = np.array([[sy],[sxy]])
15 soln = np.linalg.solve(A,b)
16 a0 = soln.flatten()[0]
17 a1 = soln.flatten()[1]
18 alpha = np.exp(a0); beta = a1
19
20 x1 = np.linspace(xn[0],xn[-1],17)
21 y1 = alpha*x1**beta
22
23 plt.figure(figsize=(7,5))
24 plt.plot(xn,yn,'or',markersize=9)
25 plt.plot(x1,y1,'-b')
26 plt.legend(['Data','Power-Law Fit'])
27 plt.xlabel('x-data'); plt.ylabel('$y(x)$')
28 plt.grid(); plt.show()
29 %--------------------------------------------------------------