0% found this document useful (0 votes)
39 views5 pages

2024 Week 6 - Jupyter Notebook

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)
39 views5 pages

2024 Week 6 - Jupyter Notebook

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/ 5

In [1]: 1 import numpy as np

Write a python code to evaluate the first derivative of the function 𝑓(𝑥) = 𝑥2 + 𝑥3 + 𝑥5 𝑥 = 1.1
at using the 2-
point forward difference scheme for ℎ = 0.1 .
3
Round the answer to decimal places using the Python function round() as below: ans = round(ans, 3)
Note: Do not use inbuilt methods/libraries to compute the derivative.

In [2]: 1 def forward_difference(f, x, h):


2 return (f(x + h) - f(x)) / h # Define the 2-point forward difference scheme
3 ​
4 def f(x):
5 return x**2 + x**3 + x**5
6 ​
7 # Parameters
8 x = 1.1
9 h = 0.1
10 ​
11 derivative = forward_difference(f, x, h)
12 round(derivative, 3)

Out[2]: 15.048

Write a python code to find the first derivative of the function 𝑓(𝑥) = 𝑥 + 𝑥42 + 𝑥93 at 𝑥 = 2.0 using the 2-point
backward difference scheme for ℎ = 0.1
.
Round your answer to 3 decimal places. Note: Do not use inbuilt methods/libraries.

In [3]: 1 def backward_difference(f, x, h):


2 return (f(x) - f(x - h)) / h # Define the 2-point backward difference scheme
3 ​
4 def f(x):
5 return x + (x**2 / 4.0) + (x**3/ 9.0)
6 ​
7 # Parameters
8 x = 2.0
9 h = 0.1
10 ​
11 derivative = backward_difference(f, x, h)
12 round(derivative, 3)

Out[3]: 3.243

Write a python code to find the first derivative of the function 𝑓(𝑥) = 𝑥𝑥22−1+1 at 𝑥 = 0.09 using the 3-point central
difference scheme for ℎ = 0.01
.
Round the answer to 1 decimal place. Note: Do not use inbuilt methods/libraries.

In [4]: 1 def central_difference(f, x, h):


2 return (f(x + h) - f(x - h)) / (2 * h) # Define the 3-point central difference schem
3 ​
4 def f(x):
5 return (x**2 - 1) / (x**2 + 1)
6 ​
7 # Parameters
8 x = 0.09
9 h = 0.01
10 ​
11 derivative = central_difference(f, x, h)
12 round(derivative, 1)

Out[4]: 0.4
Write a python code to solve the differential equation
𝑑𝑥
𝑑𝑡 = 1𝑥 + 1𝑡 .
Use Euler’s forward scheme with time step, ∆𝑡 = 0.001
to find the value of 𝑥(𝑡) 𝑡 = 2.0
at for the initial condition
𝑥(1) = 1.
Round the answer to two decimal places. Note: You should not use any inbuilt methods/libraries in your code.

In [5]: 1 def euler_forward(f, x0, t0, dt, t_end):


2 x, t = x0, t0
3 while t < t_end:
4 x = x + f(x, t) * dt
5 t = t + dt
6 return x
7 ​
8 def f(x, t):
9 return 1/x + 1/t
10 ​
11 # Initial conditions and parameters
12 x0 = 1.0 # Initial value x(1) = 1
13 t0 = 1.0 # Initial time t = 1
14 t_end = 2.0 # Final time t = 2
15 dt = 0.001 # Time step
16 ​
17 x_t = euler_forward(f, x0, t0, dt, t_end)
18 round(x_t, 2)

Out[5]: 2.3

= 𝑥2 − 100𝑥.
Write a python code to solve the differential equation
𝑑𝑥
𝑑𝑡
Use Euler’s backward scheme with time step, Δ𝑡 = 0.001 to find the value of 𝑥(𝑡) at 𝑡 = 1 for the initial condition
𝑥(0) = 1.
Round the answer to two decimal places. Note: You should not use any inbuilt methods/libraries in your code.

In [6]: 1 def euler_backward(f, x0, t0, dt, t_end):


2 x = x0
3 t = t0
4 while t < t_end:
5 x_new = x # Initial guess for x_new is the old x value
6 for _ in range(10): # Iterate to refine x_new
7 x_new = x + dt * f(x_new)
8 x = x_new
9 t += dt
10 return x
11 ​
12 def f(x):
13 return x**2 - 100*x
14 ​
15 # Initial conditions and parameters
16 x0 = 1.0 # Initial value x(0) = 1
17 t0 = 0.0 # Initial time t = 0
18 t_end = 1.0 # Final time t = 1
19 dt = 0.001 # Time step
20 ​
21 x_t = euler_backward(f, x0, t0, dt, t_end)
22 round(x_t, 2)

Out[6]: 0.0

A body of mass 3𝑘𝑔 is attached to a spring with a spring constant 9.


The motion of the body is impeded by a drag force whose magnitude is 6 times speed and the direction is opposite
to that of its velocity.
𝑦 of body is given by 𝑑𝑑𝑡2𝑦2 + 2 𝑑𝑦𝑑𝑡 + 3𝑦 = 0 .
The differential equation governing the displacement,
Find the displacement 𝑦(𝑡 = 4) using Euler-Forward scheme given that 𝑦(0) = 4.0 and 𝑑𝑡 (0) = −1.0 . Take
𝑑𝑦
Δ𝑡 = 0.001.
Round your answer to 2 decimal places.
In [7]: 1 def euler_forward(f, y0, v0, t0, t_end, dt):
2 y = y0
3 v = v0
4 t = t0
5 while t < t_end:
6 dy_dt, dv_dt = f(y, v)
7 y = y + dy_dt * dt
8 v = v + dv_dt * dt
9 t += dt
10 return y
11 ​
12 def f(y, v):
13 dy_dt = v
14 dv_dt = -2 * v - 3 * y
15 return dy_dt, dv_dt
16 ​
17 # Initial conditions and parameters
18 y0 = 4.0 # Initial displacement y(0) = 4.0
19 v0 = -1.0 # Initial velocity dy/dt(0) = -1.0
20 t0 = 0.0 # Initial time t = 0
21 t_end = 4.0 # Final time t = 4
22 dt = 0.001 # Time step
23 ​
24 y_t = euler_forward(f, y0, v0, t0, t_end, dt)
25 round(y_t, 2)

Out[7]: 0.04

𝑖
The differential equation which relates the current , voltage 𝑉 , resistance 𝑅 and inductance 𝐿 of a series
electrical circuit, is given by -

𝑑𝑖
𝑑𝑡 + 𝑅𝐿𝑖 = 𝑉𝐿.
Use Euler forward and Euler backward scheme to find the value of current 𝑖(𝑡 = 2)
for the initial condition
𝑖(𝑡 = 0) = 0.0 .
Use 𝑉 = 240.0,𝑅 = 20.0,𝐿 = 50.0 Δ𝑡 = 0.01
and .
Let's say the result from Euler forward scheme is e_forward and from euler backward scheme is e_backward.
Round them both to two decimal places.
Now calculate the following quantity

𝑒 = ((𝑒𝑒𝑓𝑜𝑟𝑤𝑎𝑟𝑑 −𝑒𝑏𝑎𝑐𝑘𝑤𝑎𝑟𝑑 ) × 104


𝑓𝑜𝑟𝑤𝑎𝑟𝑑 +𝑒𝑏𝑎𝑐𝑘𝑤𝑎𝑟𝑑 )
Round the value of 𝑒 to two decimal places using round(e,2) . What is the value of e ?
In [8]: 1 # Euler-Forward Scheme
2 def euler_forward(i0, V, R, L, dt, t_end):
3 i = i0
4 t = 0.0
5 while t < t_end:
6 i = i + dt * (V - R * i) / L
7 t += dt
8 return i
9 ​
10 # Euler-Backward Scheme
11 def euler_backward(i0, V, R, L, dt, t_end):
12 i = i0
13 t = 0.0
14 while t < t_end:
15 i = (i + dt * V / L) / (1 + dt * R / L)
16 t += dt
17 return i
18 ​
19 # Parameters
20 V = 240.0 # Voltage
21 R = 20.0 # Resistance
22 L = 50.0 # Inductance
23 dt = 0.01 # Time step
24 t_end = 2.0 # Final time
25 i_0 = 0.0 # Initial current i(0)
26 ​
27 e_forward = round(euler_forward(i_0, V, R, L, dt, t_end), 2)
28 e_backward = round(euler_backward(i_0, V, R, L, dt, t_end), 2)
29 error = ((e_forward - e_backward) / (e_forward + e_backward)) * 10**4
30 round(error, 2)

Out[8]: 15.13

𝑑𝑥 + 𝑥𝑡 = 0 by using Euler's backward scheme with


Write a python program to solve the differential equation
𝑑𝑡
time step,Δ𝑡 = 0.01
𝑥(𝑡 = 2.0) given the initial condition 𝑥(𝑡 = 0) = 1.0. Round the answer to 2 decimal places.
and find the value of

In [9]: 1 def euler_backward(x0, dt, t_end):


2 x = x0
3 t = 0.0
4 while t < t_end:
5 x = x / (1 + dt)
6 t += dt
7 return x
8 ​
9 # Parameters
10 dt = 0.01 # Time step
11 t_end = 2.0 # Final time
12 x0 = 1.0 # Initial condition
13 ​
14 x_t_end = euler_backward(x0, dt, t_end)
15 round(x_t_end, 2)

Out[9]: 0.14

Write a Python program to solve the differential equation = 𝑡𝑥2 using Trapezoid method and evaluate
𝑑𝑥
𝑑𝑡
𝑥(𝑡 = 2.0).
Use time step, Δ𝑡 = 0.01 and the initial condition, 𝑥(𝑡 = 1) = 1 . Round your answer to 2 decimal places.
In [10]: 1 def trapezoid_method(x0, dt, t_end, t_start):
2 x = x0
3 t = t_start
4 while t < t_end:
5 t_next = t + dt
6 x_next = (x + (dt / 2) * (x / t**2)) / (1 - (dt / 2) * (1 / t_next**2)) # x_nex
7 x = x_next
8 t = t_next
9 return x
10 ​
11 # Parameters
12 dt = 0.01 # Time step
13 t_end = 2.0 # Final time
14 x0 = 1.0 # Initial condition
15 t_start = 1.0 # Initial time
16 ​
17 x_t_end = trapezoid_method(x0, dt, t_end, t_start)
18 round(x_t_end, 2)

Out[10]: 1.65

Write a python code to solve the differential equation = 𝑥 + 𝑦 using Euler Forward scheme and find the value
𝑑𝑦
𝑑𝑥
of 𝑦(𝑥 = 2) .
Take time step, Δ𝑥 = 0.01 and the initial condition to be 𝑦(𝑥 = 0) = 1.0.Round the answer to two decimal
places.

In [11]: 1 def euler_forward(y0, dx, x_end):


2 y = y0
3 x = 0.0
4 while x < x_end:
5 y = y + dx * (x + y) # Update y using Euler Forward formula
6 x += dx # Increment x
7 return y
8 ​
9 # Parameters
10 dx = 0.01 # Time step (Δx)
11 x_end = 2.0 # Final value of x
12 y0 = 1.0 # Initial condition y(x=0)
13 ​
14 y_at_end = euler_forward(y0, dx, x_end)
15 round(y_at_end, 2)

Out[11]: 11.63

You might also like