0% found this document useful (0 votes)
3 views

Assignment 8

The document contains Python code for simulating and plotting two different mathematical models using the Euler method: one for velocity under a force and another for radioactive decay. It includes parameters, initialization of variables, and results visualization with matplotlib. Additionally, it features a Lagrange interpolation function and a central difference method for calculating derivatives.

Uploaded by

Sougata Halder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assignment 8

The document contains Python code for simulating and plotting two different mathematical models using the Euler method: one for velocity under a force and another for radioactive decay. It includes parameters, initialization of variables, and results visualization with matplotlib. Additionally, it features a Lagrange interpolation function and a central difference method for calculating derivatives.

Uploaded by

Sougata Halder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment 8

March 20, 2025

[41]: import numpy as np


import matplotlib.pyplot as plt

# Parameters
a = 10.0
b = 1.0
dt = 0.01 # time step
T = 10.0 # total time for simulation
N = int(T / dt) # number of steps

# Initialize time and velocity arrays


time = np.linspace(0, T, N + 1)
v = np.zeros(N + 1) # starting with v(0)=0

# Euler method iteration


for n in range(N):
v[n+1] = v[n] + dt * (a - b * v[n])

# Plotting the results


plt.figure(figsize=(4, 3))
plt.plot(time, v, label="Euler Approximation")

# Adding the terminal velocity line


v_terminal = a / b
plt.axhline(y=v_terminal, color='r', linestyle='--', label=f'Terminal Velocity␣
↪= {v_terminal}')

# Adding labels and title


plt.xlabel("Time")
plt.ylabel("Velocity")
plt.title("Euler Method: Solving dv/dt = a - b*v")
plt.legend()
plt.grid(True)
plt.show()

1
[36]: import numpy as np
import matplotlib.pyplot as plt

# Parameters
tau = 1.0
dt = 0.01 # time step (s)
T = 5.0 # total time (s)
steps = int(T/dt)

# Initialize arrays
time = np.linspace(0, T, steps+1)
N_A = np.zeros(steps+1)
N_B = np.zeros(steps+1)

# Initial conditions: For example, N_A = 100, N_B = 0


N_A[0] = 100
N_B[0] = 0

# Euler integration
for i in range(steps):
dN_A = (N_B[i] - N_A[i]) / tau
dN_B = (N_A[i] - N_B[i]) / tau
N_A[i+1] = N_A[i] + dt * dN_A
N_B[i+1] = N_B[i] + dt * dN_B

# Adding equilibrium line and plot size


plt.figure(figsize=(4, 3))

2
N_avg = 50
plt.axhline(y=N_avg, color='r', linestyle='--', label=f'equilibrium')

# Plotting results
plt.plot(time, N_A, label=r'$N_A(t)$')
plt.plot(time, N_B, label=r'$N_B(t)$')
plt.xlabel('Time (s)')
plt.ylabel('Number of Nuclei')
plt.title('Radioactive Decay: Two-Step Process')
plt.legend()
plt.grid(True)
plt.show()

[26]: import numpy as np

# Given data points


z = np.array([0, 1.25, 3.75])
T = np.array([13.5, 12, 10])

def Lagrange(x, y, xp):


yp = 0
for xi, yi in zip(x,y):
yp += yi * np.prod((xp - x[x != xi])/(xi - x[x != xi]))
return yp

def central_diff(f, x, h = 1e-3):


return (f(x+h) - f(x-h)) / (2*h)

3
# Create a one-variable function
f = lambda x: Lagrange(z, T, x)

# Derivative at z = 0
dTdz_0 = central_diff(f, 0, h=1e-3)

# Compute heat flux


q = -k * rho * C * dTdz_0
print("Heat flux q =", round(q,4), "W/m^2")

Heat flux q = 0.7056 W/m^2

You might also like