Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Question 1 [19 marks] The fourth order Runge-Kutta formula is one of the most widely used methods for the numer- ical solution of differential equations
Question 1 [19 marks] The fourth order Runge-Kutta formula is one of the most widely used methods for the numer- ical solution of differential equations of the type: dy de = f(,y) It improves the accuracy of the Euler method by considering several points for the approximation of the derivative of the unknown function. Given a set of points 11 = to+ih, i = 0,1,2,..., n-1, with co the initial point, h the distance between consecutive points and n the total number of points, the corresponding values of the unknown function yi can be computed using the following formulas: 24+ ki = hf (149) h ki ky = hf 1 + h ky kg = hf. + 2 y + ka = hf (x +h, yi+k3) ki k2 ka kt Yi+1 = +5 + + 3 3+5 where yo is considered to be known. Write a function, named RungeKuttat, implementing the method for unknowns y and right hand sides f of arbitrary dimension such that it can be used for second order equations as well. The function should (a) Receive three input arguments: + ENG1002 1 TURN OVER ENG1002 Engineering Mathematics and Scientific Computing TMA 2 A Python function or lambda representing the right hand side of the equation to be solved. This should receive as input y and c and return a list with all the components of the function. A list or array containing the values of the components of y for the first point in the interval of interest. . A Numpy array with the points x; for which the equation should be solved. [1 mark] (b) Return a 2D Numpy array containing the values of y. The first index of this array (rows) should represent different values of i and the second columns) different components of y. [1 mark] (c) Include a docsrtring and be appropriately commented. [2 marks] (d) Pass the unit tests included in the provided test_coursework.py file. [15 marks] Question 2: [15 marks] The second order differential equation for a damped harmonic oscillator is: m +ci+ku=F(t) where: m is the mass of the oscillator c is the damping coefficient of the oscillator k is the stiffness of the oscillator F(t) is the applied force 1, , i is the displacement, velocity and acceleration of the mass The period of the oscillator can be computed as: T=2-V Write a class, named harmoniclecillator to represent and solve harmonic oscillators for different applied forces and initial conditions. The class should contain the following: (a) A constructor, that: Receives the mass, damping coefficient and stiffness of the oscillator as input arguments. Ensures that the provided values are meaningful by checking their data types are float or int and their signs are positive for m and k and nonnegative for c. Creates data attributes, named , c and for the mass, damping coefficient and stiffness of the oscillator with the provided values. Passes the unit tests included in the provided test_coursework.py file. [4 marks] ENG1002 2 TURN OVER ENG1002 Engineering Mathematics and Scientific Computing TMA 2 (b) A method named period to compute the period of the oscillator based on the provided for- mula. The method should pass the corresponding tests in the provided test_coursework.py file. [2 marks] (c) A method, named provided solve, to solve the equation of the oscillator for user-provided initial conditions and applied forces, using the function from Question 1. This method should: Receive as input a Python function or lambda with the value of the applied force as a function of t and a list with the initial displacement and velocity of the oscillator. Create a lambda with the right hand side of the first order equation corresponding to the equation of the oscillator. . Call RungeKuttad to solve the equation of the oscillator using 1000 evenly distributed points in the interval (0, 107]. Use pyplot to plot the solution. Return 1D Numpy arrays with the values of t used for the solution and the solution in terms of displacements. Pass the corresponding tests in the provided test_coursework.py file. [7 marks] (d) Docstrings and comments for the class and all its methods. [2 marks]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started