《科学计算科学计算 (38).pdf》由会员分享,可在线阅读,更多相关《科学计算科学计算 (38).pdf(18页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、6.46.4 Solve Nonlinear Equation and Find Solve Nonlinear Equation and Find Extreme Value of FunctionExtreme Value of FunctionThe solution of nonlinear equationsThe calculation of function extreme values1 1 The The S Solution of olution of NNonlinear onlinear E Equationsquations(1)Solving univariate
2、nonlinear equationsThe syntax of the function is:x=fzero(filename,x0)Where filename is the function expression at the left of the quation,and x0 is the initial value.Example 1:Find the root of at x0=-5 and x0=1 as the initial value of iteration.f(x)=x-x+15 f=(x)x-1./x+5;x1=fzero(f,-5)x1=-5.1926 x2=f
3、zero(f,1)x2=0.1926 x3=fzero(f,0.1)x3=3.7372e-16Lets take the initial value around x=0 and see what the result is.Obviously,x3 is not the solution of the equation.So it is very important to choose the initial value when solving the equation by using the fzero function,which involves the method of its
4、 internal implementation.Therefore,when usingMATLAB to solve a problem,we need to analyze the problem from different aspects instead of mechanically applying commands or programs.f=(x)x.2-1;x=;x0=-0.25:0.001:0.25;for x00=x0 x=x,fzero(f,x00);endplot(x0,x,-o)xlabel(Initial value);ylabel(The root of th
5、e equation);axis(-0.25,0.25,-1,1)As the result shows,around the zero point,the range of initial values corresponding to the same root is not continuous,and the root obtained is not the root closer to the initial value.So,the fzero function performs a numerical search process,and the result depends o
6、n the function properties and the specified initial values.The root of f(x)=x2-1=0.(2)Solution of nonlinear equationsThe optimization toolbox of MATLAB provides the solution function fsolve of systems of nonlinear equations.Its syntax is as follows:x=fsolve(filename,x0,option)x is the approximate va
7、lue returned.Filename is the function expression at the left of the equation whose root is to be found.x0 is the initial value.Option is used to set the optimum parameters of the optimization toolbox and can be performed by calling the optimset function.For example,when the Display parameter is set
8、to“off”,no intermediate results are displayed.f=(x)x-1./x+5;x1=fsolve(f,-5,optimset(Display,off)x1=-5.1926 x2=fsolve(f,1,optimset(Display,off)x2=0.1926 x3=fsolve(f,0.1,optimset(Display,off)x3=0.1926when the initial value is 0.1,t he correct result cannot be obtained by using the fzero function.But w
9、e can obtain the correct result by using the fsolve function here,because of different implementation methods and applications of different functions.In short,I t is necessary to choose and analyze from various aspects.Use the fsolve function to solve the equation of example 1.Example 2:Find the sol
10、utions of the following equations around(1,1,1)and verify the results.xxyz exyzxyz2sin000+=+=f=(x)sin(x(1)+x(2)+x(3)2*exp(x(1),x(1)+x(2)+x(3),x(1)*x(2)*x(3);f(1,1,1)ans=4.5598 3.0000 1.0000 x=fsolve(f,1,1,1,optimset(Display,off)x=0.0224 -0.0224 -0.0000 f(x)ans=1.0e-06*-0.5931 -0.0000 0.00062 2The Th
11、e C Calculation of alculation of F Function unction E Extreme xtreme V ValuesaluesExtreme values of functions include maximums and minimums which are also called maximum and minimum values.MATLAB only calculates the minimum value.The maximum value of f(x)can be obtained by finding the minimum value
12、of-f(x).(1)The unconstrained optimization problemsThe unconstrained optimization problem is described as follows:min()among them,x=x1,x2,xnT.The functions to find the minimum value are:xmin,fmin=fminbnd(filename,x1,x2,option)xmin,fmin=fminsearch(filename,x0,option)xmin,fmin=fminunc(filename,x0,optio
13、n)Among them,xmin represents the minimum point.fmin represents the minimum value.filename isthe defined objective function.The input arguments xl and x2 of the first function respectivelyrepresent the left and right bounds of the interval studied.The input argumrent x0 of the latter twofunctions is
14、a vector,representing the initial value of the extreme point.Option is an optimizationparameter that can be set by the optimset function.Example 3:Find the function of ,the minimum point on the interval(-10,-1)and(1,10).f xxx=+()15 f=(x)x-1./x+5;xmin,fmin=fminbnd(f,-10,-1)xmin=-9.9999fmin=-4.8999 xm
15、in,fmin=fminbnd(f,1,10)xmin=1.0001fmin=5.0001(2)The constrained optimization problemThe constrained optimization problem is generally described as follows:min.()0()among them,x=x1,x2,xnT.Finding a set of x to minimize the objective function f(x)and also meeting the constraint:G(x)0.S.t.is the abbrev
16、iation of ”subject to”,meaning that x should meet the following constraints.Constraints can be subdivided into linear inequality constraints,linear equality constraints,nonlinear inequality constraints,nonlinear equality constraints,and lower and upper bounds of x.The function to find the minimum va
17、lue under constraints is:xmin,fmin=fmincon(filename,x0,A,b,Aeq,beq,Lbnd,Ubnd,NonF,option)xmin,fmin,filename,x0 and option have the same meanings as those of the previous function for finding the minimum value.The remaining arguments are constraints including linear inequality constraints,linear equa
18、lity constraints,lower and upper bounds of x,and functions defining nonlinear constraints.If a constraint does not exist,I t can be represented by an empty matrix.Example 4:Solve a constrained optimization problem.f=(x)0.4*x(2)+x(1)2+x(2)2-x(1)*x(2)+1/30*x(1)3;x0=0.5;0.5;A=-1,-0.5;-0.5,-1;b=-0.4;-0.
19、5;lb=0;0;option=optimset(Display,off);xmin,fmin=fmincon(f,x0,A,b,lb,option)xmin=0.33960.3302fmin=0.2456f xxxxx xxxs txxxxxx212221213121212min()0.4130.0.50.40.50.50,0=+(3)Application of the minimum value problem.Example 5:A company has 5 factoriesA,B,C,D and E,which are located at 5 coordinate points
20、 on the x-y plane.(10,10),(30,50),(16.667,29),(0.555,29.888)and(22.2221,49.988).Suppose that the distance between two points represents the driving distance between factories in kilometers.The company plans to build a warehouse at a coordinate point on the plane and expects to make 10,18,20,14 and 2
21、5 deliveries to factories A,B,C,D and E each week on average.Ideally,to minimize the mileage of weekly delivery,where should the warehouse be located on the x-y plane?d(,)=10 102+102+18 302+502+20 16.6672+292+14 0.5552+29.8882+25 22.22212+49.9882If the coordinate point of the warehouse is(x y),the e
22、xpression of the total mileage is as follows:Therefore,this problem is to find the minimum value of d(x,y)without constraints.a=10,30,16.667,0.555,22.2221;b=10,50,29,29.888,49.988;c=10,18,20,14,25;f=(x)sum(c.*sqrt(x(1)-a).2+(x(2)-b).2);xmin,fmin=fminsearch(f,15,30)xmin=19.8143 41.1247fmin=1.3618e+03
23、Example 6:In Example 5,if the warehouse must be built on a curve y=x2 due to geographical restrictions,where should it be built?Nonlinear constrained function file funny.m:function c,h=funny(x)c=;h=x(2)-x(1)2;a=10,30,16.667,0.555,22.2221;b=10,50,29,29.888,49.988;c=10,18,20,14,25;f=(x)sum(c.*sqrt(x(1)-a).2+(x(2)-b).2);xmin,fmin=fmincon(f,15,30,funny)xmin=5.9363 35.2402fmin=1.6676e+03