MAE 376: Lab 5 Boundary Value Problems & Solutions of Nonlinear Equations A Boundary Value Problem This problem emphasizes the importance of the choice of finite-difference rules when discretizing differential equations. Consider an example of the classical convection-diffusion transport equation, which describes the transport of a fluid due to convection and diffusion: νu00 − au0 + 1 = 0, u(0) = 0, x ∈ [0, 1] u(1) = 0 Depending on the so-called Peclet number P e = a∆x 2ν , where ∆x is the spacing between nodes in the finite difference approximation, we can get spurious behavior in the numerical solution if we use a central difference method for the convection (first-order derivative) term. In particular, if P e > 1 (convectiondominated), then a different scheme should be used. Analytical solution of this differential equation is: a u(x) = a x − x ev + evx − 1 a a − a ev Assignments for Part A (To be graded) (A1 ) (Method 1) By hand, discretize the differential equation using a central difference approximation for both terms and write down the resulting equation in terms of ui , ui−1 and ui+1 . (Note: when P e > 1, this scheme will be “unstable”). (A2 ) (Method 2) By hand, discretize the differential equation using a backward difference rule for the first-order derivative and a central difference rule for the second-order derivative. Write down the resulting equation in terms of ui , ui−1 and ui+1 . (This alternative scheme is referred to as “upwinding”). (A3 ) By hand, express the equation obtained in (A1 ) in matrix form for an arbitrary number of points N , using the form that includes boundary points in u (your matrix should be N -by-N ). (A4 ) By hand, express the equation obtained in (A2 ) in matrix form for an arbitrary number of points N , using the form that includes boundary points in u (your matrix should be N -by-N ). (A5 ) Using your work for parts (A1 )-(A4 ), write three MATLAB codes as described below: (a) a user-defined function named centraldiff.m that takes in three inputs consisting of a, ν and N , performs method 1 mentioned in (A1 ) and (A3 ), and returns the numerical solution for u and the Peclet number. (b) a user-defined function named upwinding.m that takes in three inputs consisting of a, ν and N , performs method 2 mentioned in (A2 ) and (A4 ), and returns the numerical solution for u and the Peclet number. (c) a MATLAB script named lab5 bvp.m that calls the above functions and produces the following figures: 1 i. Take a = ν = 2, and N = 26, Compute the numerical solution using both methods, plot both of them as well as the analytical solution on one graph. ii. Take a = 1000, ν = 2, and N = 26, Compute the numerical solutions using both method, plot both of them, as well as the analytical solution, on one graph. (Note: you will observe a “weird” result in this figure.) (A6 ) For the two ODE parameters in (A5 )(c)ii(a = 1000, ν = 2), estimate how many discretization points N 0 it requires to eliminate the “weird” phenomenon mentioned in (A5 )(c)ii. You must provide a (brief) justification of how you obtain this value for N 0 as a comment (hint: think about the Peclet number). Plot the two numerical solutions using this N 0 together with the analytical solution on one graph. For all of the figures in this section, indicate the corresponding parameters a, ν, and N as well as the Peclet number in the title and make sure the three curves are distinguishable from each other by using different line styles, markers, colors and a legend. B Solutions of Nonlinear Equations The goal of this exercise is to study the behavior of a simple mechanical system, in particular the Acrobot. The Acrobot is a two-link revolute planar robot with one actuator at the elbow, as shown in Fig. 1. The Acrobot is a robot arm that has been studied as a challenging control problem in Control Theory, AI, Robotics, and Machine Learning. The current Acrobat is composed of light rigid bars (L1 and L2 ) connected to heavy spheres located at points A, B, and O. Suppose that the location of B is experimentally measured for a given period of time. The provided xB and yB data has noise, which is natural given that measurements are usually noisy due to ambient noise, the vibration of the system itself, malfunctions in the instruments, among others. You will use the input xB (t) and yB (t) to compute the resulting θ1 (t) and θ2 (t). Figure 1: A schematic of a simple piston mechanism. The vectors correspond to the location of the piston p are illustrated. 2 Assignments for Part B (To be graded) (B 1 ) It can easily be shown that the position of point B is given by: xB = L1 cos θ1 + L2 cos(θ1 + θ2 ) yB = L1 sin θ1 + L2 sin(θ1 + θ2 ) (1) (Note: θ can be either positive or negative depending on the direction of rotation. If the rotation is clockwise θ < 0 and if the rotation is counterclockwise θ > 0.) (B 2 ) Based on equation 1, rewrite the system of equations for the position of point B in the form: f (θ , θ ) f (θ1 , θ2 ) = 1 1 2 = 0 (2) f2 (θ1 , θ2 ) where θ1 (t) and θ2 (t) are the unknowns. You may assume L1 , L2 , xB , and yB are given. (B 3 ) Based on the system of equations f (θ1 , θ2 ), write the in the Newton-Raphson method to solve the system defined as: ∂f1 ∂θ1 J(z) = ∂f2 ∂θ1 Jacobian matrix J that will be implemented of equations. i.e. the Jacobian matrix J is ∂f1 ∂θ2 ∂f2 ∂θ2 (3) where f is the system of equations, J is the Jacobian, and z = [θ1 , θ2 ]T . (B 4 ) Write a user-defined MATLAB function called acrobot.m that uses the Newton-Raphson method to solve the system of equations defined previously. The function will take as an input seven parameters; L1 , L2 , xB , yB , the tolerance (tol), and the initial guesses θ10 and θ20 . The function will output two solutions θ1 and θ2 . The function should look like: [theta 1,theta 2] = acrobot(L1,L2,xB,yB,tol,theta 1 0,theta 2 0) The Newton-Raphson method is a root-finding algorithm that produces a successively better approximations to the roots of the problem. With a good initial condition of the root, the Newton-Raphson method proceeds by solving the algorithm below, J(zn )∆zn = −f(zn ) zn+1 = zn + ∆zn (4) (5) where f is the system of equations, J is the Jacobian, and z = [θ1 , θ2 ]. The iterative code will stop when the L2 norm ||f(zn+1 )||2 converges to a value smaller than the user defined tolerance. i.e ||f(zn+1 )||2 < tolerance (6) Hint: To check if your function is working right, try the following parameters L1 = 2 m, L2 = 1 m, xB = 2.5 m, yB = 1.5m, tolerance = 10−3 and your initial guesses to be θ10 = 0 & θ20 = π/4. The output answer should be θ1 = 0.37291 and θ2 = 0.50736. (B 5 ) Write a MATLAB script named lab5 acrobot.m where you load the data in the csv file “data.csv” using the MATLAB built in command csvread(‘filename’). The data is a three-dimensional array containing the sampling values of time ti in the first column and the corresponding values of xB (ti ) and yB (ti ) in the next two columns. For each value of xB (ti ) and yB (ti ) solve the system of equations for θ1 (ti ) and θ2 (ti ) by calling the acrobot function you created in (B 4 ). Use tolerance = 10−3 , L1 = 2.0, L2 = 1.0, and the first pair of initial guesses are θ10 = 0 & θ20 = π/4. Plot xA , yA , xB , and yB versus time (you can obtain xA and yA from the solutions of θ1 (ti ), where 3 xA = L1 ∗ cos(θ1 ) and yA = L1 ∗ sin(θ1 )). Four curves must be distinguishable from each other using different line styles and colors and a legend. (Bonus) Visualize the physical movement of the acrobot by plotting (0,yA ,yB ) versus (0,xA ,xB ) with lines in between for every time step. The legend should indicate the origin, point A and point B, see an example in Fig. 2. (Please submit this figure with the rest of the figures in the same zipped folder as explained below.) Figure 2: Example trace plot Submission instruction: • A zipped folder named “Lab5_person#.zip” containing: 1. a PDF file showing your work for parts (A1 )-(A4 ), and your vector system of equations and the Jacobian for parts (B 2 ) and (B 3 ) 2. three figures for (A5 )(c)i, (A5 )(c)ii and (A6 ) 3. one figure for part (B 5 ). 4. all five of your MATLAB codes (.m files) from (A5 )a,(A5 )b,(A5 )c,(B 4 ) and (B 5 ). 4 Practice Problem 1: BVP - 1-D Rod Mixed BCs (will NOT be graded) Figure 3: 1-D Heat Transfer in a Rod d2 T + h(T∞ − T ) = 0 dx2 0≤x≤L dT (0) =F dx T (L) = Tb Parameter: T∞ = 200 & h = 0.05 & L = 10 & F =0 & Tb = 400 Discretize the problem with central difference method, solve the problem in three different meshes (coarsemedium-fine) and plot T vs. x on one graph and compare with the analytical solution: √ √ Tb − T∞ √ √ T (x) = ex h + e−x h + T∞ eL h + e−L h Bonus: use MATLAB function contourf() to visualize the heated rod. 5 Practice Problem 2: Nonlinear system (will NOT be graded) In this exercise, we shall find the points of intersection between a circle and the infinity sign as illustrated in the figure. The equations that govern the circle and infinity sign, respectively, are given by: (x − π/2)2 + y 2 = 1 sin(x) cos(x)2 − y 2 = 0 (7) 1. Plot the two curves. 2. Write a MATLAB code that implements the Newton-Raphson method to find the four intersection points. i.e. the code should solve the following equations J(zn )∆zn = −f(zn ) zn+1 = zn + ∆zn (8) (9) where f is the system of equations, J is the Jacobian, and z = [x, y]. Run the code until the infinity norm ||f(zn+1 )||∞ converges to value smaller than 10−3 . i.e ||f(zn+1 )||∞ < 10−3 (10) ||f(zn+1 )||∞ = max |f(zn+1 )| (11) where 3. Plot the intersection points. 6