340 likes | 480 Views
ME451 Kinematics and Dynamics of Machine Systems. Numerical Integration. Stiffness: Implicit Methods. October 30, 2013. Radu Serban University of Wisconsin-Madison. Before we get started…. Last Time: Recovering constraint reaction forces Started discussion on numerical integration
E N D
ME451 Kinematics and Dynamics of Machine Systems Numerical Integration. Stiffness: Implicit Methods. October 30, 2013 Radu Serban University of Wisconsin-Madison
Before we get started… • Last Time: • Recovering constraint reaction forces • Started discussion on numerical integration • Today • Stiff differential equations • Implicit numerical integration formulas • Assignments: • Homework 9 – 6.3.3, 6.4.1 – due November 4 (12:00pm) • Matlab 7 – due tonight, October 30, Learn@UW (11:59pm) • Project 1 – due Wednesday, November 6, Learn@UW (11:59pm) • Your simEngine2D can now perform complete Kinematic analysis! • Test the provided visualization function (available on the SBEL course webpage) • Miscellaneous • Draft proposals for the Final Project due on Friday, November 1 • Midterm 2 – Wednesday, November 6, 12:00pm in ME 1143 • Review session – Monday, November 4, 6:30pm in ME 1143
Basic Concept • IVP • In general, all we can hope for is approximating the solution at a sequence of discrete points in time • Uniform grid (constant step integration) • Adaptive grid (variable step integration) • The numerical solution is then the sequence of approximations • Basic idea: somehow turn the differential problem into an algebraic problem (approximate the derivatives)
Simplest method: Forward Euler • Starting from the IVP • Use the simplest approximation to the derivative • Rewrite the above asand use ODE to obtain Forward Euler Method with constant step-size
FE: Geometrical Interpretation • IVP • Forward Euler integration formula
FE: Example • Solve the IVPusing Forward Euler (FE) with a step-size • Compare to the exact solution
Forward Euler: Effect of Step-Size % IVP (RHS + IC) f = @(t,y) -0.1*y + sin(t); y0 = 0; tend = 50; % Analytical solution y_an = @(t) (0.1*sin(t) - cos(t) + exp(-0.1*t)) / (1+0.1^2); % Loop over the various step-size values and plot errors colors = [[0, 0.4, 0]; [1, 0.5, 0]; [0.6, 0, 0]]; Figure, hold on, box on h = [0.001 0.01 0.1]; for ih = 1:length(h) tspan = 0:h(ih):tend; y = zeros(size(tspan)); err = zeros(size(tspan)); y(1) = y0; err(1) = 0; for i = 2:length(tspan) y(i) = y(i-1) + h(ih) * f(tspan(i-1), y(i-1)); err(i) = y(i) - y_an(tspan(i)); end plot(tspan, err, 'color', colors(ih,:)); end legend('h = 0.001', 'h = 0.01', 'h = 0.1'); FE errors for different values of the step-size
FE: Effect of Step-Size % IVP (RHS + IC) f = @(t,y) -0.1*y + sin(t); y0 = 0; tend = 50; % Loop over the various step-size values and plot errors colors = [[0, 0.4, 0]; [1, 0.5, 0]; [0.6, 0, 0]]; Figure, hold on, box on h = [0.1 1.0 5.0]; for ih = 1:length(h) tspan = 0:h(ih):tend; y = zeros(size(tspan)); y(1) = y0; for i = 2:length(tspan) y(i) = y(i-1) + h(ih) * f(tspan(i-1), y(i-1)); end plot(tspan,y, 'color', colors(ih,:)) end legend('h = 0.1', 'h = 1', 'h = 5'); FE accuracy at different values of the step-size
A Simple IVP Example • Consider the IVPwhose analytical solution is • 5 integration steps with Forward Euler formula, , , • Compare the global error (difference between analytical and numerical solution) Error for 0 0.36787944117144 0.13533528323661 0.04978706836786 0.01831563888873 0.00673794699909 Error for 0 2.04978706836786 -3.99752124782333 8.00012340980409 -15.99999385578765 32.00000030590232 Error for 0 0.01873075307798 0.03032004603564 0.03681163609403 0.03972896411722 0.04019944117144
A Simple IVP Example Analytical Solution Forward Euler
Stiff Differential Equations • Problems for which explicit integration methods (such as Forward Euler) do not work well • Other explicit formulas: Runge-Kutta (RK4), DOPRI5, Adams-Bashforth, etc. • Stiff problems require a different class of integration methods: implicit formulas • The simplest implicit integration formula: Backward Euler (BE)
BE: Geometrical Interpretation • IVP • Forward Euler integration formula • Backward Euler integration formula
A Simple IVP Example Analytical Solution Backward Euler Forward Euler
Backward-Difference Formulas (BDF) • IVP • Also known as Gear method (DIFSUB – 1971) • Family of implicit linear multi-step formulas • BDF of 1st order: • BDF of 2nd order: • BDF of 3rd order: • BDF of 4th order: • BDF of 5th order: C.W. (Bill) Gear b. 1935
Curtiss & Hirschfelder Example Forward Euler Backward Euler C.F. Curtiss and J.O. Hirschfelder – “Integration of Stiff Equations”Proc. Nat. Acad. Sci, USA (1952)
Two Key Properties of a Numerical Integrator Two properties are relevant when considering a numerical integrator for finding an approximation of the solution of an IVP • Stability • Accuracy
Stability of a Numerical Integrator • The problem: • How big can the integration step-size be without the numerical solution blowing up? • Tough question, answered in a Numerical Analysis class • Different integration formulas, have different stability regions • We would like to use an integration formula with large stability region: • Example: Backward Euler, BDF methods, Newmark, etc. • Why not always use these methods with large stability region? • There is no free lunch: these methods are implicit methods that require the solution of an algebraic problem at each step
Accuracy of a Numerical Integrator • The problem: • How accurate is the formula that we are using? • If we decrease , how will the accuracy of the numerical solution improve? • Tough question, answered in a Numerical Analysis class • Examples: • Forward and Backward Euler: accuracy • RK45: accuracy • Why not always use methods with high accuracy order? • There is no free lunch: these methods usually have very small stability regions • Therefore, they are limited to using very small values of
Implicit Integration and Nonlinear Systems[Examples] • Backward Euler (BE) formula: • Apply one step of BE for the following IVPs ()
Implicit Integration and Nonlinear Systems[Example 1] • Backward Euler (BE) formula: • Apply one step of BE for the following IVP () • This example shows that discretization with an implicit integration formula requires the solution of an algebraic equation
Implicit Integration and Nonlinear Systems[Example 2] • Backward Euler (BE) formula: • Apply one step of BE for the following IVP () • This example shows that the solution of the resulting algebraic equation raises additional problems (selecting one of multiple possible solutions)
Implicit Integration and Nonlinear Systems[Example 3] • Backward Euler (BE) formula: • Apply one step of BE for the following IVP () • This example shows that, in general, the resulting nonlinear algebraic problem can only be solved numerically.
Implicit Integration and Nonlinear Systems[Examples] • Backward Euler (BE) formula: • Apply one step of BE for the following IVPs () • Upon discretization with an implicit integration formula (such as BE), the approximation to the solution of the DE at the new time is obtained by solving an algebraic equation • In general, the resulting (system of) nonlinear equation(s) can only be solved numerically (using for example the Newton-Raphson method)
Implicit Integration: Conclusions • Stiff problems require the use of implicit integration methods • Because they have very good stability, implicit integration methods allow for step-sizes that could be orders of magnitude larger than those needed if using explicit integration • However, for most real-life IVPs, discretization using an implicit integration formula leads to another nasty problem: • To find the solution at the new time, we must solve a nonlinear algebraic problem • This brings back into the picture the Newton-Raphson method (and its variants) • We have to deal with providing a good starting point (initial guess), computing the matrix of partial derivatives, etc.
General Call to a Matlab ODE Solver • IVP: • Example of using Matlab’sode45: • Use odesetto create an ODE options structure to change default values for various solver parameters [t,y] = ode45(odefun,tspan,y0,options) function dydt = odefun(t,y) y0 [t0 tend] options = odeset(…)
Example of IVP Solution with Matlab Convert to 1st order ODE >> [t,y]=ode45(‘rhs’,[0 20],[2;0]); >> plot(t, y); Convert tovector form function yd = rhs(t, y) yd = [y(2); (1-y(1)^2)*y(2)-y(1)];
ODE Solvers in MATLAB Use ode45 for non-stiff problems and ode15s for stiff problems
A Stiff Problem ---- ode45 solver statistics ---- 7557 successful steps 504 failed attempts 48367 function evaluations Elapsed time is 0.564820 seconds. ---- ode15s solver statistics ---- 139 successful steps 3 failed attempts 288 function evaluations 1 partial derivatives 27 LU decompositions 284 solutions of linear systems Elapsed time is 0.188120 seconds. % specify RHS function rhs = @(t,y) [-1,-1;1,-5000]*y; % specify accuracy and turn on stats option options = odeset('RelTol',1e-6,'Stats','on'); % call the ode45 solver fprintf('\n---- ode45 solver statistics ----\n') tic, [~,~] = ode45(rhs, [0, 5] , [1; 1], options); toc % call the ode15s solver fprintf('\n---- ode15s solver statistics ----\n') tic, [~,~] = ode15s(rhs, [0, 5] , [1; 1], options); toc • On a stiff problem, • an explicit solver is forced to take small steps to ensure stability • an implicit solver can take much larger steps, which reduces overall time to solution; but it must solve algebraic equations at each time step