ENGR1330-Assignment5
ENGR1330-Assignment5
Anthony, James
R#11937991
DESKTOP-LKKK32J
desktop-lkkk32j\ja606
C:\Users\ja606\anaconda3\python.exe
3.12.4 | packaged by Anaconda, Inc. | (main, Jun 18 2024, 15:03:56) [MSC v.1929 64 bit (AMD64)]
sys.version_info(major=3, minor=12, micro=4, releaselevel='final', serial=0)
Resources
https://www.w3schools.com/PYTHON/numpy/numpy_creating_arrays.asp
https://www.w3schools.com/PYTHON/numpy/numpy_array_shape.asp
https://www.w3schools.com/PYTHON/numpy/numpy_array_sort.asp
Instructions
1. Create a 1-dimensional Array, named my_arr containing the elements [9,8,7,6,5,4,3,2,1].
2. Print the shape of my_arr.
3. Create a 2-dimensional Array, named my_mat containing rows, each containing a copy of my_arr.
4. Print the shape of my_mat.
5. Create a new Array named my_prod and use it to store the result of the operation my_arr * my_arr.
6. Print out my_prod
7. Use the np.sort function to sort my_prod and store the result in a new Array named my_prod_sorted.
8. Print out my_prod_sorted
1. In Question 2, you created an array called my_arr. Use the reshape function to create a 3x3 matrix named arr_reshaped.
2. Print out arr_reshaped to see the result.
3. Use indexing to print out the last row of the matrix and print out the element at the center of the matrix.
4. Create two 1-dimensional arrays named X and Y with the elements [3,9,5,4] and [1,8,7,6], respectively.
5. Compute the dot product of X and Y and print the result.
import numpy as np
last_row = arr_reshaped[-1]
print("Last row:", last_row)
center_element = arr_reshaped[1, 1]
print("Element at the center:", center_element)
X = np.array([3, 9, 5, 4])
Y = np.array([1, 8, 7, 6])
dot_product = np.dot(X, Y)
print("Dot product of X and Y:", dot_product)
arr_reshaped:
[[9 8 7]
[6 5 4]
[3 2 1]]
Last row: [3 2 1]
Element at the center: 5
Dot product of X and Y: 134
a, b = -3, 3
root_bisection = bisection(a, b)
print(f"Root from Bisection Method: {root_bisection}")
def f(x):
return x**3 - 7*x
x0, x1 = -3, 3
root_secant = secant(x0, x1)
print(f"Root from Secant Method: {root_secant}")
Given:
Length of the rod (L): 1 meter
Initial temperature distribution: Uniform at 100°C
Boundary conditions:
(u(0, t) = 100) °C (left end)
(u(L, t) = 0) °C (right end)
Total simulation time (T): 1 second
Spatial step ( Delta x): 0.1 m
Time step ( Delta t): 0.01 s
Thermal diffusivity (alpha): 0.1 m²/s
Tasks
1. Initialize the temperature distribution along the rod.
2. Use the explicit finite difference method to update the temperature at each time step.
# Parameters
L = 1.0 # Length of the rod
alpha = 0.1 # Thermal diffusivity
T = 1.0 # Total time
dx = 0.1 # Spatial step
dt = 0.01 # Time step
# Discretization
x = np.arange(0, L + dx, dx)
Nx = len(x)
time_steps = int(T / dt)
# Initial condition
u = np.ones(Nx) * 100 # Initial temperature: 100°C
u = u_new