ME685 Homework3
ME685 Homework3
ME685 Homework3
PSEUDOCODE:
1. Initialize parameters:
𝑁 𝑠
𝑚 = 30 𝑘𝑔, 𝑘 = 20 , 𝑐 = 10 𝑁. , ∆𝑡 = 0.1𝑠,
𝑚 𝑚
𝑡𝑠𝑡𝑎𝑟𝑡 = 0𝑠, 𝑡𝑒𝑛𝑑 = 15𝑠, 𝑥𝑜 = 1𝑚, 𝑥𝑜̇ = 0 𝑚/𝑠
CODE:
PLOT AND RESULT:
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:
Q2.
𝑑𝑦
= −1000𝑦 + 3000 − 2000𝑒 −𝑡
𝑑𝑡
𝑡 ∈ [0, 4] and 𝑡 ∈ [0, 0.05]
PSEUDOCODE:
Driving Code:
4th Order Runge-Kutta Method:
Runge-Kutta Cash Karp Method:
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: