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

Ilovepdf Merged (2)

The document discusses various mathematical models including growth and decay models, differential equations, and wave and heat equations, all implemented using Python. It provides equations, source code, and explanations for plotting and solving these models. Each section emphasizes the importance of visualizing solutions to understand system behavior and make predictions.

Uploaded by

naviff26
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)
17 views

Ilovepdf Merged (2)

The document discusses various mathematical models including growth and decay models, differential equations, and wave and heat equations, all implemented using Python. It provides equations, source code, and explanations for plotting and solving these models. Each section emphasizes the importance of visualizing solutions to understand system behavior and make predictions.

Uploaded by

naviff26
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/ 17

6.

Growth and Decay Model (Exponential case)

Aim:To plot growth and decay model and to predict the change in quantity over
time .
Programming language used :Python

Mathematical concept

Growth and decay models are mathematical representations used to describe and
predict the changes in quantity over time .

Exponential Growth Model:

The general form of an exponential growth model is given by the equation:


P(t)=P(0).e^(rt) where

P(t) is the quantity at time


P(0) is the initial quantity.

r is the growth rate per time period.


t is the time elapsed.

Exponential Decay Model:

The general form of an exponential decay model is given by the equation:


P(t)=P(0).e^(-rt)

P(t) is the quantity at time


P(0) is the initial quantity.

r is the decay rate per time period.

t is the time elapsed.


Source code

import numpy as np

import matplotlib.pyplot as plt

time = np.arange(-2, 2, 0.0001)

constant=float(input("Enter the constant:"))

amplitude_grow = constant * np.exp(time)

amplitude_decay = constant * np.exp(-time)

plt.plot(time, amplitude_grow, time, amplitude_decay)

plt.title('Exponential Curve', color='b')

plt.xlabel('Time' + r'$\rightarrow$')

plt.ylabel('Amplitude' + r'$\rightarrow$')

plt.legend(['Growing Exponential', 'Decaying Exponential'])

plt.grid()

plt.axhline(y=0, color='k')

plt.axvline(x=0, color='k')

plt.show()

Input

Enter the constant:0.8


Output

Uses:
• By analyzing the plotted growth or decay model, one
can make predictions about the future behavior of a
system.
• It help in understanding how a quantity increases over
time and how a quantity decreases over time.
7.Solution of Differential Equation

Aim: To solve the differential equation and plotting the solution


Programming language used:Python

Mathematical concept

A differential equation is an equation that relates a function and its derivatives. The solution is
essentially the function that, when substituted into the differential equation, makes the equation
true.

Differential equations can be categorized into ordinary differential equations (ODEs) and partial
differential equations (PDEs), depending on whether the unknown function depends on one or more
independent variables.

The process of finding a solution to a differential equation can vary based on the type of differential
equation and its complexity. Some differential equations can be solved analytically, meaning an
explicit formula for the solution can be obtained. Others may require numerical methods for
approximation.

Source code

import numpy as np

import matplotlib.pyplot as plt

from scipy.integrate import odeint

# Get user input for the ODE function as a string

ode_function_str = input("Enter the ODE function as a Python expression : ")

# Define the ODE function using eval()

def model(y, t):

dydt = eval(ode_function_str)

return dydt

# Get input for the initial condition

y0 = float(input("Enter the initial condition (y0): "))


# Get input for the time points

t_start = float(input("Enter the start time: "))

t_end = float(input("Enter the end time: "))

num_points = int(input("Enter the number of time points: "))

t = np.linspace(t_start, t_end, num_points)

# Solve the ODE

y = odeint(model, y0, t)

# Plot the solution

plt.plot(t, y, label='y(t)')

plt.title("solution of differential equation")

plt.xlabel('Time')

plt.ylabel('y(t)')

plt.legend()

plt.show()

Input

Enter the ODE function as a Python expression : -y

Enter the initial condition (y0): 1.0

Enter the start time: 0

Enter the end time: 20

Enter the number of time points: 100


Output

Uses:
• Solutions to differential equations help in understanding the
behaviour of dynamic systems. They provide insights into how
quantities change with respect to each other and to time.
• Solutions to differential equations often provide deep insights into
the underlying mathematical structure of a system.
9.SECOND ORDER SOLUTION FAMILY OF DIFFERENTIAL
EQUATION
Aim:To plot the second order solution family of Differential equation.

Programming language used :Python

Mathematical concept

Second-order solution family refers to a set of solutions that satisfy a second-order


differential

equation.

A general second-order linear homogeneous ordinary differential equation (ODE) is


of the

form:ay”+by’+cy=0 ,where a,b and c are constants, and y is the dependent


variable with respect to

the independent variable t.

The general solution to this second-order ODE is expressed in terms of a solution


family, which

typically involves two arbitrary constants. The solution has the form :
y(t)=C1y1(t)+C2y2(t)

The nature of the solution depends on the roots of the characteristic equation
associated with the

differential equation,ie aλ^2+bλ+c=0where λ is a variable. The roots of this


characteristic

equation determine the types of solutions and behaviour of the second-order ODE.

(i)Distinct real roots (λ1 not equal to λ2)

y(t)=C1e^(λ1t)+C2e^(λ2t)

(ii)A real double roots (λ1=λ2)

y(t)=(C1+C2t)e^(λ1t)

(iii)Complex conjugate roots(λ1, λ2=α ± βi)

y(t)=e^(αt)(C1cos(βt)+C2sin(βt))
SOURCE CODE

import numpy as np

import matplotlib.pyplot as plt

# Define the time values

t = np.linspace(0, 5, 100)

# Define the solution family function

def second_order_solution(t, C1, C2):

return (C1 + C2 * t) * np.exp(-t)

# Get input for C1_values and C2_values

C1_input = input("Enter values for C1 separated by commas (e.g., 1, 0, -1): ")

C2_input = input("Enter values for C2 separated by commas (e.g., 1, 0, -1): ")

# Convert input strings to lists of floats

C1_values = [float(value) for value in C1_input.split(',')]

C2_values = [float(value) for value in C2_input.split(',')]

# Plotting the solution family for input constants

plt.figure(figsize=(8, 6))

for C1, C2 in zip(C1_values, C2_values):

y = second_order_solution(t, C1, C2)

plt.plot(t, y, label=f'C1={C1}, C2={C2}')

plt.title('Second Order DE Solution Family')

plt.xlabel('Time (t)')

plt.ylabel('y(t)')

plt.legend()

plt.grid(True)

plt.show()
Input

Enter values for C1 separated by commas (e.g., 1, 0, -1): 1,2,0

Enter values for C2 separated by commas (e.g., 1, 0, -1): 1,3,2

Output

Uses:

• Plotting the second-order solution family of a differential equation

(DE) can provide valuable insights into the behaviour of the system

described by the DE.

• Visualization makes it easier to understand the characteristics of the

solutions.

• The stability of a system can be assessed by observing the behavior of


solutions.

• Plots can be used to verify numerical methods and simulations by

comparing them with analytical solutions.


9).Wave Equation
Aim: To find solution to Wave Equation for the associated conditions and plot
the solution.
Mathematical Concept:The wave equation is a second-order partial
differential equation that describes the behavior of waves, such as sound
waves, electromagnetic waves, and mechanical waves. The general form of the
one-dimensional wave equation is:
∂2u/∂t2 =c2(∂2 u/∂x2)
Here, u(x,t) represents the displacement of the wave as a function of both
space (x) and time (t), and c is the wave speed.
The associated conditions for the wave equation often involve specifying the
initial conditions and boundary conditions. These conditions help determine
the unique solution to the wave equation for a given physical situation.
i).Initial Conditions:
The initial condition provides the starting temperature distribution
throughout the spatial domain at the beginning of the simulation
ii).Boundary Conditions:
Boundary conditions are specifications applied to the solution on the
boundary of the domain in which the equation is being solved.
Programming Language: Python
Source Code:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def wave_equation_simulation(L, T, c, Nx, Nt):
# Discretization
dx = L / (Nx - 1)
dt = T / Nt
x = np.linspace(0, L, Nx)
t = np.linspace(0, T, Nt)
# Initialize solution grid
u = np.zeros((Nt, Nx))
# Initial condition
u[0, :] = np.sin(np.pi * x)
# Finite Difference Method
for n in range(1, Nt):
for i in range(1, Nx - 1):
u[n, i] = u[n-1, i] + (c * dt / dx)**2 * (u[n-1, i+1] - 2 * u[n-1, i] + u[n-1, i-
1])
# Plot the solution
X, T = np.meshgrid(x, t)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, T, u, cmap='viridis')
ax.set_xlabel('X')
ax.set_ylabel('Time')
ax.set_zlabel('Displacement')
ax.set_title('Numerical Solution of the Wave Equation')
plt.show()
# Input parameters
L_input = float(input("Enter the length of the spatial domain (L): "))
T_input = float(input("Enter the total simulation time (T): "))
c_input = float(input("Enter the wave speed (c): "))
Nx_input = int(input("Enter the number of spatial grid points (Nx): "))
Nt_input = int(input("Enter the number of time steps (Nt): "))
# Run the simulation with user input
wave_equation_simulation(L_input, T_input, c_input, Nx_input, Nt_input)
Input:
Enter the length of the spatial domain (L): 1
Enter the total simulation time (T): 1
Enter the wave speed (c): 1
Enter the number of spatial grid points (Nx): 4
Enter the number of time steps (Nt): 100
Output:

Observations:
➢ Implementing the solutions of wave equations in graph is crucial in the
fields of seismology, signal processing,etc,,.
➢ Using programming would help to handle complex conditions and
calculations in time effective manner
10.One – Dimensional Heat Equation

Aim: To find solution of a one – dimensional heat equation.


Mathematical Concept:The one-dimensional heat equation is a partial differential equation that
describes the distribution of heat in a one-dimensional domain over time. The general form of the
one-dimensional heat equation is:

∂u/∂t=α∂2 u/∂t2

where:

• u(x,t) is the temperature distribution at position x and time t,

• α is the thermal diffusivity.

The heat equation is subject to various boundary and initial conditions, which specify the behavior of
the solution.

I). Initial Condition: u(x,0)=f(x) The initial condition specifies the temperature distribution at the
initial time t=0.

II). Boundary Conditions: These conditions help determine the behavior of the solution near the
edges of the region under consideration. In the case of the heat equation, the boundaries
correspond to the spatial limits of the material or system being studied.

III). Periodic Boundary Condition:u(a,t) =u(b,t) For a domain with periodic boundary conditions, the
temperature at one boundary is equal to the temperature at the opposite boundary for all time t.

Programming Language: Python

Source Code:
import numpy as np

import matplotlib.pyplot as plt

L = float(input("Length of the rod:"))

T = float(input("Final time:"))

alpha = 0.01

N = int(input("Number of spatial points:"))

x = np.linspace(0, L, N+1)

dx = L / N

dt = 0.5 * dx**2 / alpha

M = int(T / dt)
u0 = np.sin(np.pi * x)

u_left = 0

u_right = 0

u = np.zeros((N+1, M+1))

u[:, 0] = u0

u[0, :] = u_left

u[-1, :] = u_right

for k in range(0, M):

for i in range(1, N):

u[i, k+1] = u[i, k] + alpha * dt / dx**2 * (u[i+1, k] - 2 * u[i, k] + u[i-1, k])

plt.figure(figsize=(10, 6))

for k in range(0, M+1, 50):

plt.plot(x, u[:, k], label=f'Time step {k}')

plt.xlabel('Position (x)')

plt.ylabel('Temperature (u)')

plt.title('Temperature Distribution over Time (One-dimensional Heat Equation)')

plt.legend()

plt.grid(True)

plt.show()

Input

Length of the rod:4

Final time:3

Number of spatial points:150


Output

Uses:
• The solution helps in predicting how the temperature evolves and understanding the
thermal behavior of the system under given constraints.

You might also like