《MATLAB程序设计教程》第7章 MATLAB解方程与函数极值
- 格式:ppt
- 大小:148.00 KB
- 文档页数:28
第七章MATLAB解方程与函数极值班级:工自03-1班姓名:陈录平学号:030544103 7-1(1)A=[2,3,5;3,7,4;1,-7,1];b=[10,3,5];c=inv(A);[L,U]=lu(A);x=c*b'y=A\b'z=U\(L\b')x =-1.8060-0.53733.0448y =-1.8060-0.53733.0448z =-1.8060-0.53733.0448(2)A=[6,5,-2,5;9,-1,4,-1;3,4,2,-2;3,-9,0,2];b=[-4,13,1,11];c=inv(A);[L,U]=lu(A);x=c*b'y=A\b'z=U\(L\b')x = 0.6667-1.00001.5000-0.0000y =0.6667-1.00001.5000-0.0000z = 0.6667-1.00001.5000-0.00007-2(1)A=[1/2,1/3,1/4;1/3,1/4,1/5;1/4,1/5,1/6];b=[0.95,0.67,0.52];x=A\b'x = 1.20000.60000.6000(2)A=[1/2,1/3,1/4;1/3,1/4,1/5;1/4,1/5,1/6];b=[0.95,0.67,0.53];x=A\b'x = 3.0000-6.60006.6000(3)A=[1/2,1/3,1/4;1/3,1/4,1/5;1/4,1/5,1/6];c=cond(A)c =1.3533e+003矩阵条件数很大,因此个别元素的微小扰动会引起解向量的很大变化。
7-3(1)建立函数文件function fx=fun(x)fx=x^41+x^3+1;调用函数求根z=fzero('funx',-1)z =-0.9525(2)建立函数文件function fx=fun(x)fx=x-sin(x)./x;调用函数求根z=fzero('funx',0.5)z =0.8767(3)建立函数文件function fx=fun(x)fx=3*x+sin(x)-exp(x);调用函数求根z=fzero('funx',1.5)z =1.8900(4)建立函数文件function fx=fun(x)fx=x-1./x+5;调用函数求根z=fzero('funx',1)z =0.19267-4(1)建立函数文件function q=myfun(p)x=p(1);y=p(2);q(1)=x^2+y^2-9;q(2)=x+y-1;调用函数x=fsolve('myfun',([3,0]),optimset('display','off'))x =2.5616 -1.5616(2)建立函数文件function q=myfun(p)x=p(1);y=p(2);z=p(3);q(1)=sin(x)+y^2+log(z)-7;q(2)=3*x+2*y-z^2+1;q(3)=x+y+z-5;调用函数x=fsolve('myfun',([1,1,1]),optimset('display','off')) x =0.0715 2.4541 2.47447-5建立函数function xdot=lorenz(t,x)xdot=[-0.5,1;-1,-0.5]*x;调用函数x0=[0;1]';[t,x]=ode23('lorenz',[0,4*pi],x0);plot(x(:,1),x(:,2));axis([10,10,10,10]);t = 00.00010.00050.00250.01250.06250.27400.54190.81441.08541.35031.61421.88112.15082.42382.69382.95813.2224 3.48973.75994.03344.30234.56614.83095.09855.36915.64315.91116.17436.43956.70766.97857.25317.52037.78298.04858.31708.58848.86249.12899.39169.65769.926410.198210.470910.736710.999911.266311.535511.807812.079312.344512.5664x = 0 1.00000.0001 1.00000.0005 0.99980.0025 0.99880.0124 0.99370.0605 0.9674 0.2358 0.8395 0.3930 0.6536 0.4836 0.4572 0.5137 0.2719 0.4966 0.1122 0.4458 -0.01850.3721 -0.1183 0.2859 -0.1862 0.1965 -0.2236 0.1135 -0.2341 0.0425 -0.2239 -0.0153 -0.1991 -0.0588 -0.1645 -0.0879 -0.1248 -0.1032 -0.0842 -0.1065 -0.0470 -0.1008 -0.0155 -0.0888 0.0100 -0.0726 0.0289 -0.0544 0.0413 -0.0359 0.0475 -0.0193 0.0484 -0.0053 0.0453 0.0059 0.0396 0.0141 0.0320 0.0193 0.0236 0.0218 0.01530.0219 0.0079 0.0204 0.0017 0.0176 -0.0033 0.0141 -0.0068 0.0102 -0.0090 0.0065 -0.0100 0.0032 -0.0099 0.0004 -0.0091 -0.0017 -0.0078 -0.0033 -0.0062 -0.0042 -0.0044 -0.0046 -0.0027 -0.0045 -0.0013 -0.0041 -0.0000 -0.0035 0.0009 -0.0027 0.0016 -0.0019 0.0020 -0.0012 0.0021 -0.0005 0.0020 -0.0000 0.0019-0.2-0.100.10.20.30.40.50.6-0.4-0.20.20.40.60.817-6建立函数文件 function yp=funt(t,y) yp=-(1.2+sin(10*t)*y);调用函数t0=0;tf=5;y0=1;[t,y]=ode23('funt',[0,5],y0)t = 00.06670.13960.20200.27000.34780.42570.49090.55440.63300.68580.73860.78730.84770.90260.95631.01671.10091.16261.22431.28791.38011.45381.51701.58541.67331.73881.80431.8666 1.93762.00652.07552.13862.20832.30232.36752.43272.49542.56812.63232.69652.75972.82922.92252.98823.05393.11663.18773.25763.32753.39083.46153.56003.62493.68983.75283.82713.89123.95544.01884.08984.19014.25484.31954.38264.45784.52194.58594.64944.72124.82594.89004.95425.0000y = 1.00000.89990.76170.64420.53470.43900.36550.30860.24770.15910.09380.0282 -0.0301 -0.0990 -0.1606 -0.2239 -0.3032 -0.4321 -0.5339 -0.6286 -0.7049 -0.7690-0.8009-0.8385-0.9096-1.0621-1.2121-1.3657-1.4818-1.5485-1.5508-1.5294-1.5298-1.5868-1.7822-1.9781-2.1766-2.3171-2.3730-2.3363-2.2683-2.2273-2.2617-2.4739-2.7132-2.9630-3.1384-3.1999-3.1264-3.0009-2.9254-2.9514-3.2241-3.5123-3.8003 -3.9883 -4.0188 -3.9011 -3.7374 -3.6246-3.6363-3.9520-4.2870-4.6180-4.8267-4.8387-4.6777-4.4668-4.3206-4.3262-4.7137-5.0980-5.4626-5.6371(2)建立函数文件function yp=funt(t,y)yp=cos(t)-(1/(1+t.*t)).*y; 调用函数t0=0;tf=5;y0=1;[t,y]=ode23('funt',[t0,tf],y0)t = 00.50000.80161.10331.40771.75372.23012.52152.81292.97273.13263.30013.50913.75494.03754.36374.75845.0000y = 1.00001.01461.03631.04501.01620.91570.64510.41220.1437-0.0122-0.1699-0.3330-0.5273-0.7327-0.9233-1.0649-1.1040-1.0534(3)建立函数文件function xdot=vdpol(t,x)ydot(1)=(-3*y(2)-2.*T.*y(2))./(1+t^2) ydot(2)=y(1);ydot=ydot';调用函数to=0;tf=5;y0=[0;1];[t,y]=ode45('vdpol',[0,5],y0)t = 00.00000.00000.00010.00010.00020.00020.00030.00040.0008 0.00120.00170.00210.00420.00630.00840.01040.02090.03140.04180.05230.10460.15700.20930.26160.36170.46180.56200.66210.77330.88460.99591.1072 1.2148 1.3223 1.4299 1.5375 1.6625 1.78751.91252.0375 2.1625 2.28752.41252.53752.66252.78752.91253.03753.16253.28753.41253.53753.66253.78753.91254.03754.16254.28754.41254.53754.65314.76874.88445.0000y = 0 1.0000 -0.0001 1.0000-0.0001 1.0000-0.0002 1.0000-0.0002 1.0000-0.0005 1.0000-0.0007 1.0000-0.0010 1.0000-0.0012 1.0000-0.0025 1.0000-0.0037 1.0000-0.0050 1.0000-0.0062 1.0000-0.0125 1.0000-0.0188 0.9999-0.0251 0.9999-0.0315 0.9998-0.0632 0.9993-0.0951 0.9985-0.1271 0.9974-0.1593 0.9959-0.3219 0.9833 -0.4852 0.9622-0.6467 0.9325-0.8040 0.8945-1.0848 0.7997-1.3274 0.6787-1.5216 0.5357-1.6622 0.3758-1.7541 0.1850-1.7812 -0.0123-1.7495 -0.2092-1.6673 -0.3997-1.5487 -0.5730-1.3996 -0.7319-1.2278 -0.8734-1.0402 -0.9954-0.8104 -1.1112-0.5758 -1.1979-0.3430 -1.2552-0.1175 -1.28390.0966 -1.28510.2962 -1.26040.4789 -1.21170.6433 -1.14140.7884 -1.05170.9138 -0.94511.0195 -0.82411.1057 -0.69101.1730 -0.54841.2222 -0.39851.2541 -0.24361.2699 -0.08571.2705 0.07331.2571 0.23141.2310 0.38701.1933 0.53861.1452 0.68491.0879 0.82461.0225 0.95650.9501 1.07990.8779 1.18560.8014 1.28270.7215 1.37080.6388 1.4495(4)建立函数function ydot=vdpol(t,y)ydot(1)=cos(t)+5*(cos(2*t)./(t+1)^2).*y(1)-y(2)-(1/(3+sin(t))).*y(3); ydot(2)=y(1);ydot(3)=y(2);ydot=ydot';ydot'=ydot'';调用函数t0=0;tf=5;y0=[1;0;2];[t,y]=ode45('vdpol',[0,5],y0)t = 00.00010.00010.00020.00020.00050.00070.00100.00120.00250.00370.00500.00620.01250.01880.02510.03130.06270.09410.12550.15690.2195 0.28200.34450.40700.53200.65700.78200.90701.02111.13521.24931.36341.48841.61341.73841.86341.98842.11342.23842.36342.48842.61342.73842.86342.98843.11343.23843.36343.48843.61343.73843.86343.98844.11344.23844.36344.48844.61344.73844.86344.89764.93174.96595.0000y = 1.0000 0 2.00001.0003 0.00012.00001.0005 0.00012.00001.0008 0.00022.00001.0011 0.00022.00001.0024 0.00052.00001.0038 0.00072.00001.0051 0.00102.00001.0064 0.00122.00001.0132 0.00252.00001.0199 0.00382.00001.0267 0.00502.00001.0335 0.00632.00001.0680 0.01292.00011.1030 0.01972.00021.1386 0.02682.00031.1749 0.03402.00051.3647 0.07392.00221.5677 0.11992.00521.7822 0.17242.00982.0058 0.2319 2.01612.4683 0.3717 2.03482.9334 0.5405 2.06323.3764 0.7379 2.10303.7737 0.9617 2.15604.3485 1.4740 2.3068 4.5636 2.0346 2.5252 4.4084 2.5980 2.8155 3.9436 3.1228 3.1744 3.3407 3.5396 3.5553 2.6429 3.8818 3.9796 1.9092 4.1418 4.4381 1.1789 4.3176 4.9215 0.4072 4.41635.4684 -0.3277 4.42106.0218 -1.0291 4.3359 6.5699 -1.7076 4.16477.1020 -2.3780 3.9094 7.6075 -3.0529 3.57008.0759 -3.7425 3.1454 8.4965 -4.4523 2.6335 8.8586 -5.1832 2.03169.1511-5.9286 1.3372 9.3626 -6.6747 0.5493 9.4815 -7.3993 -0.3307 9.4961 -8.0730 -1.2984 9.3953 -8.6589 -2.3452 9.1683 -9.1181 -3.4578 8.8061 -9.4112 -4.6179 8.3018 -9.5015 -5.8024 7.6508 -9.3604 -6.9837 6.8516 -8.9696 -8.1318 5.9062 -8.3235 -9.2154 4.8210 -7.4281 -10.2026 3.6063 -6.3005 -11.0630 2.2758 -4.9650 -11.7689 0.8471 -3.4512 -12.2965 -0.6591 -1.7921 -12.6257 -2.2190 -0.0200 -12.7401 -3.8067 1.8334 -12.6274 -5.39453.7383 -12.2793 -6.95364.2637 -12.1428 -7.37054.7902 -11.9882 -7.78255.3174 -11.8157 -8.1888 5.8445 -11.6252 -8.58907-7(1)建立函数文件function fx=mymin(x)fx=-(1+x.*x)./(1+x.*x.*x.*x);调用函数x=fmin('mymin',0,2)x =0.6436(2)建立函数文件function fx=mymin(x)fx=-(sin(x)+cos(x.*x));调用函数x=fmin('mymin',0,pi)x =0.7310。
第七讲 Matlab 优化(求极值)理论介绍:算法介绍、软件求解.一.线性规划问题1.线性规划问题是在一组线性约束条件的限制下,求一线性目标函数最大或最小值的问题,Matlab 中规定线性规划的标准形式为min s.t.T xc x Ax b Aeq x beqlb x ub ≤⎧⎪⋅=⎨⎪≤≤⎩其中c 和x 为n 维列向量,A 、Aeq 为适当维数的矩阵,b 、beq 为适当维数的列向量。
注意:线性规划问题化为Matlab 规定中的标准形式。
求解线性规划问题的Matlab 函数形式为linprog(c,A,b),它返回向量x 的值,它的具体调用形式为:[x,fval]=linprog(c,A,b,Aeq,beq,LB,UB,x0,OPTIONS)这里fval 返回目标函数的值,LB 、UB 分别是变量x 的下界和上界,x0是x 的初始值,OPTIONS 是控制参数。
例1 求解线性规划问题123123123123123max 23572510s.t.312,,0z x x x x x x x x x x x x x x x =+-++=⎧⎪-+≥⎪⎨++≤⎪⎪≥⎩ 程序:c=[2;3;5];>> A=[-2,5,-1;1,3,1];b=[-10;12];>> Aeq=[1,1,1];beq=[7];>> LB=[0;0;0];(zeros(3,1))>> [x,fval]=linprog(c,A,b,Aeq,beq,LB,[])练习与思考:求解线性规划问题12312312123min 23+428s.t.3+26,,0z x x x x x x x x x x x =+++≥⎧⎪≥⎨⎪≥⎩ 注意:若没有不等式:b AX ≤存在,则令A=[ ],b=[ ]. 若没有等式约束, 则令Aeq=[ ], beq=[ ].2.可以转化为线性规划的问题规划问题12min||+||++||s.t.,n x x x Ax b ≤ 其中1=[],T n x x x ,A b 为相应维数的矩阵和向量。
MATLAB求函数零点与极值
1. roots函数
针对多项式求零点(详见MATLAB多项式及多项式拟合)
2. fzero函数
返回⼀元函数在某个区间内的的零点.
x0 = fzero(@(x)x.^2-3*x-4,[1,5]);
只能求区间⾥⾯的⼀个零点,并且要求在给定区间端点函数值异号,所以使⽤之前应该先作图,得出单个零点分布的区间,然后使⽤该函数求零点.若有多个零点,则需多次使⽤该函数.
如需求上例中的全部零点,先作图
fplot(@(x)x.^2-3*x-4,[-10,10]);
得知两个零点的分布区间,然后两次使⽤fzero函数求对应区间的零点.
x1 = fzero(@(x)x.^2-3*x-4,[-2,0]);
x2 = fzero(@(x)x.^2-3*x-4,[2,6]);
3. solve函数
求⼀元函数(⽅程)的零点.
x0 = solve('x^2-3*x-4=0','x');
注意⽅程需包含’=0’部分,另外,不建议直接将⽅程写在函数solve的参数部分,可以⽤符号运算的⽅法.
4. fminbnd函数
求⼀元函数在某个区间内的最⼩值和对应的最⼩值点.
[x0,fmin]=fminbnd(@(x)x+1/(x+1),-0.5,2);
求极值与极值点之前须估计极值点的区间,保证在该区间没有使得函数值趋于⽆穷的点.。
matlab求解非线性方程组及极值默认分类2010-05-18 15:46:13 阅读1012 评论2 字号:大中小订阅一、概述:求函数零点和极值点:Matlab中三种表示函数的方法: 1. 定义一个m函数文件, 2.使用函数句柄; 3.定义inline函数, 其中第一个要掌握简单函数编写, 二, 三中掌握一个。
函数的'常规'使用有了函数了, 我们怎么用呢, 一种是直接利用函数来计算, 例如: sin(pi), 还有我们提到的mysqr(3)...另一种是函数画图, 例如Plottools中提到的ezplot, ezsurf... 但是这也太小儿科了, 有没有想过定义函数后, 利用它来: 求解零点(即解f(x)=0方程), 最优化(求最值/极值点), 求定积分, 常微分方程求解等. 当然这里由于篇幅有限(空间快满了)以及这个只是'基础教程'的缘故, 只提及一些皮毛知识, 掌握这些后, 如果需要你可以进一步学习.解f(x)=0已知函数求解函数值=0所表示的方程, Matlab中有两个函数可以做到, fzero和fsolve前者只能解一元方程, 后者可以解多元方程组, 不过基本使用形式上差不多:解=fzero(函数, 初值, options)解=fsolve(函数, 初值, options)关于解: fzero给出的是x单值的解, fsolve给出的是解x可能处于的区间, 当然, 这个区间很窄.关于'函数', 还记得前面提到的三种表示方法吧, 在这里都可以用, 记住就是: 如果直接使用函数名, 要用单引号将它括起来, 而函数句柄, inline函数可以直接使用.关于'初值': 电脑比较笨, 它寻找解的办法是尝试不同地x值, 摸索解在哪里, 所以我们一开始就要给它指明从哪里开始下手, 初值这里, 可以只给它一个值, 让它在这个值附近找解, 也可以给它一个区间(区间用[下限,上限]这种方式表示), 它会在这个区间内找解.fzero的一些局限, 如果你给定的初值是区间, 而恰好函数在区间端点处同号, fzero会出错, 而如果你只给一个初值, fezro又有可能'走错方向', 例如给初值2让它解mysqr这个函数方程就出错了, FT!寻找函数极值/最值Matlab中也有两个函数可以做到, 是: fminbnd: 寻找一元函数极小值; fminsearch: 寻找多元函数极小值(当然一元也行). 别问我怎么没有找极大值的Matlab函数, 你把原函数取负数, 寻找它的极小值不就行了. 相关语法:x=fminbnd(函数, 区间起始值, 区间终止值)x=fminsearch(函数, 自变量初值)相关说明: fminbnd中指定要查找极小值的自变量区间, 好像不指定也行, 不过那样的话, 如果函数有多个极小值就可能比较难以预料结果了.fminsearch中要给定一个初值, 这个初值可以是自变量向量(将自变量依次排在一起组成向量)的初值, 也可以是表示向量初值区间的一个矩阵.函数: 那三种形式都适用, 但是记住, 直接使用函数名称需要加单引号!cite from:/qq529312840/blog/item/3687e4c7e7e2d6d9d0006049.html二、实例+讲解(1)非线性方程数值求解:1 单变量非线性方程求解在MATLAB中提供了一个fzero函数,可以用来求单变量非线性方程的根。
Mat lab求解方程和函数极值The fifth chapter, 22010-03-31 22:50Two. Matlab for solving equations and function extremumI. solving linear equations1. direct solutionThe direct solution to the use of the left division operatorFor linear equations Ax=b, we can use the left division operator to solve: X=A\bFor example, the following linear equations are solved by direct solution. The commands are as follows:A=[2, 1, -5, 1; 1; -5,0,7; 0,2, 1; -1; 1,6; -1; —4];B=[13, -9,6,0],;X=A\bSolving linear equations by the decomposition of matrixMatrix decomposition is the product of decomposing a matrix into several matrices according to certain principles・ Common matrix decomposition includes LU decomposition, QR decomposition, Cholesky decomposition, and Schur decomposition, Hessenberg decomposition, singular decomposition,and so on.(1)LU decompositionThe LU decomposition of a matrix means that a matrix is expressed as a product of a commutative lower triangular matrix and an upper triangular matrix・Linear algebra has proved that LU decomposition is always possible as long as the square A is nonsingular・\IATLAB provides the Lu function for LU decomposition of the matrix, the call format for:[L, U]二lu (X): produces an upper triangular array U and a transform form of lower triangular array L (row exchange) to satisfy X二LU. Note that the matrix X here must be square・[L, U, P]二lu (X): produces an upper triangular array U and a lower triangular matrix L, and a permutation matrix P, which satisfies PX=LU ・ Of course, the matrix X must also be square・After the LU decomposition, the solution of the linear equations Ax二b (L\b) or x二U\ (L\Pb), which can greatly improve the speed of operation x二U\・For example, LU is used to solve the linear equations in example 7-1. The commands are as follows:A二[2,1, -5, 1; 1; -5,0,7; 0,2, 1; T; 1,6; -1; -4];B二[13, -9, 6,0]';[L, U]二lu (A);X二U\ (L\b)Or using LU decomposition of the second formats, as follows:[L, U, P]二lu (A);X二U\ (L\P*b)(2)QR decompositionQR decomposition of the matrix X, is the decomposition of X into an orthogonal matrix Q and an upper triangular matrix R product form. QR decomposition can only be carried out by the opponent,s array. MATLAB function QR can be used for QR decomposition of the matrix, the call format for:[Q, R]=qr (X): produces an orthogonal matrix Q and an upper triangular matrix R so that it satisfies X二QR・[Q, R, E]=qr (X): produce an orthogonal matrix Q, an upper triangular matrix, R, and a permutation matrix E, so that it satisfies XE二QR.After the QR decomposition, the solution of the linear equation Ax二b is x二R\ (Q\b) or x二E (R\ (Q\b))・For example, QR is used to solve the linear equations in example 7-1.The commands are as follows:A二[2,1, -5, 1; 1; -5,0,7; 0,2, 1; T; 1,6; -1; -4];B二[13, -9, 6,0]';[Q, R]二qr (A);X二R\ (Q\b)Or using QR decomposition of the second formats, as follows:[Q, R, E] =qr (A):X二E* (R\ (Q\b))(3)Cholesky decompositionIf the matrix X is symmetric and positive definite, the Cholesky decomposition decomposes the matrix X into the product of a lower triangular matrix and upper triangular matrix・ Set the triangle matrix to R,Then the lower triangular matrix is transposed, that is, X二R' R・ The MATLAB function Choi (X) is used for Cholesky decompositionof the matrix X, and its call format is:R二chol (X): produces an upper triangular array R that makes R'R二X ・If the X is an asymmetric positive definite, an error message is output ・[R, p]二chol (X): this command format will not output error messages・ When X is symmetric positive definite, then p二0 and R are the same as those obtained in the above format: otherwise, P is a positive integer ・If X is a full rank matrix, then R is an upper triangular matrix of order q=p-l and satisfies R'R二X (l:q, l:q)・After the Cholesky decomposition, the linear equation group Ax二b becomes R 'Rx二b, so x二R\ (R, \b ')・Example 4 decomposes the system of linear equations in example 7-1 by Cholesky decomposition.The commands are as follows:A二[2,1, -5, 1; 1; -5, 0,7; 0,2, 1; T; 1,6; -1; -4];B 二[13, —9,6,0]';R二chol (A)Error using Chol =>???Matrix, must, be, positive, definiteWhen the command is executed, an error message appears indicating that the A is a non positive definite matrix・2. iteration methodIterative method is very suitable for solving large coefficient matrix equations・ In numerical analysis, the iterative methods mainly include Jacobi iterative method, Gauss-Serdel iterative method, over relaxationiterative method and two step iterative method・Jacobi iteration methodFor linear equations Ax二b, if A is a nonsingular square, that is, (i=l, 2,・・・(n), A can be decomposed into A 二D-L-U, where D is a diagonal matrix, and its elements are diagonal elements of A, L and U are lower triangular and upper triangular matrices of A, so Ax二b becomes・・:The corresponding iteration formula is:This is the Jacobi iteration formula・If the sequence converges to x, then x must be the solution of equation Ax二b・The MATLAB function file of the Jacobi iteration method Jacobi.m is as follows:Function, [y, n]二jacobi (A, B, xO, EPS)If nargin=3Eps=1.Oe-6;Elseif nargin〈3ErrorReturnEndD=diag (diag (A)) -% A diagonal matrixL=-tril (A, -1); the lower triangular array of% AU二-triu (A, 1); the upper triangular array of% AB二D\ (L+U);F二D\b;Y二B*xO+f:N二1;% iterationsWhile norm (y-xO) 〉=epsXO二y;Y二B*xO+f:N二n+1;EndExample 5 uses the Jacobi iterative method to solve the following linear equations・The initial value of iteration is 0, and the iteration accuracy is 10-6・Call the function file "Jacobi.nT in the command. The command is as follows:A二[10, -1,0; -1, 10; -2; 0; -2, 10];B二[9, 7,6]';[x, n]二jacobi (A, B, [0,0,0]', 1. Oe-6)Gauss-Serdel iteration methodIn the Jacobi iteration process, the calculations have been obtained and need not be reused, i.e., the original iterative formula Dx (k+1)二(L+U) x (k) +b can be improved to Dx (k+1) 二Lx (k+1) +Ux (k) +b and thus obtained:X (k+1)二(D-L) -lUx (k) + (D-L) TbThis formula is the Gauss-Serdel iterative formula・ Compared with the Jacobi iteration,The Gauss-Serdel iteration replaces the old components with new ones, with higher accuracy・The MATLAB function f订e of the Gauss-Serdel iteration method gauseidel.m is as follows:Function, [y, n]=gauseidel (A, B, xO, EPS)If nargin=3Eps二1. 0e-6;Elseif nargin<3ErrorReturnEndD=diag (diag (A)) -% A diagonal matrixL=-tril (A, -1); the lower triangular array of% AU=-triu (A, 1); the upper triangular array of% AG二(D-L) \U;F二(D-L) \b;Y二G*xO+f;N=l;% iterationsWhile norm (y-xO) >=epsXO二y;Y二G*xO+f:N二n+1;EndExample 6 uses the Gauss-Serdel iterative method to solve the followinglinear equations・The initial value of iteration is 0, and the iteration accuracy is 10-6・Call the function file z,gauseidel.m" in the command. The command is as follows:A二[10, -1,0; -1, 10; -2; 0; -2, 10];B二[9, 7,6]';[x, n]二gauseidel (A, B, [0,0,0]', 1.0e~6)Example 7, Jacobi iteration and Gauss-Serdel iteration method are used to solve the following linear equations, to see whether convergence・The commands are as follows:A二[1,2, -2; 1, 1, 1; 2, 2,11;B二[9; 7; 6];[x, n]=jacobi (a, B, [0; 0; 0])[x, n]=gauseidel (a, B, [0; 0; 0])Two. Numerical solution of nonlinear equations1 solving the single variable nonlinear equationIn MATLAB, a fzero function is provided that can be used to find the roots of a single variable nonlinear equation. The call format of this functionis: z二fzero ('fname', xO, tol, trace)Where fname is the root to be a function of the file name, xO as the search starting point・ A function may have more than one root, but the fzero function only gives the root nearest to the x0・ The relative accuracy of the TOL control results, by default, take tol二eps, trace, specify whether the iteration information is displayed in the operation, and when 1 is displayed, 0 is not displayed, and the trace=0 is taken by default・Example 8 ask for roots in the neighborhood.Steps are as follows:(1) establishment of function file funx・m.Function fx二funx (x)Fx二xTO. x+2;(2) fzero function call root.Z=fzero (' funx', 0. 5)二ZZero point three seven five eight2. solving nonlinear equationsFor nonlinear equations F (X) =0, the numerical solution is obtained by using the fsolve function. The call format of the fsolve function is:X二fsolve (' fun,, X0, option)Where X is the return of the solution, fun is used to define the demand for solutions of nonlinear equations function of the file name, X0 is the initial rooting process, option optimization toolbox options・ The Optimization Toolbox provides more than 20 options that users can display using the optimset command・If you want to change one of the options, you can call the optimset () function to complete it. For example, Display display option decision function calls intermediate results, in eluding 〃off〃does not show, 〃ITER〃said each show,'final5 shows only the final results・ Optimset ('Display', 'off') will set the Display option to 'off'.Example 9 find the numerical solution of the following nonlinear equations in (0.5,0.5) neighborhood・(1)establishment of function file myfun. m.Function q=myfun (P)X二p (1);Y二p (2);Q (1) =x-0・6*sin (x) -0.3*cos (Y);Q (2)二y-0・6*cos (x) +0. 3*sin (Y);(2)under the given initial value x0=0・5 and y0=0・5, call the fsolve function to find the root of the equation.X二fsolve C myfun,, [0.5,0. 5]', optimset ('Display'off'))二xZero point six three five fourZero point three seven three fourThe solution is returned to the original equation, and it can be checked whether the result is correct or not:Q二myfun (x)1.0e-009 *0. 2375, 0. 2957We can see the result of higher precision.Three・ Numerical solution of initial value problems for ordinary differential equations1. introduction of Runge Kutta method2., the realization of Runge Kutta methodBased on Runge Kutta method, MATLAB provides the function of finding numerical solutions of ordinary differential equations:[t, y]二ode23 C fname,, tspan, Y0)[t, y]二ode45 C fname,, tspan, Y0)Where fname is the function file name that defines f (T, y), and the function file must return a column vector・ The tspan form is [tO, tf], which represents the solution interva. 1. Y0 is the initial state column vector ・ The time vectors and corresponding state vectors are given by T and Y respectively.Example 10 has an initial value problem,Try to find the numerical solution and compare it with the exact solution (the exact solution is y (T)二).(1)establishment of function file funt. m.Function yp二funt (T, y)Yp= (y"2-t-2) /4/ (t+1);(2)solving differential equations・TOO; tf二10;¥0=2;[t, y]=ode23 ('funt', [tO, tf], Y0) ;% for numerical solutionYl=sqrt (t+1) +1;% refinement; exact solutionPlot (T, y, 'B・',t, Yl, ?)% are compared by graphThe numerical solution is represented by a blue dot, and the exact solution is expressed in red solid lines・ As shown. It can be seen that the two results approximate・ CloseFour. Function extremumMATLAB provides functions based on the simplex algorithm for solving extremal functions Fmin and fmins, which are used respectively for minimum values of univariate functions and multivariate functions:X二fmin (' fname,, xl, x2)X二fmins (' fname' , xO)The call formats of the two functions are similar・ Among them, the Fmin function is used to find the minimum point of a single variable function. Fname is the object function name that is minimized, and XI and X2 limit the range of arguments・ The fmins function is used to find the minimum point of a multivariable function, and xO is the initial value vector of the solution.MATLAB provides no special function to find the maximum function, but as long as the attention to -f (x) in the interval (a, b) is the minimum value of F (x) in (a, b) of the maximum value, so Fmin (F, xl, x2) f (x) function in the interval (xl the maximum value, X2)・Example 13 asks in [0,Minimum point in 5]・(1)establishment of function file mymin. m.Function fx二mymin (x)Fx=x.八3-2*x-5;(2)call the Fmin function to find the minimum point・Zero point eight one six fiveThree・Matlab numerical integration and differentiationI. numerical integration1.basic principles of numerical integrationNumerical methods for solving definite integration are various, such as simple trapezoidal method, Xin Pusheng (Simpson) method, Newton Cortes (Newton-Cotes) method, etc・,are often used・Their basic idea is to divide the whole integral interval [a and b] into n sub intervals [[, ], i=l, 2],・・・ N, in which,・ In this way, the problem of determining integral is decomposed into summation problem・2.realization method of numerical integrationVariable step symplectic methodBased on the variable step long Xin Pusheng method, MATLAB gives the quad function to find the definite integra 1. The call format of this function is:[I, n]二quad ('fname', a, B, tol, trace)Where fname is the product function name・A and B are the lower bound and upper bound of the definite integra 1. TOL is used to control the integration accuracy, and the tol=0.001 is taken by default・ Does the trace control show the integration process? If you take the non 0, it will show the integral process, take 0 instead of the default, and take the trace=0 bydefault・ Returns the parameter I, the fixed integral value, and the call number of n for the integrand・Finding definite integral(1)establish the integrand function file fesin.m.Function f二fesin (x)F二exp (一0.5*x)・ *sin (x+pi/6);(2)call the numerical integral function quad to find the definite integra1.[S, n]二quad ('fesin', 0, 3*pi)二SZero point nine Zero Zero eight二nSeventy-sevenThe Newton Cotes methodNewton Cotes method based on MATLAB, gives the quad8 function to find the definite integra 1. The call format of this functionis: [I, n]=quad8 C fname,, a, B, tol, trace)The meaning of the parameter is similar to that of the quad function, but only the default value of the tol. The function can more accurately calculate the value of the definite integral, and in general, the number of steps of the function call is less than the quad function, thus ensuring the higher efficiency to find the required integral value・Finding definite integral(1)the integrand function file fx. m・Function f二fx (x)F二x. *sin (x) / (1+cos (x)・*cos (x));(2)call the function quad8 to find the definite integral.I二quad8 (' fx' , 0, PI)二ITwo point four six seven fourExample 3 uses the quad function and the quad8 function respectively to find the approximate value of the integral, and compares the number of calls of the function under the same integral accuracy.Call the function quad to find the definite integral:Format long;Fx二inline ('exp (-x));[I, n]二quad ('FX', 1,2. 5, le-10)二IZero point two eight five seven nine four four four two fivefour seven six six二nSixty-fiveCall the function quad8 to find the definite integral:Format long;Fx二inline ('exp (-x));[I, n] =quad8 (FX, 1,2. 5, le-10)二IZero point two eight five seven nine four four four two fivefour seven five fourThirty-threeThe integrand is defined by a tableIn MATLAB, the problem of determining the function relations defined in tabular form is called trapz (X, Y) functions. The vector X, Y defines the function relation Y二f (X).Example 8-4 calculates definite integral with trapz function.The commands are as follows:X=l:0. 01:2.5:Y二exp (-X);% generated function relational data vectorTrapz (X, Y)二ansZero point two eight five seven nine six eight two four one six three nine threeNumerical solution of 3. double definite integralConsider the following double definite integral problem:By using the dblquad function provided by MATLAB, the numerical solution of the above double definite integral can be obtaineddirectly. The call format of this function is:I=dblquad (F, a, B, C, D, tol, trace)The function asks f (x, y) to double definite integral on the [a, b] * [c, d] regions・The parameter tol, trace is used exactly the same as function quad・Example 8~5 calculates the double definite integral(1)build a function file fxy.m:Function f二fxy (x, y)Global ki;Ki=ki+1:%ki is used to count the number of calls to the integrandF二exp (-x.八2/2). *sin (x. 2+y);(2)call the dblquad function to solve・Global ki; ki二0;I二dblquad (' fxy' , -2, 2, -1, 1)Ki1.57449318974494 (data format related)二kiOne thousand and thirty-eightTwo. Numerical differentiation1numerical difference and difference quotient2the realization of numerical differentiationIn MATLAB, there is no function that provides direct numerical derivatives・ Only the function diff, which calculates the forward difference, is called:DX二diff (X) : calculates the forward difference of vector X, DX (I), =X (i+1), -X (I), i二1,2,・・.N-l.DX二diff (X, n) : calculates the forward difference of the n order of X. For example, diff (X, 2)二diff (diff (X)).DX二diff (A, N, dim): calculates the n order difference of the matrix A, the dim=l (default state), calculates the difference by column, and dim=2 calculates the difference by line・6 cases of Vandermonde matrix generation based on vectorV= [ 1, 2, 3, 4, 5, 6], according to the column difference operation. The commands are as follows:V=vander (1:6)DV二diff (V)% calculates the first order difference of V 二V111111321684212438127931102425664164131256251252551777612962163661二DV31157310211 65 19 5 1 0781 175 37 7 1 02101 369 61 9 1 04651 671 91 11 1 0用不同的方法求函数f(X)的数值导数,并在同一个坐标系中做出f (X)的图像。
1.解方程1.1 线性方程组求解(1) 左除运算符的直接解法x=A\b(2) 迭代解法迭代解法非常适合求解大型系数矩阵的方程组。
迭代解法主要包括 Jacobi 迭代法、Gauss-Serdel迭代法、两步迭代法。
①Jacobi迭代法对于线性方程组Ax=b,如果A为非奇异方阵,即aii≠0(i=1,2,…,n),则可将A分解为A=D-L-U,其中D为对角阵,其元素为A的对角元素,L与U为A的下三角阵和上三角阵,于是Ax=b化为:x=D-1(L+U)x+D-1b与之对应的迭代公式为:x(k+1)=D-1(L+U)x(k)+D-1b 这就是Jacobi迭代公式。
A=[10,-1,5;-1,10,-2;6,-4,50];b=[53 6]';[y,n]=jacobi(A,b,[0. 0. 0.]',1.0e-6)A\by =0.49150.36720.0904n =13ans =0.49150.36720.0904②Gauss-Serdel迭代法在Jacobi迭代过程中,计算时,已经得到,不必再用,即原来的迭代公式D x(k+1)=(L+U) x(k)+b可以改进为D x(k+1)=L x(k+1)+U x(k)+b,于是得到:x(k+1)=(D-L)-1Ux(k)+(D-L)-1b[y,n]=gauseidel(A,b,[0. 0. 0.]',1.0e-6)y =0.49150.36720.0904n =61.2 非线性方程数值求解①单变量非线性方程求解fzero函数可以用来求单变量非线性方程的根。
该函数的调用格式为:z=fzero(fname,x0,tol,trace)fname是待求根的函数文件名,x0为搜索的起点。
一个函数可能有多个根,但fzero函数只给出离x0最近的那个根。
tol控制结果的相对精度,缺省时取tol=eps,trace•指定迭代信息是否在运算中显示,为1时显示,为0时不显示,缺省时取trace=0。
线性方程组求解非线性方程数值求解常微分方程初值问题地数值解法函数极值线性方程组求解直接解法.利用左除运算符地直接解法对于线性方程组,可以利用左除运算符“\”求解:\例用直接解法求解下列线性方程组.命令如下:[];[]';\.利用矩阵地分解求解线性方程组矩阵分解是指根据一定地原理用某种算法将一个矩阵分解成若干个矩阵地乘积.常见地矩阵分解有分解、分解、分解,以及分解、分解、奇异分解等.文档来自于网络搜索() 分解矩阵地分解就是将一个矩阵表示为一个交换下三角矩阵和一个上三角矩阵地乘积形式.线性代数中已经证明,只要方阵是非奇异地,分解总是可以进行地.文档来自于网络搜索提供地函数用于对矩阵进行分解,其调用格式为:[]():产生一个上三角阵和一个变换形式地下三角阵(行交换),使之满足.注意,这里地矩阵必须是方阵.文档来自于网络搜索[]():产生一个上三角阵和一个下三角阵以及一个置换矩阵,使之满足.当然矩阵同样必须是方阵.文档来自于网络搜索实现分解后,线性方程组地解\(\)或\(\),这样可以大大提高运算速度.文档来自于网络搜索例用分解求解例中地线性方程组.命令如下:[];[]';[]();\(\)或采用分解地第种格式,命令如下:[ ]();\(\*)() 分解对矩阵进行分解,就是把分解为一个正交矩阵和一个上三角矩阵地乘积形式.分解只能对方阵进行.地函数可用于对矩阵进行分解,其调用格式为:文档来自于网络搜索[]():产生一个一个正交矩阵和一个上三角矩阵,使之满足.[]():产生一个一个正交矩阵、一个上三角矩阵以及一个置换矩阵,使之满足.文档来自于网络搜索实现分解后,线性方程组地解\(\)或(\(\)).例用分解求解例中地线性方程组.命令如下:[];[]';[]();\(\)或采用分解地第种格式,命令如下:[]();*(\(\))() 分解如果矩阵是对称正定地,则分解将矩阵分解成一个下三角矩阵和上三角矩阵地乘积.设上三角矩阵为,则下三角矩阵为其转置,即'.函数()用于对矩阵进行分解,其调用格式为:文档来自于网络搜索():产生一个上三角阵,使'.若为非对称正定,则输出一个出错信息.[]():这个命令格式将不输出出错信息.当为对称正定地,则,与上述格式得到地结果相同;否则为一个正整数.如果为满秩矩阵,则为一个阶数为地上三角阵,且满足'().文档来自于网络搜索实现分解后,线性方程组变成‘,所以\(’\).例用分解求解例中地线性方程组.命令如下:[];[]';()??? >命令执行时,出现错误信息,说明为非正定矩阵.迭代解法迭代解法非常适合求解大型系数矩阵地方程组.在数值分析中,迭代解法主要包括迭代法、迭代法、超松弛迭代法和两步迭代法.文档来自于网络搜索.迭代法对于线性方程组,如果为非奇异方阵,即≠(,…),则可将分解为,其中为对角阵,其元素为地对角元素,与为地下三角阵和上三角阵,于是化为:文档来自于网络搜索()与之对应地迭代公式为:()()()这就是迭代公式.如果序列{()}收敛于,则必是方程地解.迭代法地函数文件如下:[]();<(()); 求地对角矩阵(); 求地下三角阵(); 求地上三角阵\();\;*;; 迭代次数()>;*;;例用迭代法求解下列线性方程组.设迭代初值为,迭代精度为.在命令中调用函数文件,命令如下:[];[]';[](,[]').迭代法在迭代过程中,计算时,已经得到,不必再用,即原来地迭代公式()()()可以改进为()()(),于是得到:文档来自于网络搜索()()()()该式即为迭代公式.和迭代相比,迭代用新分量代替旧分量,精度会高些.文档来自于网络搜索迭代法地函数文件如下:[]();<(()); 求地对角矩阵(); 求地下三角阵(); 求地上三角阵()\;()\;*;; 迭代次数()>;*;;例用迭代法求解下列线性方程组.设迭代初值为,迭代精度为.在命令中调用函数文件,命令如下:[];[]';[](,[]')例分别用迭代和迭代法求解下列线性方程组,看是否收敛.命令如下:[];[];[](,[])[](,[])非线性方程数值求解单变量非线性方程求解在中提供了一个函数,可以用来求单变量非线性方程地根.该函数地调用格式为:('')其中是待求根地函数文件名,为搜索地起点.一个函数可能有多个根,但函数只给出离最近地那个根.控制结果地相对精度,缺省时取,•指定迭代信息是否在运算中显示,为时显示,为时不显示,缺省时取.文档来自于网络搜索例求()在附近地根.步骤如下:() 建立函数文件.().^;() 调用函数求根.('')非线性方程组地求解对于非线性方程组(),用函数求其数值解.函数地调用格式为:('')其中为返回地解,是用于定义需求解地非线性方程组地函数文件名,是求根过程地初值,为最优化工具箱地选项设定.最优化工具箱提供了多个选项,用户可以使用命令将它们显示出来.如果想改变其中某个选项,则可以调用()函数来完成.例如,选项决定函数调用时中间结果地显示方式,其中‘’为不显示,‘’表示每步都显示,‘’只显示最终结果.(‘’,‘’)将设定选项为‘’.文档来自于网络搜索例求下列非线性方程组在() 附近地数值解.() 建立函数文件.()();();()*()*();()*()*();() 在给定地初值下,调用函数求方程地根.('',[]'('',''))文档来自于网络搜索将求得地解代回原方程,可以检验结果是否正确,命令如下:*可见得到了较高精度地结果.常微分方程初值问题地数值解法龙格-库塔法简介龙格-库塔法地实现基于龙格-库塔法,提供了求常微分方程数值解地函数,一般调用格式为:[]('')[]('')其中是定义()地函数文件名,该函数文件必须返回一个列向量.形式为[],表示求解区间.是初始状态列向量.和分别给出时间向量和相应地状态向量.文档来自于网络搜索例设有初值问题,试求其数值解,并与精确解相比较(精确解为()).() 建立函数文件.()(^)();() 求解微分方程.;;[]('',[]); 求数值解(); 求精确解'''为数值解,为精确值,显然两者近似.例求解著名地方程.例有模型地状态方程,试绘制系统相平面图.函数极值提供了基于单纯形算法求解函数极值地函数和,•它们分别用于单变量函数和多变量函数地最小值,其调用格式为:文档来自于网络搜索('')('')这两个函数地调用格式相似.其中函数用于求单变量函数地最小值点.是被最小化地目标函数名,和限定自变量地取值范围.函数用于求多变量函数地最小值点,是求解地初始值向量.文档来自于网络搜索没有专门提供求函数最大值地函数,但只要注意到()在区间()上地最小值就是()在()地最大值,所以()返回函数()在区间()上地最大值.文档来自于网络搜索例求()在[]内地最小值点.() 建立函数文件.().^*;() 调用函数求最小值点.('')。
第7章MATLAB解方程与函数极值第7章MATLAB解方程与函数极值7.1线性方程组求解7.2非线性方程数值求解7.3常微分方程初值问题的数值解法7.4函数极值7.1线性方程组求解7.1.1直接解法1.利用左除运算符的直接解法对于线性方程组Ax=b,可以利用左除运算符“\”求解:x=A\b例7-1用直接解法求解下列线性方程组。
命令如下:A=[2,1,-5,1;1,-5,0,7;0,2,1,-1;1,6,-1,-4];b=[13,-9,6,0]'';x=A\b2.利用矩阵的分解求解线性方程组矩阵分解是指根据一定的原理用某种算法将一个矩阵分解成若干个矩阵的乘积。
常见的矩阵分解有LU分解、QR分解、Cholesky分解,以及Schur分解、Hessenberg分解、奇异分解等。
(1)LU分解矩阵的LU分解就是将一个矩阵表示为一个交换下三角矩阵和一个上三角矩阵的乘积形式。
线性代数中已经证明,只要方阵A是非奇异的,LU分解总是可以进行的。
MATLAB提供的lu函数用于对矩阵进行LU分解,其调用格式为:[L,U]=lu(X):产生一个上三角阵U和一个变换形式的下三角阵L(行交换),使之满足X=LU。
注意,这里的矩阵X必须是方阵。
[L,U,P]=lu(X):产生一个上三角阵U和一个下三角阵L以及一个置换矩阵P,使之满足PX=LU。
当然矩阵X同样必须是方阵。
实现LU分解后,线性方程组Ax=b的解x=U\(L\b)或x=U\(L\Pb),这样可以大大提高运算速度。
例7-2用LU分解求解例7-1中的线性方程组。
命令如下:A=[2,1,-5,1;1,-5,0,7;0,2,1,-1;1,6,-1,-4];b=[13,-9,6,0]'';[L,U]=lu(A);x=U\(L\b)或采用LU 分解的第2种格式,命令如下:[L,U,P]=lu(A);x=U\(L\Pb)(2)QR分解对矩阵X进行QR分解,就是把X分解为一个正交矩阵Q和一个上三角矩阵R的乘积形式。
《MATLAB程序设计教程》第7章_MATLAB解方程与函数极值《MATLAB程序设计教程》第7章主要介绍了MATLAB解方程和寻找函数极值的方法和技巧。
本章包括方程求解的基本原理和方法、使用MATLAB求解方程的一般步骤、常见方程求解函数的使用、函数极值的概念和求解方法等内容。
第一节介绍了方程求解的基本原理和方法。
方程是指等式中含有未知数的数学表达式,求解方程就是找到满足该方程的未知数的值。
主要介绍了一元方程和多元方程的求解方法,包括代数法、图形法、逼近法和迭代法等。
其中,迭代法是指通过不断逼近解的方法,使得方程两边的差异越来越小,最终得到近似解。
第二节介绍了使用MATLAB求解方程的一般步骤。
通过引用MATLAB中的方程求解函数,可以快速、准确地求解各种方程。
主要包括定义方程、选择合适的求解函数、输入参数、调用函数求解方程、查看结果等几个步骤。
通过实例演示了如何使用MATLAB求解一元方程和多元方程。
第三节介绍了常见方程求解函数的使用。
MATLAB提供了多种求解方程的函数,如符号计算工具箱中的solve函数、数值计算工具箱中的fsolve函数、优化工具箱中的fmincon函数等。
主要介绍了这些函数的基本用法和输入输出参数。
第四节介绍了函数极值的概念和求解方法。
函数极值是指函数的最大值和最小值,在数学和工程领域中具有重要的应用价值。
主要介绍了一元函数极值的求解方法,包括使用MATLAB中的fminbnd函数和fminsearch 函数。
通过实例演示了如何使用这些函数求解一元函数的极值。
通过学习本章内容,读者可以了解方程求解和函数极值的基本原理和方法,并掌握使用MATLAB进行方程求解和函数极值求解的技巧和方法。
这对于数学建模、优化问题求解和工程实际应用等方面都具有重要的指导意义。
函数的极值与matlab应用函数的极值与matlab应用一、什么是函数的极值在数学中,函数的极值指的是在给定的定义域中函数的最大值和最小值。
当函数的导数为0或不存在时,函数就有可能有极值。
具体来说,当函数在该点的导数为0,或该点左侧的导数为正、右侧导数为负,或左侧导数为负、右侧导数为正时,该点就是函数的极值点。
极大值指的是函数在该点的值比周围点更大,而极小值则代表函数在该点的值更小。
二、如何通过matlab求解函数的极值matlab是一种数学软件,它支持各种数学操作,包括求解函数的极值。
在matlab中,我们可以使用fminbnd和fminsearch函数来找到函数的最小值,使用fmaxbnd和fminimax函数来找到函数的最大值。
这些函数使用类似于梯度下降的算法来搜索函数的最值,一旦找到极值点,就可以使用fmincon函数来求解该点的极值。
三、matlab实例下面给出一个matlab实例,该实例演示了如何使用matlab求解函数的极值。
假设我们要求解以下函数的极小值:f(x) = sin(x) + cos(2x) + x^2我们可以使用fminbnd函数来求解该函数的最小值:f = @(x) sin(x) + cos(2*x) + x^2;x0 = 1;xmin = fminbnd(f, -5, 5, x0)上述代码会计算出函数在x=-1.2673时的最小值,此时f(x)约等于-0.925。
类似地,我们可以使用fmaxbnd函数来求解该函数的最大值:f = @(x) sin(x) + cos(2*x) + x^2;x0 = 1;xmax = fmaxbnd(@(x) -f(x), -5, 5, x0)上述代码会计算出函数在x=-3.3706时的最大值,此时f(x)约等于3.829。
最后,我们可以使用fmincon函数来求解极值点的详细信息:f = @(x) sin(x) + cos(2*x) + x^2;[xmin, fval] = fmincon(f, -5, [], [], [], [], -5, 5)上述代码会计算出函数的全局最小值(因为定义域为[-5,5],所以与之前的结果相同),此时f(x)约等于-1.056。