Numrcalallinone.ipynb - Colab
Numrcalallinone.ipynb - Colab
# Jacobi method
def jacobi(A, b, x0, tol, max_iterations):
n = len(A)
x = x0.copy()
x_new = x0.copy()
x = x_new.copy()
Example:
# Initial guess x0
x0 = np.zeros(len(b))
Table sulution
Variable Solution
0 x1 4.910714
1 x2 4.642857
2 x3 3.660714
# Line chart
plt.figure(figsize=(8, 6))
plt.plot(['x1', 'x2', 'x3'], solution, marker='o', linestyle='-', color='b')
plt.title('Jacobi Method Solution - Line Chart')
plt.xlabel('Variables')
plt.ylabel('Values')
plt.grid(True)
plt.show()
# Bar chart
plt.figure(figsize=(8, 6))
plt.bar(['x1', 'x2', 'x3'], solution, color='orange')
plt.title('Jacobi Method Solution - Bar Chart')
plt.xlabel('Variables')
plt.ylabel('Values')
plt.show()
# Violin chart
plt.figure(figsize=(8, 6))
sns.violinplot(data=solution, inner="point", linewidth=2)
plt.title('Jacobi Method Solution - Violin Chart')
plt.show()
/usr/local/lib/python3.10/dist-packages/seaborn/_base.py:949: FutureWarning: When grouping with a length-1 list-like, you will need to p
data_subset = grouped_data.get_group(pd_key)
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
# Gauss-Seidel method
def gauss_seidel(A, b, x0, tol, max_iterations):
n = len(A)
x = x0.copy()
iteration_data = []
iteration_data.append(x.copy())
# Initial guess x0
x0 = np.zeros(len(b))
x1 x2 x3 Iteration
0 3.750000 3.437500 3.359375 1
1 4.609375 4.492188 3.623047 2
2 4.873047 4.624023 3.656006 3
3 4.906006 4.640503 3.660126 4
4 4.910126 4.642563 3.660641 5
5 4.910641 4.642820 3.660705 6
6 4.910705 4.642853 3.660713 7
7 4.910713 4.642857 3.660714 8
8 4.910714 4.642857 3.660714 9
9 4.910714 4.642857 3.660714 10
10 4.910714 4.642857 3.660714 11
11 4.910714 4.642857 3.660714 12
12 4.910714 4.642857 3.660714 13
13 4.910714 4.642857 3.660714 14
/usr/local/lib/python3.10/dist-packages/seaborn/_base.py:949: FutureWarning: When grouping with a length-1 list-like, you will need to p
data_subset = grouped_data.get_group(pd_key)
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
return diff_table
return fwd_table
return result
# Example points
x_vals = np.array([1, 2, 3, 4])
y_vals = np.array([1, 8, 27, 64])
/usr/local/lib/python3.10/dist-packages/seaborn/_base.py:949: FutureWarning: When grouping with a length-1 list-like, you will need to p
data_subset = grouped_data.get_group(pd_key)
Backward differnce
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
return diff_table
central difference
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
return diff_table
return result
simpson 1/3
Simpson’s 1/3 method is a numerical integration technique used to approximate the value of a definite integral. It uses parabolic segments to
approximate the area under a curve. Simpson’s 1/3 rule is applied when the number of intervals is even.
ℎ = 𝑏 − 𝑎 𝑛 h= n b−ais the width of each interval. 𝑛 n is the number of intervals (must be even).
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
h = (x[-1] - x[0]) / n
return integral
plt.figure(figsize=(8, 6))
plt.plot(x_interp, y_interp, label='f(x) = x^2', color='blue')
plt.scatter(x_vals, y_vals, label='Sampled Points', color='red')
plt.fill_between(x_interp, y_interp, color='skyblue', alpha=0.3)
plt.title("Simpson's 1/3 Method - Line Chart")
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.grid(True)
plt.show()
# Mesh chart (3D plot of the function values and x)
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(x_vals, y_vals, np.zeros_like(x_vals), color='cyan')
plt.show()
simpson 3/8
The Simpson's 3/8 rule is another numerical integration technique similar to Simpson's 1/3 rule but is used when the number of intervals is a
multiple of 3. The formula for Simpson's 3/8 rule is:
ℎ = 𝑏 − 𝑎 𝑛 h= n b−ais the width of each interval. 𝑛 n is the number of intervals (must be a multiple of 3).
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
h = (x[-1] - x[0]) / n
return integral
plt.figure(figsize=(8, 6))
plt.plot(x_interp, y_interp, label='f(x) = x^2', color='blue')
plt.scatter(x_vals, y_vals, label='Sampled Points', color='red')
plt.fill_between(x_interp, y_interp, color='skyblue', alpha=0.3)
plt.title("Simpson's 3/8 Method - Line Chart")
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.grid(True)
plt.show()
plt.show()
Eulers method
Problem: Solving the ODE 𝑑 𝑦 𝑑 𝑥 = 𝑓 ( 𝑥 , 𝑦 ) dx dy=f(x,y) Let’s take an example of the differential equation 𝑑 𝑦 𝑑 𝑥 = 𝑥 + 𝑦 dx dy=x+y with the
initial condition 𝑦 ( 0 ) = 1 y(0)=1, and we'll solve it over the interval [ 0 , 2 ] [0,2].
Step-by-Step Guide Step 1: Euler's Method Implementation Euler's method formula is:
ℎ h is the step size. 𝑦 𝑖 + 1 y i+1is the next value of 𝑦 y. 𝑓 ( 𝑥 𝑖 , 𝑦 𝑖 ) f(x i,y i) represents the derivative at ( 𝑥 𝑖 , 𝑦 𝑖 ) (x i,y i).
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = x0
y = y0
plt.show()
Heuns method
Heun's method, also known as the improved Euler method, is a numerical technique used to solve ordinary differential equations (ODEs). It's
more accurate than Euler's method because it averages the slopes at the beginning and end of each step.
Formula for Heun's Method For the differential equation 𝑑 𝑦 𝑑 𝑥 = 𝑓 ( 𝑥 , 𝑦 ) dx dy=f(x,y), the Heun's method is given by:
𝑦 𝑖 + 1 = 𝑦 𝑖 + ℎ 2 ( 𝑓 ( 𝑥 𝑖 , 𝑦 𝑖 ) + 𝑓 ( 𝑥 𝑖 + 1 , 𝑦 predict ) ) y i+1=y i
ℎ h is the step size. 𝑦 predict = 𝑦 𝑖 + ℎ ⋅ 𝑓 ( 𝑥 𝑖 , 𝑦 𝑖 ) y predict=y i+h⋅f(x i,y i) is the predicted value of 𝑦 y at 𝑥 𝑖 + 1 x i+1. Example: Solving 𝑑 𝑦 𝑑 𝑥 =
𝑥 + 𝑦 dx dy=x+y Let's solve the differential equation 𝑑 𝑦 𝑑 𝑥 = 𝑥 + 𝑦 dx dy=x+y with an initial condition 𝑦 ( 0 ) = 1 y(0)=1, and step size ℎ = 0.1
h=0.1 over the interval [ 0 , 2 ] [0,2].
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = x0
y = y0
plt.show()
Double-click (or enter) to edit
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = x0
y = y0
y = y + h * k2
x = x + h
x_vals.append(x)
y_vals.append(y)
plt.show()
Iterative Output (x, y values):
x y
0 0.0 1.000000
1 0.1 1.110000
2 0.2 1.242050
3 0.3 1.398465
4 0.4 1.581804
5 0.5 1.794894
6 0.6 2.040857
7 0.7 2.323147
8 0.8 2.645578
9 0.9 3.012364
10 1.0 3.428162
11 1.1 3.898119
12 1.2 4.427921
13 1.3 5.023853
14 1.4 5.692857
15 1.5 6.442607
16 1.6 7.281581
17 1.7 8.219147
18 1.8 9.265658
19 1.9 10.432552
20 2.0 11.732470
Runge kutta 3rd order
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = x0
y = y0
y = y + (h / 6) * (k1 + 4 * k2 + k3)
x = x + h
x_vals.append(x)
y_vals.append(y)
plt.show()
Iterative Output (x, y values):
x y
0 0.0 1.000000
1 0.1 1.110167
2 0.2 1.242418
3 0.3 1.399076
4 0.4 1.582704
5 0.5 1.796136
6 0.6 2.042505
7 0.7 2.325272
8 0.8 2.648261
9 0.9 3.015699
10 1.0 3.432257
11 1.1 3.903096
12 1.2 4.433922
13 1.3 5.031036
14 1.4 5.701406
15 1.5 6.452729
16 1.6 7.293511
17 1.7 8.233155
18 1.8 9.282047
19 1.9 10.451669
20 2.0 11.754707
Rungee kutta 4th order
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = x0
y = y0
y = y + (h / 6) * (k1 + 2 * k2 + 2 * k3 + k4)
x = x + h
x_vals.append(x)
y_vals.append(y)
plt.show()
Iterative Output (x, y values):
x y
0 0.0 1.000000
1 0.1 1.110342
2 0.2 1.242805
3 0.3 1.399717
4 0.4 1.583648
5 0.5 1.797441
6 0.6 2.044236
7 0.7 2.327503
8 0.8 2.651079
9 0.9 3.019203
10 1.0 3.436559
11 1.1 3.908327
12 1.2 4.440228
13 1.3 5.038586
14 1.4 5.710391
15 1.5 6.463368
16 1.6 7.306053
17 1.7 8.247881
18 1.8 9.299278
19 1.9 10.471769
20 2.0 11.778090
Runge kutta 5th order
runge_kutta_5th_order: This function implements the fifth-order Runge-Kutta method to solve the ODE. The method calculates six slopes 𝑘 1 , 𝑘
2 , 𝑘 3 , 𝑘 4 , 𝑘 5 , k1,k2,k3,k4,k5, and 𝑘 6 k6 at each step. f: This function defines the differential equation 𝑑 𝑦 𝑑 𝑥 = 𝑥 + 𝑦 dx dy=x+y.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = x0
y = y0
y = y + (h / 6) * (k1 + 4 * k4 + k5)
x = x + h
x_vals.append(x)
y_vals.append(y)
matplotlib.axes._base._AxesBase.set_xlabel
def set_xlabel(xlabel, fontdict=None, labelpad=None, *, loc=None, **kwargs)
/usr/local/lib/python3.10/dist-packages/matplotlib/axes/_base.py
Set the label for the x-axis.
Parameters
----------
xlabel : str
h l b l t t
Double-click (or enter) to edit
The Classical Runge-Kutta method, commonly known as the fourth-order Runge-Kutta method, is widely used to solve ordinary differential
equations (ODEs) due to its balance between accuracy and computational efficiency. Below, I'll guide you through implementing the Classical
Runge-Kutta method in Google Colab.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = x0
y = y0
y = y + (h / 6) * (k1 + 2 * k2 + 2 * k3 + k4)
x = x + h
x_vals.append(x)
y_vals.append(y)
plt.show()
Iterative Output (x, y values):
x y
0 0.0 1.000000
1 0.1 1.110342
2 0.2 1.242805
3 0.3 1.399717
4 0.4 1.583648
5 0.5 1.797441
6 0.6 2.044236
7 0.7 2.327503
8 0.8 2.651079
9 0.9 3.019203
10 1.0 3.436559
11 1.1 3.908327
12 1.2 4.440228
13 1.3 5.038586
14 1.4 5.710391
15 1.5 6.463368
16 1.6 7.306053
17 1.7 8.247881
18 1.8 9.299278
19 1.9 10.471769
20 2.0 11.778090
solve higher order ode with classical runge kutta method
𝑑 2 𝑦 𝑑 𝑥 2 = − 𝑦 dx 2
𝑑 𝑦 1 𝑑 𝑥 = 𝑦 2 dx dy 1
=y 2
𝑑 𝑦 2 𝑑 𝑥 = − 𝑦 1 dx dy 2
=−y 1
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = x0
y1 = y0[0]
y2 = y0[1]
x_vals.append(x)
y_vals.append(y1)
y2_vals.append(y2)
plt.show()
Iterative Output (x, y1, y2 values):
x y1 y2
0 0.0 1.000000 0.000000
1 0.1 0.995004 -0.099833
2 0.2 0.980067 -0.198669
3 0.3 0.955337 -0.295520
4 0.4 0.921061 -0.389418
.. ... ... ...
97 9.7 -0.962366 0.271753
98 9.8 -0.930429 0.366471
99 9.9 -0.889194 0.457528
100 10.0 -0.839075 0.544014
101 10.1 -0.780573 0.625064
𝑑 3 𝑦 𝑑 𝑥 3 = − 𝑦 dx 3
d 3 y=−y We can convert this into a system of first-order equations by introducing two new variables:
Let 𝑦 3 = 𝑑 2 𝑦 𝑑 𝑥 2 y 3= dx 2
d 2 y
𝑑 𝑦 1 𝑑 𝑥 = 𝑦 2 dx dy 1
=y 2
𝑑 𝑦 2 𝑑 𝑥 = 𝑦 3 dx dy 2
=y 3
𝑑 𝑦 3 𝑑 𝑥 = − 𝑦 1 dx dy 3
=−y 1
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
x = x0
y1 = y0[0]
y2 = y0[1]
y3 = y0[2]
x_vals.append(x)
y1_vals.append(y1)
y2_vals.append(y2)
y3_vals.append(y3)
# Line chart showing Runge-Kutta method approximation for y1, y2, and y3
plt.figure(figsize=(10, 6))
plt.plot(x_vals, y1_vals, label='y1 (Position)', color='blue', marker='o')
plt.plot(x_vals, y2_vals, label='y2 (Velocity)', color='red', marker='x')
plt.plot(x_vals, y3_vals, label='y3 (Acceleration)', color='green', marker='s')
plt.title("Fifth-Order Runge-Kutta Method - Line Chart")
plt.xlabel('x')
plt.ylabel('y')
plt.grid(True)
plt.legend()
plt.show()
plt.show()