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

MLL703_minor_tutorial222222

The document outlines the instructions and questions for a laboratory exam in Mathematical and Computational Methods in Materials, scheduled for 23-Sep-2024. It includes five questions requiring programming tasks in Python, such as input handling, solving a quadratic equation, plotting a decay curve, and implementing Euler's method. Students must submit their answers in a Jupyter notebook format and are warned against using AI-generated content.

Uploaded by

Jyoti Bharti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

MLL703_minor_tutorial222222

The document outlines the instructions and questions for a laboratory exam in Mathematical and Computational Methods in Materials, scheduled for 23-Sep-2024. It includes five questions requiring programming tasks in Python, such as input handling, solving a quadratic equation, plotting a decay curve, and implementing Euler's method. Students must submit their answers in a Jupyter notebook format and are warned against using AI-generated content.

Uploaded by

Jyoti Bharti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

MLL703

Mathematical and Computational Methods in Materials


Laboratory exam

Time: 1.5 hours DATE: 23-Sep-2024

Instructions:

1. Attempt all questions.


2. Do not copy from ChatGPT or Gemini. Your answers will be
run through Turnitin, which can detect AI-generated content.
3. Submit all your answers in a single .ipynb file (Jupyter notebook).
Make sure each question is clearly labeled with comments and
markdown. Upload your .ipynb file on Moodle with the format
keybrosid_name.ipynb.
4. No extra time will be given.

Question 1: Write a program that takes the input of a student's name and their marks, ensure
that the marks are entered as a floating-point number. Print the student's name and their marks,
along with the type of both variables. (3 marks)

Question 2: Write a Python code to solve a quadratic equation (10 marks)


𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0

• Take input of a, b, and c from the user using the input function.
• Use the if else statement.
o Syntax of if else is
if condition:
# block of code executed if the condition is true
elif another_condition:
# block of code executed if the previous condition is false
and this one is true
else:
# block of code executed if all the above conditions are false

Question 3: Write a print(f"{}) statement to display the values of a = 5 and b = 10 in the


format: "The values of a and b are 5 and 10." (2 marks)
Question 4: A radioactive substance decays according to a decay constant of λ=0.1 (per unit
time), with an initial quantity of N0=1000. (10 marks)

1. Formulate the ordinary differential equation (ODE) that describes this decay
process.
2. Utilize the solve_ivp function from the scipy.integrate module to numerically solve
for the remaining quantity of the substance over time.
3. Plot the decay curve over the time interval from t=0 to t=50 using
the matplotlib.pyplot module.
4. Ensure that both axes are labeled using plt.xlabel and plt.ylabel, and provide a title for
the graph.

Question 5: Fill in the blanks: (5 marks)

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp
# Function representing the ODE: dy/dx = f(x, y)
def f(x, y):
return _____________ # ODE: y’+2y=x3*e-2x

# Euler's method implementation


def euler_method(f, x0, y0, h, x_end):
# Create arrays to store the results
x_values = np.arange(_________) # create values from x0 to x_end with interval of h
y_values = np.zeros(len(x_values))
# Initial condition
y_values[0] = y0
# Euler's method iterative process
for i in range(1, len(x_values)):
y_values[i] = ________________________ # formula of euler method
return x_values, y_values
# Solve the ODE using solve_ivp
def solve_with_solve_ivp(f, x0, y0, x_end):
sol = solve_ivp(f, [x0, x_end], [y0], t_eval=np.arange(x0, x_end, 0.01))
return sol.t, sol.y[0]

# Initial conditions and parameters


x0 = 0
y0 = 1 # y(x0) = y0
h = 0.01 # Step size for Euler's method
x_end = 2 # We want to approximate up to x = 2

# Run Euler's method


x_euler, y_euler = euler_method(f, x0, y0, h, x_end)

# Solve using solve_ivp


x_solve_ivp, y_solve_ivp = ________________ # solve using function ivp

# Plot both methods


plt.plot(x_euler, y_euler, 'bo--', label="Euler's Method")
plt.plot(x_solve_ivp, y_solve_ivp, 'r-', label="solve_ivp (RK45)")

plt.xlabel('x')
plt.ylabel('y')
plt.title("Comparison of Euler's Method and solve_ivp for ODE")
plt.legend()
plt.grid(True)
plt.show()

You might also like