ME685 Homework3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Homework Assignment 3

(Harsh Garg – 200411)


Q1.

(a) - Euler Method:

PSEUDOCODE:

1. Initialize parameters:
𝑁 𝑠
𝑚 = 30 𝑘𝑔, 𝑘 = 20 , 𝑐 = 10 𝑁. , ∆𝑡 = 0.1𝑠,
𝑚 𝑚
𝑡𝑠𝑡𝑎𝑟𝑡 = 0𝑠, 𝑡𝑒𝑛𝑑 = 15𝑠, 𝑥𝑜 = 1𝑚, 𝑥𝑜̇ = 0 𝑚/𝑠

2. Create time array 𝑡 from 𝑡𝑠𝑡𝑎𝑟𝑡 to 𝑡𝑒𝑛𝑑 with step ∆𝑡.


3. Initialize arrays 𝑥 and 𝑥̇ with zeros.
4. Set initial conditions:
a. 𝑥(0) = 𝑥𝑜
b. 𝑥̇ (0) = 𝑥𝑜̇
5. Loop over time from 𝑖 = 1 to 𝑛𝑢𝑚𝑒𝑙(𝑡)
𝑘 𝑐
a. Compute 𝑥̈ = − 𝑚 𝑥 − 𝑚 𝑥̇
b. Update 𝑥̇ (𝑖) = 𝑥̇ (𝑖 − 1) + ∆𝑡 × 𝑥̈ (𝑖 − 1)
c. Update 𝑥(𝑖) = 𝑥(𝑖 − 1) + ∆𝑡 × 𝑥̇ (𝑖 − 1)
6. Return time array 𝑡, displacement array 𝑥, and velocity array 𝑥̇ .

CODE:
PLOT AND RESULT:

Displacement 𝑥(𝑡 = 15) = 0.0598 𝑚 and Velocity 𝑥̇ (𝑡 = 15) = 0.0373 𝑚/𝑠

(b) - Modified Heun's Method:

PSEUDOCODE:

1. Initialize parameters:
𝑁 𝑠
𝑚 = 30 𝑘𝑔, 𝑘 = 20 , 𝑐 = 10 𝑁. , ∆𝑡 = 0.1𝑠,
𝑚 𝑚
𝑡𝑠𝑡𝑎𝑟𝑡 = 0𝑠, 𝑡𝑒𝑛𝑑 = 15𝑠, 𝑥𝑜 = 1𝑚, 𝑥𝑜̇ = 0 𝑚/𝑠
2. Create time array 𝑡 from 𝑡𝑠𝑡𝑎𝑟𝑡 to 𝑡𝑒𝑛𝑑 with step ∆𝑡.
3. Set initial conditions:
a. 𝑥(0) = 𝑥𝑜
b. 𝑥̇ (0) = 𝑥𝑜̇
4. Loop over time steps from 𝑖 = 3 to 𝑛𝑢𝑚𝑒𝑙(𝑡)
a. Use predictor step to estimate 𝑦𝑜 and 𝑥𝑜 using the explicit Euler method.
b. Loop for a specified number of iterations (e.g., 10) for the corrector step:
i. Compute 𝑥1̇ 𝑎𝑛𝑑 𝑥1 using the corrector formula.
ii. Update 𝑥𝑜 and 𝑦𝑜 with the new values.
c. Apply the corrector modifier to 𝑥1 and 𝑦1 to improve accuracy.
d. Update 𝑥(𝑖) and 𝑥̇ (𝑖) with the corrected values.
5. Return the time array 𝑡, displacement array 𝑥, and the velocity array 𝑥̇ .
CODE:
PLOT ANS RESULT:

Displacement 𝑥(𝑡 = 15) = 0.0634 𝑚 and Velocity 𝑥̇ (𝑡 = 15) = 0.0323 𝑚/𝑠

Q2.
𝑑𝑦
= −1000𝑦 + 3000 − 2000𝑒 −𝑡
𝑑𝑡
𝑡 ∈ [0, 4] and 𝑡 ∈ [0, 0.05]

Step size, h = 0.001 and 0.002

PSEUDOCODE:

1. 4th Order Runge Kutta:


a. Choose the step size (h) as 0.001 initially, and then adjust it to 0.002.
b. Specify the time interval (t) from 0 to 4 initially, and later from 0 to 0.05.
c. Initialize arrays to hold the time values (t1) and the results obtained using the
Runge-Kutta Method (yRK1).
d. Utilize a for loop to iterate through the time steps, employing the 4th-order
Runge-Kutta method (RK4) to calculate the solution at each step.
e. Generate a plot depicting the solution obtained via the Runge-Kutta method as a
function of time.
f. Assume that the code includes the RK4 function to facilitate the Runge-Kutta
integration process.
2. Adaptive Step Size Runge-Kutta:
a. Set the maximum number of steps (stepmax) to 500.
b. Initialize the iteration counter (it) to 1, the step size (h) to 0.001 initially, and then
to 0.001 again. Also, set the initial conditions for y and t.
c. Set the tolerance (eps) to 5×10−5 and define the final time (tf) as 4 initially, and
then as 0.05.
d. Utilize a while loop to iterate until either the maximum number of steps is
reached, or the final time is exceeded.
e. Inside the loop:
i. Calculate the derivative (df) using the provided function f_yt.
ii. Determine the scaling factor (yscal) for error estimation.
iii. Invoke the Adapt function to adjust the step size (h) and update the time
and solution arrays (t and y).
iv. If the next time step exceeds the final time, adjust the step size to
precisely reach the final time.
v. Increment the iteration counter (it).
f. Plot the solution (y) against time (t).
3. Adapt Function:
a. Implement the Adapt function to dynamically adjust the step size according to
the error estimation.
b. Set the error tolerance (etol) and a scaling factor (s) to an initial value of 1.
c. Employ a while loop to iteratively adjust the step size until the error falls
within the specified tolerance.
d. Calculate the updated step size (hnew) based on the estimated error.
e. If the error surpasses the specified tolerance, further adjust the step size as
necessary.
f. Update both the time variable (x) and the solution variable (y) using the new
step size.
4. RK Cash Karp method:
a. Create a function for the RK Cash-Karp method to execute Runge-Kutta
integration.
b. Set up constants and establish the differential equation f(y, t).
c. Calculate intermediate values (k1 to k6) utilizing the given coefficients and the
derivative function.
d. Determine the output values (yout) and the error estimate (yerr) employing the
Runge-Kutta method.
e. Provide the output values yout and the error estimate yerr upon completion.
5. RK4 Function:
a. Create a RK4 function to execute fourth-order Runge-Kutta integration.
b. Define the function handle f_yt for the differential equation.
c. Calculate the four intermediate values (k1 to k4) by applying the provided
differential equation and the current state (x, y) at each iteration.
d. Determine the updated state (ynew) by averaging these four intermediate values
with appropriate weights.
e. Output the updated state ynew.
CODE:

Driving Code:
4th Order Runge-Kutta Method:
Runge-Kutta Cash Karp Method:

Adaptive Step-Size RK:


PLOT AND RESULT:
Q3.

PSEUDOCODE:

1. Initialize parameters:
a. 𝑔 = 9.81 𝑚2 /𝑠
b. 𝐿 = 0.5 𝑚
c. ℎ = 0.001 𝑠(𝑠𝑡𝑒𝑝 𝑠𝑖𝑧𝑒)
d. 𝑡𝑠𝑡𝑎𝑟𝑡 = 0𝑠, 𝑡𝑒𝑛𝑑 = 1𝑠
e. 𝜃𝑜 = 0 𝑟𝑎𝑑, 𝜃𝑜̇ = 0.25 𝑟𝑎𝑑/𝑠
2. Create time array t:
a. Generate time points from 𝑡𝑠𝑡𝑎𝑟𝑡 to 𝑡𝑒𝑛𝑑 with step h.
3. Initialize arrays:
a. Create arrays 𝜃 and 𝜃̇ with zeros to store angular deflection and velocity.
4. Set initials conditions:
a. Set 𝜃(0) = 𝜃𝑜 and 𝜃̇ (0) = 𝜃̇𝑜 .
5. Loop over time steps from 𝑖 = 2 to 𝑛𝑢𝑚𝑒𝑙(𝑡):
a. Formulate the coefficient matrix A and the right-hand side vector b for the
implicit Euler scheme.
𝜃𝑖
b. Solve the equation 𝐴. [ ̇ ] = 𝑏 to find 𝜃𝑖 and 𝜃̇𝑖 using backslash operator.
𝜃𝑖
̇
c. Update 𝜃(𝑖) and 𝜃 (𝑖) with the obtained values.
6. Return time array t, angular deflection array 𝜃, and angular velocity array 𝜃̇.

CODE:
PLOT AND RESULT:

Angular Deflection 𝜃(𝑡 = 1) = −0.0537 𝑟𝑎𝑑 and Angular Velocity 𝜃̇ (𝑡 = 1) = −0.0691 𝑟𝑎𝑑/𝑠
Q4.

PSEUDOCODE:

1. Set the step size h for discretizing the interval [0, 0.5].
2. Generate a grid of x values from 0 to 0.5 with step size h.
3. Initialize arrays for temperature T and its derivative b:
a. T with initial condition T(0) = 200.
b. b with initial guess values for the derivative at x = 0.
4. Perform shooting method loop for each guess value of b:
a. Set the initial condition T(0) and derivative b(0) based on the current guess.
b. Use RK4 method to integrate the system of ODEs:
i. dT/dx = b(x)
ii. db/dx = 10-7 * (T + 273)4 - 4 * (150 - T)
c. Iterate over each grid point to compute T and b.
d. Store the final temperature values T(0.5) obtained for each guess of b.
5. Interpolate to find the correct derivative value b at x = 0 for which T(0.5) = 100:
a. Use Lagrange interpolation to find the correct derivative value corresponding to
T(0.5) = 100.
b. Solve for the correct derivative value using the interpolated function.
6. Integrate the system of ODEs using the correct derivative value:
a. Use RK4 method to integrate the system of ODEs with the correct derivative
value.
b. Store the temperature values T at each grid point.
7. Output the final solution T(0.5).

CODE:
RESULTS:
𝑑𝑇
= 0.0000000048140357128727373492438346147537 𝑥 4
𝑑𝑥
+ 0.000018198778977683446100854780524969 𝑥 3
+ 0.026688163633902001947717508301139 𝑥 2
+ 18.963551194000038435660826507956 𝑥
+ 5487.1281427734575117938220500946
𝑑𝑇
=
𝑑𝑥
−1487.0854335262644799617949372881
( −839.3497136810482075200411015643 )
−726.96148437672890641515320808332 + 620.24844223096095461695641661583 𝑖
−726.96148437672890641515320808332 − 620.24844223096095461695641661583 𝑖

𝑇(0.5) ≈ 99.9975
Q5.

PSEUDOCODE:

1. Initialize parameters:
a. Set the convergence tolerance 𝑒𝑡𝑜𝑙 = 1 × 10−5 .
b. Initialize arrays to store eigen vectors 𝑥 and eigen values 𝜆.
2. Generate initial random vector:
a. Generate a random vector 𝑥𝑜 of size 5 (since the matrix A is 5× 5).
3. Iterate for each eigenpair from 𝑘 = 1 𝑡𝑜 5.
a. Set the 𝑒𝑟𝑟 = 1.
b. Initialize the iteration count 𝑖 = 1.
c. While 𝑒𝑟𝑟 > 𝑒𝑡𝑜𝑙:
i. Compute 𝑥𝑖 = 𝐴𝑥𝑖−1.
ii. Find the largest magnitude element of 𝑥𝑖 and its position.
iii. Compute the eigenvalue 𝜆𝑘 as the element at the position found.
𝜆𝑘 − 𝜆𝑘−1
iv. Compute the relative error 𝑒𝑟𝑟 = | 𝜆𝑘
|.
v. Update 𝑥𝑖−1 = 𝑥𝑖 .
vi. Increment 𝑖.
d. Store the computed eigenvector 𝑥𝑖 and eigenvalue 𝜆𝑘 .
e. Update 𝐴 = 𝐴 − 𝜆𝑘 (𝑥𝑖 × 𝑥𝑖𝑇 ).
4. Return the array of eigenvectors 𝑥 and eigenvalues 𝜆.

CODE:

RESULT:

You might also like