Using MATLAB (779505), страница 54
Текст из файла (страница 54)
The first step is to build thefunction to be evaluated. The function must be capable of returning a vectoroutput when given a vector input. You must also consider which variable is inthe inner integral, and which goes in the outer integral. In this example, theinner variable is x and the outer variable is y (the order in the integral is dxdy).In this case, the integrand function isfunction out = integrnd(x,y)out = y*sin(x) + x*cos(y);To perform the integration, two functions are available in the funfun directory.The first, dblquad, is called directly from the command line.
This M-fileevaluates the outer loop using quad. At each iteration, quad calls the secondhelper function that evaluates the inner loop.14-20Numerical Integration (Quadrature)To evaluate the double integral, useresult = dblquad(@integrnd,xmin,xmax,ymin,ymax);The first argument is a string with the name of the integrand function. Thesecond to fifth arguments arexminLower limit of inner integralxmaxUpper limit of the inner integralyminLower limit of outer integralymaxUpper limit of the outer integralHere is a numeric example that illustrates the use of dblquad.xmin =xmax =ymin =ymax =resultpi;2*pi;0;pi;= dblquad(@integrnd,xmin,xmax,ymin,ymax)The result is -9.8698.By default, dblquad calls quad.
To integrate the previous example using quadl(with the default values for the tolerance argument), useresult = dblquad(@integrnd,xmin,xmax,ymin,ymax,[],@quadl);Alternatively, any user-defined quadrature function name can be passed todblquad as long as the quadrature function has the same calling and returnarguments as quad.14-2114Function Functions14-2215Differential EquationsInitial Value Problems for ODEs and DAEs . . . . .ODE Function Summary . . . .
. . . . . . . . . .Introduction to Initial Value ODE Problems . . . . . . .Initial Value Problem Solvers . . . . . . . . . . . .Representing ODE Problems . . . . . . . . . . . . .Improving ODE Solver Performance . . . . . . . . . .Examples: Applying the ODE Initial Value Problem SolversQuestions and Answers, and Troubleshooting . . . . . .. 15-3. 15-3.
15-6. 15-615-1015-1615-3115-50Boundary Value Problems for ODEs . . . .BVP Function Summary . . . . . . . . . . .Introduction to Boundary Value ODE Problems .Boundary Value Problem Solver . . . . . . .Representing BVP Problems . . . . .
. . . .Using Continuation to Make a Good Initial GuessImproving BVP Solver Performance . . . . . .............................15-5715-5715-5915-6015-6315-6815-75Partial Differential Equations . . . . .PDE Function Summary . . . . . . . .Introduction to PDE Problems . . . . . .MATLAB Partial Differential Equation SolverRepresenting PDE Problems . . . . . . .Improving PDE Solver Performance . . . .Example: Electrodynamics Problem . . . .............................15-8215-8215-8315-8415-8815-9315-94Selected Bibliography...............
. . . . . . . . . . . . . 15-9915Differential EquationsThis chapter treats the numerical solution of differential equations usingMATLAB. It includes:Initial Value Problems for ODEs and DAEsDescribes the solution of ordinary differential equations (ODEs) anddifferential-algebraic equations (DAEs), where the solution of interest satisfiesinitial conditions at a given initial value of the independent variable.Boundary Value Problems for ODEsDescribes the solution of ODEs, where the solution of interest satisfies certainboundary conditions. The boundary conditions specify a relationship betweenthe values of the solution at the initial and final values of the independentvariable.Partial Differential EquationsDescribes the solution of initial-boundary value problems for systems ofparabolic and elliptic partial differential equations (PDEs) in one spatialvariable and time.Selected BibliographyLists published materials that support concepts described in this chapter.Note In function tables, commonly used functions are listed first, followed bymore advanced functions.
The same is true of property tables.15-2Initial Value Problems for ODEs and DAEsInitial Value Problems for ODEs and DAEsThis section describes how to use MATLAB to solve initial value problems(IVPs) of ordinary differential equations (ODEs) and differential-algebraicequations (DAEs). It provides:• A summary of the MATLAB ODE solvers, related functions, and examples• An introduction to ODEs and initial value problems• Descriptions of the ODE solvers and their basic syntax• General instructions for representing an IVP in MATLAB• A discussion about changing default integration properties to improve solverperformance• Examples of the kinds of IVP problems you can solve in MATLAB• Answers to some frequently asked questions about the MATLAB ODEsolvers, and some troubleshooting suggestionsODE Function SummaryInitial Value ODE Problem SolversThese are the MATLAB initial value problem solvers.
The table lists the kindof problem you can solve with each solver, and the method each solver uses.SolverSolves These Kinds of ProblemsMethodode45Nonstiff differential equationsRunge-Kuttaode23Nonstiff differential equationsRunge-Kuttaode113Nonstiff differential equationsAdamsode15sStiff differential equations and DAEsNDFs (BDFs)ode23sStiff differential equationsRosenbrockode23tModerately stiff differential equationsand DAEsTrapezoidal ruleode23tbStiff differential equationsTR-BDF215-315Differential EquationsODE Solution EvaluationIf you call an ODE solver with one output argument, it returns a structure thatyou can use to evaluate the solution at any point on the interval of integration.FunctionDescriptiondevalEvaluate the numerical solution using output of ODE solvers.ODE Option HandlingAn options structure contains named integration properties whose values arepassed to the solver, and which affect problem solution.
Use these functions tocreate, alter, or access an options structure.FunctionDescriptionodesetCreate or alter options structure for input to ODE solvers.odegetExtract properties from options structure created with odeset.ODE Solver Output FunctionsIf an output function is specified, the solver calls the specified function afterevery successful integration step.
You can use odeset to specify one of thesesample functions as the OutputFcn property, or you can modify them to createyour own functions.15-4FunctionDescriptionodeplotTime-series plotodephas2Two-dimensional phase plane plotodephas3Three-dimensional phase plane plotodeprintPrint to command windowInitial Value Problems for ODEs and DAEsODE Initial Value Problem ExamplesThese examples illustrate the kinds of problems you can solve in MATLAB.From the MATLAB Help browser, click the example name to see the code in aneditor.
Type the example name at the command line to run it.Note The Differential Equations Examples browser enables you to view thecode for the ODE examples and DAE examples. You can also run the examplesfrom the browser. Click on these links to invoke the browser, or typeodeexamples('ode')or odeexamples('dae')at the command line.ExampleDescriptionamp1daeStiff DAE – electrical circuitballodeSimple event location – bouncing ballbatonodeODE with time- and state-dependent mass matrix – motionof a batonbrussodeStiff large problem – diffusion in a chemical reaction (theBrusselator)burgersodeODE with strongly state-dependent mass matrix – Burger’sequation solved using a moving mesh techniquefem1odeStiff problem with a time-dependent mass matrix – finiteelement methodfem2odeStiff problem with a constant mass matrix – finite elementmethodhb1daeStiff DAE from a conservation lawhb1odeStiff problem solved on a very long interval – Robertsonchemical reactionorbitodeAdvanced event location – restricted three body problem15-515Differential EquationsExampleDescriptionrigidodeNonstiff problem – Euler equations of a rigid body withoutexternal forcesvdpodeParameterizable van der Pol equation (stiff for large µ )Introduction to Initial Value ODE ProblemsWhat Is an Ordinary Differential EquationThe MATLAB ODE solvers are designed to handle ordinary differentialequations.
An ordinary differential equation contains one or more derivativesof a dependent variable y with respect to a single independent variable t ,usually referred to as time. The derivative of y with respect to t is denoted asy′ , the second derivative as y′′ , and so on. Often y(t) is a vector, havingelements y 1, y 2, …, y n .Using Initial Conditions to Specify the Solution of InterestGenerally there are many functions y(t) that satisfy a given ODE, andadditional information is necessary to specify the solution of interest. In aninitial value problem, the solution of interest satisfies a specific initialcondition, that is, y is equal to y 0 at a given initial time t 0 . An initial valueproblem for an ODE is theny′ = f ( t, y )y ( t0 ) = y0(15-1)If the function f(t, y) is sufficiently smooth, this problem has one and only onesolution.
Generally there is no analytic expression for the solution, so it isnecessary to approximate y(t) by numerical means, such as using one of theMATLAB ODE solvers.Initial Value Problem SolversThe MATLAB ODE solver functions implement numerical integration methodsfor solving IVPs for ODEs (Equation 15-1). Beginning at the initial time withinitial conditions, they step through the time interval, computing a solution ateach time step. If the solution for a time step satisfies the solver’s error15-6Initial Value Problems for ODEs and DAEstolerance criteria, it is a successful step.
Otherwise, it is a failed attempt; thesolver shrinks the step size and tries again.This section describes:• MATLAB solvers for nonstiff ODE problems and solvers for stiff ODEproblems• ODE solver basic syntax• Additional ODE solver arguments“Mass Matrix and DAE Properties” on page 15-27 explains how to solve moregeneral problems.You can also use the MATLAB Help browser to get information on the syntaxfor any MATLAB function, as well as information on example files for thesesolvers.Solvers for Nonstiff ProblemsThere are three solvers designed for nonstiff problems:ode45Based on an explicit Runge-Kutta (4,5) formula, theDormand-Prince pair.
















