matlab中mathematics+工具箱使用.doc

上传人:豆**** 文档编号:17421405 上传时间:2022-05-24 格式:DOC 页数:19 大小:387KB
返回 下载 相关 举报
matlab中mathematics+工具箱使用.doc_第1页
第1页 / 共19页
matlab中mathematics+工具箱使用.doc_第2页
第2页 / 共19页
点击查看更多>>
资源描述

《matlab中mathematics+工具箱使用.doc》由会员分享,可在线阅读,更多相关《matlab中mathematics+工具箱使用.doc(19页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、【精品文档】如有侵权,请联系网站删除,仅供学习与交流matlab中mathematics+工具箱使用.精品文档.Matlab中Mathematics 工具箱使用第8章 最优化方法8.1最优化非线性函数工具箱提供的Humps函数,其图像如下: 函数fminbnd可求单变量函数在给定区间的局部最小点,如x=fminbnd(humps,0.3,1) x=0.6370通过设置第4个参数optimset可实现显示迭代列表,如x = fminbnd(humps,0.3,1,optimset(Display,iter) fminsearch 函数求多变量函数的局部极小点。首先,创建三变量函数three_va

2、r b=(x) x(1)2+2.5*sin(x(2)-x(3)2*x(1)2*x(2)2a=fminsearch(b,-0.6,-1.2,0.135) 0.0000 -1.5708 0.1803第10章 微积分ODE求解器Solver(求解器)Solves These Kinds of Problems (求解问题)Method(方法)ode45Nonstiff differential equations(非刚性微分方程)Runge-Kuttaode23Nonstiff differential equations(非刚性微分方程)Runge-Kuttaode113Nonstiff diff

3、erential equations (非刚性微分方程)Adamsode15sStiff differential equations and DAEs(非刚性微分-代数方程)NDFs (BDFs)ode23sStiff differential equations (刚性微分方程) Rosenbrockode23tModerately stiff differential equations and DAEs(中等刚性微分方程和代数方程)Trapezoidal ruleode23tbStiff differential equations(刚性微分方程)TR-BDF2ode15iFully

4、implicit differential equations(全隐式微分方程)BDFsEvaluation and Extension(赋值和延拓)You can use the following functions to evaluate and extend solutions to ODEs.你能应用如下函数对ODE的数值解解进行赋值和延拓。FunctionDescriptiondeval Evaluate the numerical solution using the output of ODE solvers(用ODE输出对数值解进行赋值)odextendExtend the

5、solution of an initial value problem for an ODE对ODE初值问题的解进行延拓Solver Options(求解器选项)An options structure contains named properties whose values are passed to ODE solvers, and which affect problem solution. Use these functions to create, alter, or access an options structure. 选项结构包含署名属性,其值传递给ODE求解器以影响问

6、题求解。用这些函数可以创建,改变和接受选项结构。Function(函数) Description(描述)odeset Create or alter options structure for input to ODE solver.(创建和改变选项)odeget Extract properties from options structure created with odeset. (提取属性选项)Output Functions(输出函数)If an output function is specified, the solver calls the specified functio

7、n after every successful integration step. You can use odeset to specify one of these sample functions as the OutputFcn property, or you can modify them to create your own functions.如果输出函数被指定,则求解器在每步积分后调用该函数进行输出。你能够用odeset 指定这些例子函数之一作为OutputFcn属性,或创建自己的函数对其进行修改。FunctionDescriptionodeplotTime-series

8、plot(时间序列图形)odephas2 Two-dimensional phase plane plot(2-维相平面图形)odephas3Three-dimensional phase plane plot(3-维相平面图形)odeprint Print to command window (打印到命令窗)First Order ODEs(一阶ODEs)An ordinary differential equation (ODE) contains one or more derivativesof a dependent variable y with respect to a sing

9、le independent variable t,usually referred to as time. The derivative of y with respect to t is denotedas y , the second derivative as y , and so on. Often y(t) is a vector, havingelements y1, y2, ., yn.MATLAB solvers handle the following types of first-order ODEs: Explicit ODEs of the form y = f (t

10、, y) 形如y = f (t, y)的显式ODEs Linearly implicit ODEs of the form M(t, y) y = f (t, y), where M(t, y) isa matrix形如M(t, y) y = f (t, y)的线性隐式ODEs Fully implicit ODEs of the form f (t, y, y) = 0 (ode15i only):形如f (t, y, y) = 0的全隐式ODEsHigher Order ODEs(高阶ODEs)MATLAB ODE solvers accept only first-order diffe

11、rential equations. To usethe solvers with higher-order ODEs, you must rewrite each equation as anequivalent system of first-order differential equations of the formy = f(t,y)You can write any ordinary differential equationy(n) = f(t,y,y,.,y(n 1)as a system of first-order equations by making the subs

12、titutionsy1 = y, y2 = y,., yn = y(n 1)y1= y, y2 = y, . , yn = y(n 1)The result is an equivalent system of n first-order ODEs.Rewrite the second-order van der Pol equationas a system of first-order ODEs.Initial Values(初值问题)Generally there are many functions y(t) that satisfy a given ODE, andadditiona

13、l information is necessary to specify the solution of interest. Inan initial value problem, the solution of interest satisfies a specific initialcondition, that is, y is equal to y0 at a given initial time t0. An initial valueproblem for an ODE is thenIf the function f (t, y) is sufficiently smooth,

14、 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.Nonstiff Problems(刚性问题)There are three solvers designed for nonstiff problems:对于非刚性问题求解有3个求解器。ode45 Based on an explicit Runge-Kutta (4,5)

15、formula,the Dormand-Prince pair. It is a one-step solver in computing y(tn), it needs only the solution at the immediately preceding time point, y(tn1). In general,ode45 is the best function to apply as a “first try” for most problems. ode45 基于显式Runge-Kutta(4,5)公式,Dormand-Prince 对,它是计算y(tn)的单步求解器,只需

16、前一步的解y(tn-1).一般说来,ode45是对大多数问题的“第一试”的最好的函数。 ode23 Based on an explicit Runge-Kutta (2,3) pair of Bogacki and Shampine. It may be more efficient than ode45 at crude tolerances and in the presence of mild stiffness.Like ode45, ode23 is a one-step solver.ode113 Variable order Adams-Bashforth-Moulton PE

17、CE solver. It may be more efficient than ode45 at stringent tolerances and when the ODE function is particularly expensive to evaluate. ode113 is a multistep solverit normally needs the solutions at several preceding time points to compute the current solution.Stiff Problems(刚性问题)Not all difficult p

18、roblems are stiff, but all stiff problems are difficult forsolvers not specifically designed for them. Solvers for stiff problems can beused exactly like the other solvers. However, you can often significantlyimprove the efficiency of these solvers by providing them with additionalinformation about

19、the problem. (See “Integrator Options” on page 10-9.)There are four solvers designed for stiff problems: 并不是所有困难的问题都是刚性的,但是所有的刚性问题对于非专门为此设计的求解器来说都是困难的。Solver Syntax(求解语法)All of the ODE solver functions, except for ode15i, share a syntax that makesit easy to try any of the different numerical methods

20、, if it is not apparentwhich is the most appropriate. To apply a different method to the sameproblem, simply change the ODE solver function name. The simplest syntax,common to all the solver functions, is求解函数调用t,y = solver(odefun,tspan,y0,options)where solver is one of the ODE solver functions liste

21、d previously.其中,solver是如前列举的ODE求解函数The basic input arguments are举例:van der Pol Equation (Nonstiff)This example illustrates the steps for solving an initial value ODE problem:该例说明了求解ODE初值问题的步骤:1 Rewrite the problem as a system of first-order ODEs. Rewrite thevan der Pol equation (second-order)1.把高阶方程

22、表示为一阶方程组的等价形式。van der Pol 方程(二阶)where 0 is a scalar parameter, by making the substitution y1 = y2. Theresulting system of first-order ODEs is为标量常数。做代换,得到对应的一阶方程组2 Code the system of first-order ODEs. Once you represent the equationas a system of first-order ODEs, you can code it as a function that a

23、n ODEsolver can use. The function must be of the form2. 对一阶ODE方程组编写ODEfun函数,其形式为dydt = odefun(t,y)odefun函数程序function dydt = vdp1(t,y)dydt = y(2); (1-y(1)2)*y(2)-y(1);3. Apply a solver to the problem.(调用求解函数求解)Decide which solver you want to use to solve the problem. Then call thesolver and pass it t

24、he function you created to describe the first-order systemof ODEs, the time interval on which you want to solve the problem, andan initial condition vector.决定用哪个求解函数求解问题,然后调用求解器,把创建的描述方程组的函数,求解区间,和初始条件传递给求解函数。For the van der Pol system, you can use ode45 on time interval 0 20 withinitial values y(1)

25、 = 2 and y(2) = 0.对于 Van der Pol 系统,你可以用ode45在时间区间0,20进行积分,初值条件为y(1)=2和y(2)=0,其调用求解函数的方法为t,y = ode45(vdp1,0 20,2; 0);4 View the solver output. You can simply use the plot command to viewthe solver output.4.视图求解输出。你能够用plot命令视图解输出。plot(t,y(:,1),-,t,y(:,2),-)title(Solution of van der Pol Equation, mu =

26、 1);xlabel(time t);ylabel(solution y);legend(y_1,y_2)As an alternative, you can use a solver output function to process the output.The solver calls the function specified in the integration property OutputFcnafter each successful time step. Use odeset to set OutputFcn to the desiredfunction. See Sol

27、ver Output Properties, in the reference page for odeset, formore information about OutputFcn.作为选择,你能够用求解器输出处理输出。van der Pol Equation (Stiff)(刚性van der Pol 方程)This example presents a stiff problem. For a stiff problem, solutions canchange on a time scale that is very short compared to the interval of

28、integration, but the solution of interest changes on a much longer time scale.Methods not designed for stiff problems are ineffective on intervals where thesolution changes slowly because they use time steps small enough to resolvethe fastest possible change.该例表示的是一个刚性问题。对于刚性问题,解在相对积分区间非常小的时间尺度上变化,感

29、兴趣的解在更大的时间尺度上变化。不是专为刚性问题设计的方法在解变换缓慢的区间上是无效的,因为它用的时间步长非常小以适应分辨快速变化。When is increased to 1000, the solution to the van der Pol equationchanges dramatically and exhibits oscillation on a much longer time scale.Approximating the solution of the initial value problem becomes a moredifficult task. Because

30、 this particular problem is stiff, a solver intended fornonstiff problems, such as ode45, is too inefficient to be practical. A solversuch as ode15s is intended for such stiff problems.当增加到1000,vdp方程的解急剧变化,在更大时时间尺度上展示振荡。近似初值问题的解是一个困难的问题。因为这个特别的问题是刚性的,对于如ode45这样的非刚性求解器实际上是效率非常低的。象ode15s是适用于刚性问题的。The

31、vdp1000 function evaluates the van der Pol system from the previousexample, but with = 1000.vdp1000函数对的vdp方程组进行赋值。function dydt = vdp1000(t,y)dydt = y(2); 1000*(1-y(1)2)*y(2)-y(1);Now use the ode15s function to solve the problem with the initial conditionvector of 2; 0, but a time interval of 0 3000

32、. For scaling reasons, plotjust the first component of y(t).现在用ods15s求解具有初值向量2;0和积分区间0,3000上的初值问题。为尺度原因,仅画出的第一分量。t,y = ode15s(vdp1000,0 3000,2; 0);plot(t,y(:,1),-);title(Solution of van der Pol Equation, mu = 1000);xlabel(time t);ylabel(solution y_1);van der Pol Equation (Parameterizing the ODE)(参数v

33、dp方程)The preceding sections showed how to solve the van der Pol equation for twodifferent values of the parameter . In those examples, the values = 1 and=1000 are hard-coded in the ODE functions. If you are solving an ODE forseveral different parameter values, it might be more convenient to includet

34、he parameter in the ODE function and assign a value to the parameter eachtime you run the ODE solver. This section explains how to do this for thevan der Pol equation.前段表示了对两个不同的参数值的vdp方程。在这些例子中,和是ODE函数中的硬编码。如果你要对若干不同的参数值求解vdp方程,在方程中引入参数,每次运行进行参数赋值将会更方便。本段说明如何求解参数型vdp.One way to provide parameter va

35、lues to the ODE function is to write aMATLAB file that Accepts the parameters as inputs. Contains ODE function as a nested function, internally using the inputparameters. Calls the ODE solver.给ODE函数提供参数的方式是建立这样的MATLAB函数,它包含输入参数,包含作为嵌套函数的ODE函数,然后调用求解器。The following code illustrates this:如下代码实现该功能func

36、tion t,y = solve_vdp(mu)tspan = 0 max(20, 3*mu);y0 = 2; 0;% Call the ODE solver ode15s.t,y = ode15s(vdp,tspan,y0);% Define the ODE function as nested function,% using the parameter mu.function dydt = vdp(t,y)dydt = y(2); mu*(1-y(1)2)*y(2)-y(1);endendBecause the ODE function vdp is a nested function,

37、 the value of the parametermu is available to it.To run the MATLAB file for mu = 1, enter t,y = solve_vdp(1);To run the code for = 1000, entert,y = solve_vdp(1000);See the vdpode code for a complete example based on these functions.van der Pol Equation (Evaluating the Solution)对解进行赋值The numerical me

38、thods implemented in the ODE solvers produce acontinuous solution over the interval of integration a,b. You can evaluatethe approximate solution, S(x), at any point in a,b using the function devaland the structure sol returned by the solver. For example, if you solve theproblem described in “van der

39、 Pol Equation (Nonstiff)” on page 10-11 bycalling ode45 with a single output argument sol,ODE求解器实施的数值方法产生在积分区间上的连续解,你可用函数deval和solver返回的sol结构对a,b上的任何点对近似解进行赋值。例如,如果你调用ode45求解10-11的vdp问题。sol = ode45(vdp1,0 20,2; 0);ode45 returns the solution as a structure. You can then evaluate theapproximate soluti

40、on at points in the vector xint = 1:5 as follows:ode45作为一个结构返回解,你能够对向量xint=1:5的点赋值近似值,其方法如下:xint = 1:5;Sxint = deval(sol,xint)The deval function is vectorized. For a vector xint, the ith column of Sxintapproximates the solution y(xint(i).deval 函数是向量化的,对于向量xint,Sxint的第列近似解Euler Equations (Nonstiff) 非

41、刚性Euler方程rigidode illustrates the solution of a standard test problem proposed byKrogh for solvers intended for nonstiff problems 8.The ODEs are the Euler equations of a rigid body without external forces.Euler方程表示无外力的刚体运动,其方程为对应的程序如下:function rigidode%RIGIDODE Euler equations: rigid body without ex

42、ternal forcestspan = 0 12;y0 = 0; 1; 1;% Solve the problem using ode45ode45(f,tspan,y0);function dydt = f(t,y)dydt = y(2)*y(3)-y(1)*y(3)-0.51*y(1)*y(2) ;Fully Implicit ODE(全隐式ODE)The following example shows how to use the function ode15i to solve theimplicit ODE problem defined by Weissingers equati

43、on下例说明如何调用ode15i求解隐ODE问题。初值为,精确解为。t0 = 1;y0 = sqrt(3/2);yp0 = 0;y0,yp0 = decic(weissinger,t0,y0,1,yp0,0);t,y = ode15i(weissinger,1 10,y0,yp0);ytrue = sqrt(t.2 + 0.5);plot(t,y,t,ytrue,o);Finite Element Discretization(有限元离散)fem1ode illustrates the solution of ODEs that result from a finite elementdisc

44、retization of a partial differential equation. The value of N in the callfem1ode(N) controls the discretization, and the resulting system consistsof N equations. By default, N is 19.femlode 说明求解由偏微分方程有限元离散导致的ode.femlode(N)中的N控制离散化,导致的系统由N个方程构成,确省为19。This example involves a mass matrix. The system of

45、 ODEs comes from amethod of lines solution of the partial differential equation该例涉及质量矩阵。ODE方程组起源于如下偏微分方程的线方法:取定整数,取,在点偏微分方程的解表示为Here k(x) is a piecewise linear function that is 1 atxk and 0 at all the otherxj.A Galerkin discretization leads to the system of ODEs其中为分段线性函数,在点取1,其它点为0,Galerkin离散导致ODE方程

46、组。三角矩阵和可表示为The initial values c(0) are taken from the initial condition for the partialdifferential equation. The problem is solved on the time interval 0,.初值取偏微分方程的初值条件,在时间区间上求解问题。In the fem1ode example, the properties(在femlode 例子中,属性 options = odeset(Mass,mass,MStateDep,none,Jacobian,J)表明问题具有的形式,i

47、ndicate that the problem is of the form M(t)y = Jy. The nested functionmass(t) evaluates the time-dependent mass matrix M(t) and J is the constantJacobian.To run this example, click the example name, or type fem1ode at the commandline. From the command line, you can specify a value of N as an argumentto fem1ode. The default is N = 19.

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 小学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁