优化方法上机作业--2013
试验优化设计--第一章(2013)
• 回归设计基本概念 一、定义
• 就是设计试验,求取方程。
• 回归设计主要是从正交性、旋转性和D一
优良性等某种优良性出发; • 利用正交表、H阵、单纯阵、正交多项式 以及计算技术编制试验方案; • 实施实验,直接求取各种线性和非线性 回归方程,并进行寻优预测。 •Regression Design
未经编码的试验因素Z1、Z2、Z3
• 特点: 每一因素都有具体的物理意义; 有的有量纲,有的无量纲; 可以是连续的,也可以是离散的。 • 自然空间: 由自然因素构成的空间。
• 编码因素:是经过编码得到的因素, 通常记为X1 , X2 , X3…
• 特点:
无具体的物理意义; 无量纲; 全部是离散的;
作用
• 旋转性应用于各种回归旋转设计 ,它保证 p 维因素空间同一 p 维 ˆ) 球面上各点的预测方差 相等; D( y
ˆ ) 的方向性; • 消除了 D( y
• 消除了优化的方向性;
• 进一步调优创造了条件。
4、D-优良性
• 定义:
在p 维编码空间确定的区域Z上,对于给定的回归 * 模型,若 ( N ) 相对于一切可能的方案 ( N ) ,满足 或 A( ( N )) max A( ) C( (N )) min C( ) 则称 * ( N ) 具有D— —优良性。 * ( N ) 是D最优化设计 A( ( N )) C( ( N )) ——信息矩阵 max A( ) min C( ) ——信息矩阵逆矩阵
• 编码空间:由编码因素构成的空间。
因素编码
概念
• 因素编码:将自然因素通过编码公式变成 编码因素的过程。
• 编码公式:
X j f (Z j )
高中数学必修一《优化方案》答案-第一章
1.1.1集合的含义与表示[读教材·填要点]1.元素与集合(1)元素与集合的定义:一般地,把研究对象统称为元素,把一些元素组成的总体叫做集合(简称为集).(2)集合中元素的性质:①确定性:即给定的集合,它的元素是确定的.②互异性:即给定集合的元素是互不相同的.③无序性.(3)集合相等:只要构成两个集合的元素是一样的,就称这两个集合是相等的.(4)元素与集合的关系:a是集合A的元素,记作a∈A,a不是集合A的元素,记作a∉A.2.集合的表示方法除了用自然语言表示集合外,还可以用列举法和描述法表示集合.(1)列举法:把集合中的元素一一列举出来,并用花括号“{}”括起来表示集合的方法.(2)描述法:用集合所含元素的共同特征表示集合的方法.3.常用数集及其记法1.著名数学家能否构成一个集合?提示:不能,没有一定的评定标准,故著名数学家是不确定的对象,所以不能构成集合.2.一个集合能表示成{s,k,t,k}吗?提示:不能,集合中的元素是互不相同的,任何两个相同的对象在同一个集合中,只能算作这个集合的一个元素.3.集合{-5,-8}和{(-5,-8)}是同一集合吗?提示:不是同一集合.集合{-5,-8}中元素有2个,为数.而集合{(-5,-8)}中有一个元素为坐标(-5,-8).[例1](1)某校2013年在校的所有高个子同学;(2)不超过20的非负数;(3)帅哥;(4)直角坐标系平面内第一象限的一些点;(5)3的近似值的全体.[自主解答]“高个子”没有明确的标准,因此(1)不能构成集合.(2)任给一个实数x,可以明确地判断是不是“不超过20的非负数”,即“0≤x≤20”与“x>20或x<0”,两者必居其一,且仅居其一,故“不超过20的非负数”能构成集合;(3)“帅哥”没有一个明确的标准,不能构成集合;(4)“一些点”无明确的标准,对于某个点是否在“一些点”中无法确定,因此“直角坐标平面内第一象限的一些点”不能构成集合;(6)“3的近似值”不明确精确到什么程度,因此很难判断一个数如“2”是不是它的近似值,所以(5)不能构成集合.——————————————————判断指定的对象能不能构成集合,关键在于能否找到一个明确标准,对于任何一个对象,都能确定它是不是给定集合的元素,同时还要注意集合中元素的互异性、无序性.————————————————————————————————————————1.下列能构成集合的是()A.中央电视台著名节目主持人B.2013年沈阳全运会比赛的所有项目C.2010年上海世博园中所有漂亮的展馆D.世界上的高楼答案:B[例2]已知集合A={a[自主解答]若a+2=1,则a=-1,所以A={1,0,1},与集合中元素的互异性矛盾,应舍去;若(a+1)2=1,则a=0或a=-2,当a=0时,A={2,1,3},满足题意.当a=-2时,A={0,1,1},与集合中元素的互异性矛盾,舍去;若a2+3a+3=1,则a=-1或a=-2(均舍去).综上可知,a=0.例2中1∈A改为4∈A,则结果如何?解:若a+2=4,则a=2.∴A={4,9,13}满足题意.若(a+1)2=4,则a=1或a=-3.当a=1时,A={3,4,7},满足题意.当a=-3时,A={-1,3,4,}满足题意.若a 2+3a +3=4,则a =-3±132,代入后都满足题意,故a 的值为a =1,a =2,或a =-3或a =-3±132.——————————————————1.这类问题既要用元素的确定性,又要利用互异性检验解的正确与否.初学者解题时易忽略元素的互异性,学习中要高度重视.另外,本类问题往往涉及分类讨论的数学思想.2.一个集合中,元素之间没有先后顺序,只要构成两个集合的元素是一样的,这两个集合就是同一个集合. ————————————————————————————————————————2.含有两个实数的集合A 可以表示为{a -3,2a -1},求实数a 的取值范围. 解:∵A ={a -3,2a -1},∴由集合中元素的互异性可得a -3≠2a -1. ∴a ≠-2.∴a 的取值范围为a ≠-2.[例3] (1)方程组⎩⎪⎨⎪⎧x +y =3x -y =5的解集;(2)不等式2x -3>5的解集.[自主解答] (1)集合用描述法表示为{(x ,y )|⎩⎪⎨⎪⎧ x +y =3x -y =5}.解方程组,得⎩⎪⎨⎪⎧x =4,y =-1故集合用列举法表示为{(4,-1)}.(2)由2x -3>5可得x >4,所以不等式2x -3>5的解集为{x |x >4,x ∈R }. ——————————————————1.一个集合可以用不同的方法表示,需根据题意选择适当的方法,同时注意列举法和描述法的适用范围. 2.方程(或方程组)的解的个数较少,因此方程(或方程组)的解集一般用列举法表示;不等式(或不等式组)的解集一般用描述法表示.注意,当题目中要求求出“…的解集”或写出“…的集合”时,一定要将最终结果写成集合的形式.————————————————————————————————————————3.有下面六种表示方法①{x =-1,y =2} ②⎩⎨⎧(x ,y )⎪⎪⎪ ⎩⎪⎨⎪⎧⎭⎪⎬⎪⎫x =-1,y =2.③{-1,2} ④(-1,2) ⑤{(-1,2)} ⑥{x ,y |x =-1,或y =2}.其中,能正确表示方程组⎩⎪⎨⎪⎧2x +y =0,x -y +3=0的解集的是________(把所有正确答案的序号填在空格上).解析:[错解] ∵x 3∈A ,故x 3=0或x 3=1或x 3=x , 若x 3=0,则x =0; 若x 3=1,则x =1; 若x 3=x ,则x =1或x =0. 综上所述:所求x 的值为0或1.[错因] 本题错误的原因有两个,一是没有考虑到元素的互异性,解出来的结果没有代入检验,得出了错误结果;二是解x 2=x 时漏掉了x =-1这个答案,也导致了错误的结果.[正解] ∵x 3∈A , ∴x 3是集合A 中的元素.又∵集合A 中含有3个元素,∴需分情况讨论:①若x 3=0,则x =0,此时集合A 中有两个元素0,不符合集合中元素的互异性,舍去; ②若x 3=1,则x =1,此时集合A 中有两个元素1,不符合集合中元素的互异性,舍去;③若x 3=x ,则x =0、x =-1或x =1,当x =0、x =1时不符合集合中元素的互异性,都舍去.当x =-1时,此时集合A 中有三个元素1,0,-1,符合集合中元素的互异性;综上可知,x =-1. 1.有下列各组对象: ①接近于0的数的全体; ②比较小的正整数的全体;③平面上到点O 的距离等于1的点的全体; ④正三角形的全体.其中能构成集合的个数是( ) A .2 B .3 C .4D .5解析:①不能构成集合,“接近”的概念模糊,无明确标准.②不能构成集合,“比较小”也是不明确的,多小算小没明确标准.③④均可构成集合,因为任取一个元素是否是此集合的元素有明确的标准可依.答案:A2.下面几个命题中正确命题的个数是( ) ①集合N *中最小的数是1; ②若-a ∉N *,则a ∈N *;③若a ∈N *,b ∈N *,则a +b 最小值是2; ④x 2+4=4x 的解集是{2,2}. A .0 B .1 C .2D .3解析:N *是正整数集,最小的正整数是1,故①正确;当a =0时,-a ∉N *,且a ∉N *,故②错;若a ∈N *,则a 的最小值是1,又b ∈N *,b 的最小值也是1,当a 和b 都取最小值时,a +b 取最小值2,故③正确;由集合元素的互异性知④是错误的.故①③正确.答案:C3.已知集合M ={3,m +1},且4∈M ,则实数m 等于( ) A .4 B .3 C .2D .1解析:∵4∈M ,∴4=m +1,∴m =3. 答案:B4.已知①5∈R ②13∈Q ③0={0} ④0∉N⑤π∈Q ⑥-3∈Z .正确的个数为________. 解析:①②⑥是正确的;③④⑤是错误的. 答案:35.用适当的符号填空:已知A ={x |x =3k +2,k ∈Z },B ={x |x =6m -1,m ∈Z },则有:17______A ;-5______A ;17________B .解析:令3k +2=17得,k =5∈Z . 所以17∈A .令3k +2=-5得,k =-73∉Z .所以-5∉A .令6m -1=17得,m =3∈Z , 所以17∈β. 答案:∈,∉,∈6.用适当的方法表示下列集合: (1)一年中有31天的月份的全体; (2)大于-3.5小于12.8的整数的全体; (3)梯形的全体构成的集合; (4)所有非负偶数的集合; (5)所有能被3整除的数的集合; (6)方程(x -1)(x -2)=0的解集; (7)不等式2x -1>5的解集.解:(1){1月,3月,5月,7月,8月,10月,12月}. (2){-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,11,12}. (3){x |x 是梯形}或{梯形}. (4){0,2,4,6,8,…}. (5){x |x =3n ,n ∈Z }. (6){1,2}. (7){x |2x -1>5}. 一、选择题1.下列给出的对象中,能组成集合的是( ) A .一切很大的数 B .高中数学的所有难题 C .美丽的小女孩D .方程x 2-1=0的实数根解析:选项A ,B ,C 中的对象都没有明确的判断标准,不满足集合中元素的确定性,故A ,B ,C 中的对象都不能组成集合.答案:D2.下列命题不.正确的有( )①很小的实数可以构成集合;②集合{y |y =x 2-1}与集合{(x ,y )|y =x 2-1}是同一个集合; ③1,32,64,⎪⎪⎪⎪-12,0.5这些数组成的集合有5个元素; ④集合{(x ,y )|xy ≤0,x ,y ∈R }是指第二和第四象限内的点集. A .1个 B .2个 C .3个D .4个解析:①错的原因是元素不确定;②前者是数集,而后者是点集,种类不同;③32=64,⎪⎪⎪⎪-12=0.5,有重复的元素,应该是3个元素;④该集合还包括坐标轴上的点.答案:D3.已知集合A ={1,2,3,4,5},B ={(x ,y )|x ∈A ,y ∈A ,x -y ∈A },则B 中所含元素的个数为( ) A .3 B .6 C .8D .10解析:列举得集合B ={(2,1),(3,1),(4,1),(5,1),(3,2),(4,2),(5,2),(4,3),(5,3),(5,4)},共含有10个元素.答案:D4.定义集合运算:A *B ={z |z =xy ,x ∈A ,y ∈B }.设A ={1,2},B =(0,2),则集合A *B 的所有元素之和为( ) A .0 B .2 C .3D .6解析:依题意,A *B ={0,2,4},其所有元素之和为6. 答案:D 二、填空题5.集合A ={(2,-2),(2,2)}中含有________个元素. 解析:∵(2,-2),(2,2)是两个点,∴有2个元素. 答案:26.已知集合A ={(x ,y )|y =2x +1},B ={(x ,y )|y =x +3},a ∈A 且a ∈B ,则a 为________. 解析:∵a ∈A 且a ∈B ,∴a 是方程组⎩⎪⎨⎪⎧y =2x +1y =x +3的解.解方程组,得⎩⎪⎨⎪⎧x =2y =5,∴a 为(2,5). 答案:(2,5)7.用描述法表示方程x <-x -3的解集为________. 解析:∵x <-x -3, ∴x <-32.∴解集为{x |x <-32}.答案:{x |x <-32}8.{(x ,y )|(x +2)2+|y -3|=0,x ,y ∈R }=________.解析:由(x +2)2+|y -3|=0,又(x +2)2≥0,|y -3|≥0,所以(x +2)2=0,|y -3|=0,所以x =-2,y =3,所以{(x ,y )|(x +2)2+|y -3|=0,x ,y ∈R }={(-2,3)}.答案:{(-2,3)} 三、解答题9.已知集合A 含有两个元素a -3和2a -1, (1)若-3∈A ,试求实数a 的值. (2)若a ∈A ,试求实数a 的值. 解:(1)因为-3∈A ,所以-3=a -3或-3=2a -1. 若-3=a -3,则a =0.此时集合A 含有两个元素-3,-1,符合题意. 若-3=2a -1, 则a =-1.此时集合A 含有两个元素-4,-3,符合题意, 综上所述,满足题意的实数a 的值为0或-1. (2)因为a ∈A ,所以a =a -3或a =2a -1.当a =a -3时,有0=-3,不成立.当a =2a -1时,有a =1,此时A 中有两个元素-2,1,符合题意.综上知a =1.10.已知集合A ={x |kx 2-8x +16=0}只有一个元素,试求实数k 的值,并用列举法表示集合A . 解:当k =0时,原方程变为-8x +16=0, 所以x =2,此时集合A ={2};当k ≠0时,要使一元二次方程kx 2-8x +16=0有两个相等实根,需Δ=64-64k =0,即k =1.此时方程的解为x1=x2=4,集合A={4}.1.1.2集合间的基本关系[读教材·填要点]1.子集的概念2.A B(或B A)3.(1)定义:不含任何元素的集合叫做空集.(2)用符号表示为:∅.(3)规定:空集是任何集合的子集.4.子集的有关性质(1)任何一个集合是它本身的子集,即A⊆A.(2)对于集合A,B,C,如果A⊆B,且B⊆C,那么A⊆C.[小问题·大思维]1.若A B,则A⊆B且A≠B,对吗?提示:对.∵A B,首先A⊆B,其中B中至少有一个元素不属于A,即A≠B.2.任何集合都有真子集吗?提示:不是,空集∅就没有真子集.3.{0}和∅表示同一集合吗?它们之间有什么关系?提示:{0}和∅不是同一个集合.{0}表示含有一个元素0的集合,∅是不含任何元素的集合,且∅{0}.[例1]写出集合A=[自主解答]由0个元素构成的子集:∅;由1个元素构成的子集:{1},{2},{3};由2个元素构成的子集:{1,2},{1,3},{2,3};由3个元素构成的子集:{1,2,3}.由此得集合A的所有子集为∅,{1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}.在上述子集中,除去集合A本身,即{1,2,3},剩下的都是A的真子集.——————————————————1.求解有限集合的子集问题,关键有三点:(1)确定所求集合;(2)合理分类,按照子集所含元素的个数依次写出;(3)注意两个特殊的集合,即空集和集合本身.2.一般地,若集合A中有n个元素,则其子集有2n个,真子集有2n-1个,非空真子集有2n-2个. ————————————————————————————————————————1.已知集合M满足{2,3}⊆M⊆{1,2,3,4,5},求集合M及其个数.解:当M中含有两个元素时,M为{2,3};当M中含有三个元素时,M为{2,3,1},{2,3,4},{2,3,5};当M中含有四个元素时,M为{2,3,1,4},{2,3,1,5},{2,3,4,5};当M中含有五个元素时,M为{2,3,1,4,5}.所以满足条件的集合M为{2,3},{2,3,1},{2,3,4},{2,3,5},{2,3,1,4},{2,3,1,5},{2,3,4,5},{2,3,1,4,5},集合M的个数为8.[例2]下列各式正确的是(1){a}⊆{a};(2){1,2,3}={3,1,2};(3)0⊆{0};(4){1}{x|x≤5};(5){1,3}{3,4}.[自主解答]∵1<5,∴1∈{x|x≤5}.∴{1}⊆{x|x≤5}.又∵{1}≠{x|x≤5},∴{1}{x|x≤5}.∵1∈{1,3},但1∉{3,4},∴{1,3}{3,4}.“”是“真包含于”的意思[——————————————————集合间关系的判定的步骤:首先,判断一个集合A中的任意元素是否属于另一集合B,若是,则A⊆B,否则A B;,其次,判断另一个集合B中的任意元素是否属于第一个集合A,若是,则B⊆A,否则B A;,最后,下结论:若A⊆B,B⊆A,则A =B ;若A ⊆B ,B A ,则A B ;若A B ,B ⊆A ,则B A ;若上述三种情况都不成立,则A B ,B A .[注意] 有时一个集合可以看成另一个集合的元素,如{1}可以看成集合{{1},1,2,3}中的元素,也可以看成子集,因此{1}∈{{1},1,2,3}与{1}⊆{{1},1,2,3}都正确.————————————————————————————————————————2.集合M ={x |x 2+x -6=0},N ={x |2x +7>0},试判断集合M 和N 的关系. 解:M ={-3,2},N =⎩⎨⎧⎭⎬⎫x |x >-72.∵-3>-72,2>-72,∴-3∈N,2∈N .∴M ⊆N . 又0∈N ,但0∉M ,∴M N .[例3] 已知集合A ={x |-3m 的取值范围. [自主解答] ∵B ⊆A ,(1)当B =∅时,m +1≤2m -1,解得m ≥2. (2)当B ≠∅时,有⎩⎪⎨⎪⎧-3≤2m -1,m +1≤4,2m -1<m +1解得-1≤m <2, 综上得m ≥-1. ——————————————————(1)利用集合之间的关系时,首先要分析、简化每个集合.(2)此类问题通常借助数轴,利用数轴分析法,将各个集合在数轴上表示出来,以形定数,还要注意验证端点值,做到准确无误,一般含“=”用实点表示,不含“=”用虚点表示.(3)此类问题还应注意“空集”这一“陷阱”,尤其是集合中含有字母参数时,初学者会想当然认为非空集合而丢解,因此分类讨论是必须的.————————————————————————————————————————3.设集合A ={1,3,a },B ={1,a 2-a +1},且A ⊇B ,求a 的值. 解:∵A ⊇B ,而a 2-a +1∈B ,∴a 2-a +1∈A . ∴a 2-a +1=3或a 2-a +1=a . 当a 2-a +1=3时,a =2或a =-1.(1)a =2时,A ={1,3,2},B ={1,3},这时满足条件A ⊇B ; (2)a =-1时,A ={1,3,-1},B ={1,3},这时也满足条件A ⊇B .当a 2-a +1=a 时,a =1,此时A ={1,3,1},B ={1,1},根据集合中元素的互异性,故舍去a =1. ∴a 的值为2或-1.[错解] ∵M ={x |x 2-3x +2=0}={1,2},(1)当N ={1}时,有⎩⎪⎨⎪⎧ 1+1=2,1×1=a ,∴a =1.(2)当N ={2}时,有⎩⎪⎨⎪⎧ 2+2=2,2×2=a ,不成立.(3)当N ={1,2}时,有⎩⎪⎨⎪⎧1+2=2,1×2=a ,不成立.所以,a =1.[错因] 空集是一个特殊的集合,是任何集合的子集,在解决集合关系问题时极易忽略∅,错解中没有考虑集合N 为∅的情况.[正解] ∵M ={x |x 2-3x +2=0}={1,2},又N ⊆M ,∴N =∅,或N ={1},或N ={2},或N ={1,2}. (1)当N =∅时,方程x 2-2x +a =0的判别式Δ=4-4a <0,即a >1.(2)当N ={1}时,有⎩⎪⎨⎪⎧1+1=2,1×1=a ,∴a =1.(3)当N ={2}时,有⎩⎪⎨⎪⎧ 2+2=2,2×2=a ,不成立.(4)当N ={1,2}时,有⎩⎪⎨⎪⎧1+2=2,1×2=a ,不成立.综上可知实数a 的取值范围是a ≥1. 1.下列命题中,正确的有( ) ①空集是任何集合的真子集; ②若A B ,B C ,则A C ;③任何一个集合必有两个或两个以上的真子集; ④如果不属于B 的元素也不属于A ,则A ⊆B . A .①② B .②③ C .②④D .③④解析:①空集只是空集的子集而非真子集,故①错;②真子集具有传递性,故②正确;③若一个集合是空集,则没有真子集,故③错;④由韦恩(Venn)图易知④正确.答案:C2.设集合M ={x |x >-2},则下列选项正确的是( ) A .{0}⊆M B .{0}∈M C .∅∈MD .0⊆M解析:选项B 、C 中均是集合之间的关系,符号错误;选项D 中是元素与集合之间的关系,符号错误. 答案:A3.已知集合A ={x |x 是平行四边形},B ={x |x 是矩形},C ={x |x 是正方形},D ={x |x 是菱形},则( ) A .A ⊆B B .C ⊆B C .D ⊆CD .A ⊆D解析:选项A 错,应当是B ⊆A .选项B 对,正方形一定是矩形,但矩形不一定是正方形.选项C 错,正方形一定是菱形,但菱形不一定是正方形.选项D 错,应当是D ⊆A .答案:B 4.已知∅{x |x 2-x +a =0},则实数a 的取值范围是________. 解析:∵∅{x |x 2-x +a =0}.∴{x |x 2-x +a =0}≠∅. 即x 2-x +a =0有实根. ∴Δ=(-1)2-4a ≥0,得a ≤14.答案:a ≤145.若{a,0,1}={c ,1b ,-1},则a =________,b =________,c =________.解析:∵1b ≠0,∴c =0,∴a =-1,1b =1.∴a =-1,b =1.答案:-1 1 06.已知集合A ={-1,3,2m -1},集合B ={3,m 2},若B ⊆A ,求实数m 的值.解:∵B ⊆A ,∴m 2=-1,或m 2=2m -1,当m 2=-1时,显然无实数根;当m 2=2m -1时,m =1.∴实数m =1.一、选择题1.已知集合M ={x ∈Z |-3<x ≤1},则它的真子集的个数为( ) A .12 B .14 C .15D .16解析:∵M ={x ∈Z |-3<x ≤1}={-2,-1,0,1}共有4个元素,∴它的真子集共有24-1=15个. 答案:C2.定义集合A *B ={x |x ∈A ,且x ∉B },若A ={1,2,3,4,5},B ={2,4,5},则A *B 的子集个数为( ) A .1 B .2 C .3D .4解析:由题意知A *B ={1,3}, ∴A *B 的子集个数为22=4个. 答案:D3.已知集合M ={x |-5<x <3,x ∈Z },则下列集合中为集合M 子集的是( ) A .P ={-3,0,1} B .Q ={-1,0,1,2}C .R ={y |-π<y <-1,y ∈Z }D .S ={x ||x |≤3,x ∈N }解析:先用列举法表示集合,再观察元素与集合的关系.集合M ={-2,-1,0,1},集合R ={-3,-2},S ={0,1},不难发现集合P 中的元素-3∉M ,集合Q 中的元素2∉M ,集合R 中的元素-3∉M ,而S ={0,1}中的任意一个元素都在集合M 中,所以S ⊆M ,且S M .答案:D4.已知集合A ⊆{0,1,2},且集合A 中至少含有一个偶数,则这样的集合A 的个数为( ) A .6 B .5 C .4D .3解析:集合{0,1,2}的子集为:∅,{0},{1},{2},{0,1},{0,2},{1,2},{0,1,2},其中含有偶数的集合有6个.答案:A 二、填空题5.已知集合A ={x |a -1≤x ≤a +2},B ={x |3<x <5},则能使A ⊇B 成立的实数a 的取值范围是________.解析:∵A ⊇B ,∴⎩⎪⎨⎪⎧a -1≤3,a +2≥5,∴3≤a ≤4. 答案:3≤a ≤46.设a ,b ∈R ,集合{0,ba,b }={1,a +b ,a },则b -a =________.解析:由题意可知a ≠0,则a +b =0,a =-b ,所以ba =-1,则a =-1,b =1,故b -a =2.答案:27.下列关系中正确的是________.①∅∈{0}; ②∅{0}; ③{0,1}⊆{(0,1)}; ④{(a ,b )}={(b ,a )}.解析:∵∅{0},∴①错误;空集是任何非空集合的真子集,②正确,{(0,1)}是含有一个元素的点集,③错误;{(a ,b )}与{(b ,a )}是两个不等的点集,④错误,故正确的是②.答案:②8.已知集合P ={1,2},那么满足Q ⊆P 的集合的个数是________. 解析:∵P ={1,2},Q ⊆P ,∴集合Q 可以是∅或{1}或{2}或{1,2}. 答案:4 三、解答题9.由“2,a ,b ”三个元素构成的集合与由“2a,2,b 2”三个元素构成的集合是同一个集合,求a ,b 的值. 解:根据集合相等,有⎩⎪⎨⎪⎧ a =2a ,b =b 2或⎩⎪⎨⎪⎧a =b 2,b =2a ,解得⎩⎪⎨⎪⎧ a =0,b =1或⎩⎪⎨⎪⎧a =0,b =0或⎩⎨⎧a =14,b =12.再根据集合元素的互异性,得⎩⎪⎨⎪⎧a =0,b =1或⎩⎨⎧a =14,b =12.10.设集合A ={x |x 2-5x +6=0},B ={x |x 2-(2a +1)x +a 2+a =0},若B ⊆A ,求a 的值.解:法一:A ={x |x 2-5x +6=0}={2,3},由B ⊆A 得,B =∅,或B ={2},或B ={3},或B ={2,3},由于Δ=(2a +1)2-4a 2-4a =1>0,∴B ≠∅,且B 含有两个不同元素.∴B ={2,3},需2a +1=5和a 2+a =6同时成立, ∴a =2.综上所述:a =2.法二:A={x|x2-5x+6=0}={2,3},B={x|x2-(2a+1)x+a2+a=0}={x|(x-a)·(x-a-1)=0}={a,a+1},∵a≠a+1,∴当B⊆A时,只有a=2且a+1=3.∴a=2.1.1.3集合的基本运算第一课时并集与交集[读教材·填要点]1.集合的并集与交集的定义21.若A={1,2,3},B={3,4,5},那么A∪B={1,2,3,3,4,5}对吗?如何表示A∪B和A∩B?提示:A∪B={1,2,3,3,4,5}是不对的,因为不符合元素的互异性;A∪B={1,2,3,4,5},A∩B={3}.2.你认为并集概念中的“或”与我们日常生活中“或”意义一致吗?有什么区别?提示:并集中的“或”与生活中“或”是不一样的.生活用语中的“或”是“或此”“或彼”只取其一,如“老师让张明或李红去开会”,意思是张明去也可以,李红去也可以,但不包括张明和李红一起去这种情况;而并集中的“或”则是“或此”“或彼”“或彼此”.3.若集合A与集合B没有公共元素,能否说集合A与集合B没有关系?提示:当两集合A与B没有公共元素时,不能说集合A与B没有关系,而是A∩B=∅.[例1] 已知集合A ={x |(x ∪B 是( ) A .{-1,2,3} B .{-1,-2,3} C .{1,-2,3}D .{1,-2,-3}[自主解答] A ={x |(x -1)(x +2)=0}={1,-2};B ={x |(x +2)(x -3)=0}={-2,3}, ∴A ∪B ={1,-2}∪{-2,3}={-2,1,3}. [答案] C ——————————————————解决此类问题首先应看清集合中元素的范围,简化集合,若是用列举法表示的数集,可以根据交集、并集的定义直接观察或用Venn 图表示出集合运算的结果;若是用描述法表示的数集,可借助数轴分析写出结果,此时要注意当端点不在集合中时,应用“空心点”表示.————————————————————————————————————————1.已知集合A ={x |-1<x ≤3},B ={x |x ≤0,或x ≥52},求A ∩B ,A ∪B .解:∵A ={x |-1<x ≤3},B ={x |x ≤0,或x ≥52},把集合A 与B 表示在数轴上,如图. ∴A ∩B ={x |-1<x ≤3}∩{x |x ≤0或x ≥52}={x |-1<x ≤0或52≤x ≤3};A ∪B ={x |-1<x ≤3}∪{x |x ≤0或x ≥52}=R .[例2] 已知集合A =x 的值. [自主解答] ∵A ∪B ={1,3,x },A ={1,3,x },B ={1,x 2}, ∴A ∪B =A ,即B ⊆A , ∴x 2=3或x 2=x .①当x 2=3时,得x =±3.若x =3,则A ={1,3,3},B ={1,3},符合题意; 若x =-3,则A ={1,3,-3},B ={1,3},符合题意. ②当x 2=x 时,则x =0或x =1.若x =0,则A ={1,3,0},B ={1,0},符合题意; 若x =1,则A ={1,3,1},B ={1,1},不成立,舍去;综上可知,x =±3或x =0. ——————————————————(1)在利用集合的交集、并集性质解题时,常常会遇到A ∩B =A ,A ∪B =B 等这类问题,解答时常借助于交、并集的定义及上节学习的集合间的关系去分析,如A ∩B =A ⇔A ⊆B ,A ∪B =B ⇔A ⊆B 等,解答时应灵活处理.(2)对于含有参数的问题要分类讨论,同时要检验,利用好集合中元素的互异性. ————————————————————————————————————————2.已知集合A ={4,6},B ={2,m },A ∪B ={2,4,6},则m 的值为________. 解析:∵A ={4,6},B ={2,m }, 而A ∪B ={2,4,6}, ∴m =4或m =6. 答案:4或6(1) 若A ∩B =A ∪B ,求a 的值; (2)若∅A ∩B ,A ∩C =∅,求a 的值.[巧思] (1)A ∩B =A ∪B ⇔A =B ;(2)∅A ∩B ⇔A ∩B ≠∅. [妙解] 由已知,得B ={2,3},C ={2,-4}.(1)∵A ∩B =A ∪B ,∴A =B .于是2,3是一元二次方程x 2-ax +a 2-19=0的两个根,由根与系数之间的关系知:⎩⎪⎨⎪⎧2+3=a ,2×3=a 2-19解之得a =5.(2)由A ∩B ∅⇒A ∩B ≠∅,又A ∩C =∅,得3∈A,2∉A ,-4∉A . 由3∈A 得32-3a +a 2-19=0, 解得a =5或a =-2.当a =5时,A ={x |x 2-5x +6=0}={2,3},与2∉A 矛盾; 当a =-2时,A ={x |x 2+2x -15=0}={3,-5},符合题意. ∴a =-2.1.已知集合M ={1,2,3,4},N ={-2,2},下列结论成立的是( ) A .N ⊆M B .M ∪N =M C .M ∩N =ND .M ∩N ={2}解析:因为-2∉M ,可排除A ;M ∪N ={-2,1,2,3,4},可排除B ;M ∩N ={2}.答案:D2.设A={x∈N|1≤x≤10},B={x∈R|x2+x-6=0},则如图中阴影部分表示的集合为()A.{2} B.{3}C.{-3,2} D.{-2,3}解析:注意到集合A中的元素为自然数,因此易知A={1,2,3,4,5,6,7,8,9,10},而直接解集合B中的方程可知B={-3,2},因此阴影部分显然表示的是A∩B={2}.答案:A3.设集合M={x|-3≤x<7},N={x|2x+k≤0},若M∩N≠∅,则k的取值范围是()A.k≤3 B.k≥-3C.k>6 D.k≤6解析:因为N={x|2x+k≤0}={x|x≤-k2},且M∩N≠∅,所以-k2≥-3⇒k≤6.答案:D4.已知集合A={x|x是平行四边形},B={x|x是菱形},C={x|x是矩形},则A∩B∩C=________. 解析:∵A∩B={x|x是菱形}∴A∩B∩C={x|x是正方形}.答案:{x|x是正方形}5.已知集合M={0,1,2},N={x|x=2a,a∈M},则集合M∩N=________.解析:由M={0,1,2},知N={0,2,4},M∩N={0,2}.答案:{0,2}6.设集合A={a2,a+1,-3},B={a-3,2a-1,a2+1},A∩B={-3},求实数a.解:∵A∩B={-3},∴-3∈B.∵a2+1≠-3,∴①若a-3=-3,则a=0,此时A={0,1,-3},B={-3,-1,1},但由于A∩B={1,-3}与已知A∩B={-3}矛盾,∴a≠0.②若2a-1=-3,则a=-1,此时A={1,0,-3},B={-4,-3,2},A∩B={-3},综上可知a=-1.一、选择题1.已知集合A ={x |x ≥0},B ={x |-1≤x ≤2},则A ∪B =( ) A .{x |x ≥-1} B .{x |x ≤2} C .{x |0<x ≤2}D .{x |1≤x ≤2}解析:结合数轴得A ∪B ={x |x ≥-1}. 答案:A2.设集合M ={x |-3<x <2},N ={x |1≤x ≤3},则M ∩N =( ) A .{x |1≤x <2} B .{x |1≤x ≤2} C .{x |2<x ≤3}D .{x |2≤x ≤3} 解析:∵M ={x |-3<x <2}且N ={x |1≤x ≤3}, ∴M ∩N ={x |1≤x <2}. 答案:A3.设A ={x |-3≤x ≤3},B ={y |y =-x 2+t }.若A ∩B =∅,则实数t 的取值范围是( ) A .t <-3 B .t ≤-3 C .t >3D .t ≥3解析:B ={y |y ≤t },结合数轴可知t <-3. 答案:A4.已知集合A ={x |-2≤x ≤7},B ={x |m +1<x <2m -1},且B ≠∅,若A ∪B =A ,则( ) A .-3≤m ≤4 B .-3<m <4 C .2<m <4D .2<m ≤4解析:∵A ∪B =A ,∴B ⊆A .又B ≠∅, ∴⎩⎪⎨⎪⎧m +1≥-2,2m -1≤7m +1<2m -1即2<m ≤4.答案:D 二、填空题5.已知集合A ={1,2,4},B ={2,4,6},则A ∪B =________. 解析:集合A ,B 都是以列举法的形式给出,易得A ∪B ={1,2,4,6}. 答案:{1,2,4,6}6.已知集合A ={x |x ≥5},集合B ={x |x ≤m },且A ∩B ={x |5≤x ≤6},则实数m =________. 解析:用数轴表示集合A 、B 如图所示, 由于A ∩B ={x |5≤x ≤6}, 则m =6. 答案:67.已知集合A ={x |x ≤1},B ={x |x ≥a },且A ∪B =R ,则实数a 的取值范围是________. 解析:如图所示,若A ∪B =R ,则a ≤1. 答案:a ≤18.已知集合A ={(x ,y )|y =ax +3},B ={(x ,y )|y =3x +b },A ∩B ={(2,5)},则a =________,b =________. 解析:∵A ∩B ={(2,5)}. ∴5=2a +3.∴a =1. ∴5=6+b .∴b =-1. 答案:1 -1 三、解答题9.已知集合A ={x |-1≤x <3},B ={x |2x -4≥x -2}. (1)求A ∩B ;(2)若集合C ={x |2x +a >0},满足B ∪C =C ,求实数a 的取值范围. 解:(1)∵B ={x |x ≥2},A ={x |-1≤x <3}, ∴A ∩B ={x |2≤x <3}.(2)∵C ={x |x >-a2},B ∪C =C ⇔B ⊆C ,∴a >-4.10.已知集合A =⎩⎨⎧⎭⎬⎫x ⎪⎪⎪⎩⎪⎨⎪⎧ 3-x >0,3x +6>0,集合B ={m |3>2m -1},求A ∩B ,A ∪B . 解:解不等式组⎩⎪⎨⎪⎧3-x >0,3x +6>0,得-2<x <3,则A ={x |-2<x <3},解不等式3>2m -1,得m <2,则B ={m |m <2}. 用数轴表示集合A 和B ,如图所示, 则A ∩B ={x |-2<x <2},A ∪B ={x |x <3}.第二课时 补集及集合运算综合问题[读教材·填要点]1.全集(1)定义:如果一个集合含有我们所研究问题中涉及的所有元素,那么称这个集合为全集. (2)符号表示:通常记作U . 2.补集1.已知集合A、∁U A(U为全集),则A∩(∁U A)与A∪(∁U A)各有什么特点?提示:A∩(∁U A)=∅,A∪(∁U A)=U.2.设U为全集,则∁U∅、∁U U、∁U(∁U A)分别表示什么集合?提示:∁U∅=U,∁U U=∅.∁U(∁U A)=A.3.判断∁U(A∩B)=(∁U A)∩∁U B,∁U(A∪B)=(∁U A)∪(∁U B)是否正确.提示:不对.结合韦恩图可知∁U(A∩B)=(∁U A)∪(∁U B)∁U(A∪B)=(∁U A)∩(∁U B).[例1]设全集U={0,1,2,3}U m的值.[自主解答]如图,∵U={0,1,2,3},∁U A={1,2},∴A={0,3}.∴方程x2+mx=0的两根为x1=0,x2=3,∴0+3=-m.即m=-3.——————————————————(1)根据补集定义,借助Venn图,可直观地求出全集,此类问题,当集合中元素离散时,可借助V enn图;当集合中元素连续时,可借助数轴,利用数轴分析法求解.(2)解题时要注意使用补集的几个性质:∁U U=∅,∁U∅=U,A∪(∁U A)=U. ————————————————————————————————————————1.已知全集U,集合A={1,3,5,7,9},∁U A={2,4,6,8},∁U B={1,4,6,8,9},求集合B.解:借助Venn,如右图所示,得U={1,2,3,4,5,6,7,8,9},∵∁U B={1,4,6,8,9},∴B={2,3,5,7}.[例2]设U={x∈N|x(∁U A)∩(∁U B),(∁U A)∪(∁U B).[自主解答]∵U={x∈N|x<10}={0,1,2,3,4,5,6,7,8,9},A={1,5,7,8},B={3,4,5,6,9},∴A∩B={1,5,7,8}∩{3,4,5,6,9}={5},A ∪B ={1,5,7,8}∪{3,4,5,6,9}={1,3,4,5,6,7,8,9}. ∵∁U A ={0,2,3,4,6,9},∁U B ={0,1,2,7,8},∴(∁U A )∩(∁U B )={0,2},(∁U A )∪(∁U B )={0,1,2,3,4,6,7,8,9}. ——————————————————1.解决集合的混合运算时,一般先运算括号内的部分,如求∁U (A ∪B )时,先求出A ∪B ,再求补集.2.当集合是用列举法表示时,如数集,可以通过列举集合的元素分别得到所求的集合;当集合是用描述法表示时,如不等式形式表示的集合,则可借助数轴求解.————————————————————————————————————————2.已知U =R ,A ={x |x >0},B ={x |x ≤-1},则[A ∩(∁U B )]∪[B ∩(∁U A )]=( ) A .∅ B .{x |x ≤0}C .{x |x >-1}D .{x |x >0,或x ≤-1}解析:∵B ={x |x ≤-1},∴∁U B ={x |x >-1}. 又∵A ={x |x >0},∴A ∩(∁U B )={x |x >0}. 又∵∁U A ={x |x ≤0}. ∴B ∩(∁U A )={x |x ≤-1}.∴[A ∩(∁U B )]∪[B ∩(∁U A )]={x |x >0,或x ≤-1}. 答案:D[例3] 设全集U =R ,U a 的取值范围. [自主解答]∁U P ={x |x <-2或x >1}, ∵M ∁U P ,∴分M =∅,M ≠∅,两种情况讨论. (1)M ≠∅时,如图可得或⎩⎪⎨⎪⎧3a <2a +5,3a ≥1,∴a ≤-72,或13≤a <5.(2)M =∅时,应有3a ≥2a +5⇒a ≥5. 综上可知,a ≤-72,或a ≥13.——————————————————1.M⊆N,一般分两种情况讨论:①M=∅,②M≠∅.2.解用不等式表示的数集间的集合运算时,一般要借助于数轴求解,此法的特点是简单直观,同时要注意各个端点的画法. ————————————————————————————————————————3.已知集合A={x|-4≤x≤-2},集合B={x|x-a≥0}.(1)若A⊆B,求a的取值范围;(2)若全集U=R,且A⊆(∁U B),求a的取值范围.解:∵A={x|-4≤x≤-2},B={x|x≥a},(1)由A⊆B,结合数轴(如图所示)可知a的范围为a≤-4.(2)∵U=R,∴∁U B={x|x<a},要使A⊆∁U B,须a>-2.动但不喜爱乒乓球运动的人数为________.[巧思]先将文字语言转化为集合语言,设U为全班学生组成的集合,A、B分别表示喜爱篮球运动的学生组成的集合、喜爱乒乓球运动的学生组成的集合,再利用Venn图可直观得出答案.[妙解]设全集U={全班30名学生},A={喜爱篮球运动的学生},B={喜爱乒乓球运动的学生},画出Venn图如图所示.设既喜欢篮球运动又喜欢乒乓球运动的人数为x,则(15-x)+x+(10-x)=30-8,解得x=3,所以喜爱篮球运动但不喜爱乒乓球运动的人数为12.[答案]121.设全集为R,A={x|x<3,或x>5},B={x|-3<x<3},则()A.∁R(A∪B)=R B.A∪(∁R B)=RC.(∁R A)∪(∁R B)=R D.A∪B=R解析:∵∁R A={x|3≤x≤5},∁R B={x|x≤-3,或x≥3},逐个验证知B正确.答案:B2.(2013·临沂一模)已知全集U=Z,集合A={0,1},B={-1,0,1,2},则图中阴影部分所表示的集合为()A.{-1,2} B.{-1,0}C.{0,1} D.{1,2}解析:图中阴影部分表示的集合为(∁U A)∩B,因为A={0,1},B={-1,0,1,2},所以(∁U A)∩B={-1,2}.答案:A3.已知全集U={0,1,2,3,4,5,6,7,8,9},集合A={0,1,3,5,8},集合B={2,4,5,6,8},则(∁U A)∩(∁U B)=() A.{5,8} B.{7,9}C.{0,1,3} D.{2,4,6}解析:因为A∪B={0,1,2,3,4,5,6,8},所以(∁U A)∩(∁U B)=∁U(A∪B)={7,9}.答案:B4.已知全集U={2,3,a2-a-1},A={2,3},若∁U A={1},则实数a的值是________.解析:∵U={2,3,a2-a-1},A={2,3},∁U A={1},∴a2-a-1=1,即a2-a-2=0,∴a=-1或a=2.答案:-1或25.已知集合A={x|0≤x≤5},B={x|2≤x<5},则∁A B=________.解析:如图:由数轴可知:∁A B={x|0≤x<2,或x=5}.答案:{x|0≤x<2,或x=5}6.设全集U={x|0<x<10,x∈N},若A∩B={3},A∩(∁U B)={1,5,7},(∁U A)∩(∁U B)={9},求集合A,B.解:U={1,2,3,4,5,6,7,8,9},由题意画出Venn图,∴A={1,3,5,7},B={2,3,4,6,8}.一、选择题1.设U=R,A={x|x>0},B={x|x>1},则A∩(∁U B)=()A.{x|0≤x<1} B.{x|0<x≤1}C.{x|x<0} D.{x|x>1}解析:画出数轴,如图所示,∁U B={x|x≤1},则A∩(∁U B)={x|0<x≤1}.答案:B2.已知全集U=A∪B中有m个元素,(∁U A)∪(∁U B)中有n个元素.若A∩B是非空集合,则A∩B的元素个数为()A.mn B.m+nC.n-m D.m-n解析:画出Venn图,如图.∵U=A∪B中有m个元素,(∁U A)∪(∁U B)=∁U(A∩B)中有n个元素,∴A∩B中有m-n个元素.答案:D3.已知集合A={x|x<a},B={x|x<2},且A∪(∁R B)=R,则a满足()A.a≥2 B.a>2C.a<2 D.a≤2解析:∁R B={x|x≥2},则由A∪(∁R B)=R得a≥2.答案:A4.设S为全集,则下列几种说法中,错误的个数是()①若A∩B=∅,则(∁S A)∪(∁S B)=S;②若A∪B=S,则(∁S A)∩(∁S B)=∅;③若A∪B=∅,则A=B.A.0 B.1C.2 D.3解析:①如图,(∁S A)∪(∁S B)=S,正确.②若A∪B=S,则(∁S A)∩(∁S B)=∁S(A∪B)=∅,故成立.③若A∪B=∅,则A=B=∅.答案:A二、填空题5.已知集合A={1,3,5,7,9},B={0,3,6,9,12},则A∩B=________,A∩(∁N B)=________.解析:因为集合A与集合B都有元素3和9,所以A∩B={3,9},结合Venn图(如图所示),易得A∩(∁N B)={1,5,7}.答案:{3,9}{1,5,7}6.设集合A={x|x+m≥0},B={x|-2<x<4},全集U=R,且(∁U A)∩B=∅,则实数m的取值范围是________.解析:∵A={x|x≥-m},∴∁U A={x|x<-m}.又∵(∁U A)∩B=∅,-m≤-2.∴m≥2.答案:m≥27.设全集U={a,b,c,d},集合A={a,b},B={b,c,d},则(∁U A)∪(∁U B)=________.解析:依题意得知,∁U A={c,d},∁U B={a},(∁U A)∪(∁U B)={a,c,d}.答案:{a,c,d}8.已知全集U(U≠∅)和集合A、B、D,且A=∁U B,B=∁U D,则A与D的关系是________.解析:A=∁U B=∁U(∁U D)=D.答案:A=D三、解答题9.已知全集U={x|-1≤x≤4},A={x|-1≤x≤1},B={x|0<x≤3},求∁U A,(∁U B)∩A.解:∵U={x|-1≤x≤4},A={x|-1≤x≤1},B={x|0<x≤3},结合数轴(如图).可知∁U A={x|1<x≤4},∁U B={x|3<x≤4,或-1≤x≤0}.结合数轴(如图).可知(∁U B)∩A={x|-1≤x≤0}.10.2011年8月世界大学生运动会在深圳举行,大运村的50名志愿者中,会讲英语的有36人,会讲日语的有20人,既会讲英语又会讲日语的有14人,问既不会讲英语又不会讲日语的有多少人?解:设全集U={50名志愿者},A={会讲英语的志愿者},B={会讲日语的志愿者},A∩B={既会讲英语又会讲日语的志愿者},画出Venn图,如图,则由Venn图知,既不会讲英语又不会讲日语的志愿者有50-22-14-6=8(人).1.2.1函数的概念[读教材·填要点]1.函数的概念(1)函数的定义:设A,B是非空的数集,如果按照某种确定的对应关系f,使对于集合A中的任意一个数x,在集合B中都有唯一确定的数f(x)和它对应,那么就称f:A→B为从集合A到集合B的一个函数,记作y=f(x),x∈A.(2)函数的定义域与值域:函数y=f(x)中,x叫自变量,x的取值范围叫做函数的定义域,与x的值相对应的y值叫做函数值,函数值的集合{f(x)|x∈A}叫做函数的值域.显然,值域是集合B的子集.2.区间概念(a,b为实数,且a<b)3.1.从函数的定义看,它的定义域和值域能否为空集?提示:因为定义中的A、B是非空数集,所以函数的定义域和值域都不能为空集.2.所有的数集都能用区间表示吗?提示:区间是数集的另一种表示方法,但并不是所有数集都能用区间表示,如{1,2,3,4}就不能用区间表示.3.如何用区间表示下列数集?(1){x|x≥1};(2){x|2<x≤3};(3){x|x>1且x≠2}.提示:(1)[1,+∞)(2)(2,3](3)(1,2)∪(2,+∞)[例1]设M={x|0≤x≤2}M到集合N的函数关系的有()A.0个B.1个C.2个D.3个[自主解答][答案] B——————————————————判断所给对应是否是函数,首先观察两个集合A、B是否是非空数集,其次验证对应关系下,集合A中数x 的任意性,集合B中数y的唯一性. ————————————————————————————————————————1.图中(1)(2)(3)(4)四个图象各表示两个变量x,y的对应关系,其中表示y是x的函数关系的有________.解析:由函数定义可知,任意作一条直线x=a,则与函数的图象至多有一个交点,对于本题而言,当-1≤a≤1时,直线x=a与函数的图象仅有一个交点,当a>1或a<-1时,直线x=a与函数的图象没有交点.从而表示y 是x的函数关系的有(2)(3).答案:(2)(3)[例2](1)f (x )=3x +2;(2)f (x )=3-x1-x -1.[自主解答] (1)使根式3x +2有意义的实数x 的集合是⎩⎨⎧⎭⎬⎫x ⎪⎪x ≥-23,从而函数f (x )=3x +2的定义域是⎩⎨⎧⎭⎬⎫x ⎪⎪x ≥-23.(2)要使3-x1-x -1有意义,只要⎩⎨⎧x -1≥0,3-x ≥0,x ≠2.因此函数f (x )=3-x1-x -1的定义域为{x |1≤x ≤3且x ≠2}. ——————————————————求函数定义域的方法及注意事项:(1)要明确使各函数表达式有意义的条件是什么,函数有意义的准则一般有:①分式的分母不为0;②偶次根式的被开方数非负;③y =x 0要求x ≠0.(2)当一个函数由两个或两个以上代数式的和、差、积、商的形式构成时,定义域是使得各式子都有意义的公共部分的集合.(3)定义域是一个集合,要用集合或区间表示,若用区间表示数集,不能用“或”连接,而应该用并集符号“∪”连接.————————————————————————————————————————2.求下列函数的定义域: (1)y =(x +1)0|x |-x ;(2)y =2x +3-12-x +1x. 解:(1)由⎩⎪⎨⎪⎧ x +1≠0,|x |-x ≠0得⎩⎪⎨⎪⎧x ≠-1,|x |≠x ,∴x <0且x ≠-1,∴原函数的定义域为{x |x <0且x ≠-1}.(2)要使函数有意义,需⎩⎨⎧2x +3≥0,2-x >0,x ≠0.解得-32≤x <2且x ≠0,所以函数y =2x +3-12-x+1x 的定义域为⎣⎡⎭⎫-32,0∪(0,2).[例3] (1)f (x )=(x )2,g (x )=x 2; (2)f (x )=x 2-2x -1,g (t )=t 2-2t -1.[自主解答] (1)由于函数f (x )=(x )2的定义域为{x |x ≥0},而g (x )=x 2的定义域为{x |x ∈R },它们的定义域不同,所以它们不表示同一函数.(2)两个函数的定义域和对应关系都相同,所以它们表示同一函数. ——————————————————判断两个函数f (x )和g (x )是否是相等函数的步骤是:①先求函数f (x )和g (x )的定义域,如果定义域不同,那么它们不相等,如果定义域相同,再执行下一步;②化简函数的解析式,如果化简后的函数解析式相同,那么它们相等,否则它们不相等.————————————————————————————————————————3.下列各组函数中,f (x )与g (x )表示同一函数的是( ) A .f (x )=x -1与g (x )=x 2-2x +1 B .f (x )=x 与g (x )=x 2xC .f (x )=x 与g (x )=3x 3 D .f (x )=x 2-4x -2与g (x )=x +2解析:A 选项中,f (x )与g (x )的对应关系不同,它们不表示同一函数;B ,D 选项中,f (x )与g (x )的定义域不同,它们不表示同一函数.答案:C求函数y =(x -2)(x +1)(x -2)(x +3)的定义域.[错解] 要使函数y =(x -2)(x +1)(x -2)(x +3)=x +1x +3有意义,则x ≠-3.故所求函数的定义域为{x |x ≠-3}.[错因] 约分扩大了自变量的取值范围.由于同时约去了函数中分子、分母的公因式“x -2”,使原函数变形为y =x +1x +3,从而改变了原函数的自变量x 的取值范围,也就是说,函数y =(x -2)(x +1)(x -2)(x +3)与函数y =x +1x +3不相等. [正解] 要使函数有意义,必须使(x -2)(x +3)≠0, 即x -2≠0且x +3≠0, 解得x ≠2且x ≠-3,。
最优化方法实验报告(2)
最优化方法实验报告Numerical Linear Algebra And ItsApplications学生所在学院:理学院学生所在班级:计算数学10-1学生姓名:甘纯指导教师:单锐教务处2013年5月实验三实验名称:无约束最优化方法的MATLAB实现实验时间: 2013年05月10日星期三实验成绩:一、实验目的:通过本次实验的学习,进一步熟悉掌握使用MATLAB软件,并能利用该软件进行无约束最优化方法的计算。
二、实验背景:(一)最速下降法1、算法原理最速下降法的搜索方向是目标函数的负梯度方向,最速下降法从目标函数的负梯度方向一直前进,直到到达目标函数的最低点。
2、算法步骤用最速下降法求无约束问题n R()min的算法步骤如下:xxf,a )给定初始点)0(x ,精度0>ε,并令k=0;b )计算搜索方向)()()(k k x f v -∇=,其中)()(k x f ∇表示函数)(x f 在点)(k x 处的梯度;c )若ε≤)(k v ,则停止计算;否则,从)(k x 出发,沿)(k v 进行一维搜索,即求k λ,使得)(min )()()(0)()(k k k k v x f v x f λλλ+=+≥; d )令1,)()()1(+=+=+k k v x x k k k k λ,转b )。
(二)牛顿法1、算法原理牛顿法是基于多元函数的泰勒展开而来的,它将)()]([-)(1)(2k k x f x f ∇∇-作为搜索方向,因此它的迭代公式可直接写出来:)()]([)(1)(2)()(k k k k x f x f x x ∇∇-=-2、算法步骤用牛顿法求无约束问题n R x x f ∈),(min 的算法步骤如下:a )给定初始点)0(x ,精度0>ε,并令k=0;b )若ε≤∇)()(k x f ,停止,极小点为)(k x ,否则转c );c )计算)()]([,)]([)(1)(2)(1)(2k k k k x f x f p x f ∇∇-=∇--令;d )令1,)()()1(+=+=+k k p x x k k k ,转b )。
机械优化设计上机报告
机械优化设计上机实践报告班级:机械(茅以升)101姓名 :学号 : 1004010510成绩 :指导教师 : 迎辉日期 : 2013.11.201 《一维搜索方法》上机实践报告1、写出所选择的一维搜索算法的基本过程、原理(可附流程图说明)。
(一)进退法 1. 算法原理进退法是用来确定搜索区间(包含极小值点的区间)的算法,其理论依据是:()f x 为单谷函数(只有一个极值点),且[,]a b 为其极小值点的一个搜索区间,对于任意12,[,]x x a b ∈,如果()()12f x f x <,则2[,]a x 为极小值的搜索区间,如果()()12f x f x >,则1[,]x b 为极小值的搜索区间。
因此,在给定初始点0x ,及初始搜索步长h 的情况下,首先以初始步长向前搜索一步,计算()0f x h +。
(1) 如果()()00f x f x h <+则可知搜索区间为0[,]x x h +%,其中x %待求,为确定x %,后退一步计算0()f x h λ-,λ为缩小系数,且01λ<<,直接找到合适的*λ,使得()*00()f x h f x λ->,从而确定搜索区间*00[,]x h x h λ-+。
(2) 如果()()00f x f x h >+则可知搜索区间为0[,]x x %,其中x %待求,为确定x %,前进一步计算0()f x h λ+,λ为放大系数,且1λ>,知道找到合适的*λ,使得()*00()f x h f x h λ+<+,从而确定搜索区间*00[,]x x h λ+。
2. 算法步骤用进退法求一维无约束问题min (),f x x R ∈的搜索区间(包含极小值点的区间)的基本算法步骤如下:(1) 给定初始点(0)x ,初始步长0h ,令0h h =,(1)(0)x x =,0k =; (2) 令(4)(1)x x h =+,置1k k =+;(3) 若()()(4)(1)f x f x <,则转步骤(4),否则转步骤(5);(4) 令(2)(1)(1)(4),x x x x ==,()()(2)(1)f x f x =,()()(1)(4)f x f x =,令2h h =,转步骤(2); (5) 若1k =,则转步骤(6)否则转步骤(7);(6) 令h h =-,(2)(4)x x =,()()(2)(4)f x f x =,转步骤(2);(7) 令(3)(2)(2)(1)(1)(4),,x x x x x x ===,停止计算,极小值点包含于区间(1)(3)(3)(1)[,][,]x x x x 或(二)黄金分割法1、黄金分割法基本思路:黄金分割法适用于[a ,b]区间上的任何单股函数求极小值问题,对函数除要求“单谷”外不做其他要求,甚至可以不连续。
最优化方法实验报告(1)
最优化方法实验报告(1)最优化方法实验报告Numerical Linear Algebra And Its Applications学生所在学院:理学院学生所在班级:计算数学10-1学生姓名:甘纯指导教师:单锐教务处2013年5月实验一实验名称:熟悉matlab基本功能实验时间: 2013年05月10日星期三实验成绩:一、实验目的:在本次实验中,通过亲临使用MATLAB,对该软件做一全面了解并掌握重点内容。
二、实验内容:1. 全面了解MATLAB系统2. 实验常用工具的具体操作和功能实验二实验名称:一维搜索方法的MATLAB实现实验时间: 2013年05月10日星期三实验成绩:一、实验目的:通过上机利用Matlab数学软件进行一维搜索,并学会对具体问题进行分析。
并且熟悉Matlab软件的实用方法,并且做到学习与使用并存,增加学习的实际动手性,不再让学习局限于书本和纸上,而是利用计算机学习来增加我们的学习兴趣。
二、实验背景:(一)0.618法(黄金分割法),它是一种基于区间收缩的极小点搜索算法,当用进退法确定搜索区间后,我们只知道极小点包含于搜索区间内,但是具体哪个点,无法得知。
1、算法原理黄金分割法的思想很直接,既然极小点包含于搜索区间内,那么可以不断的缩小搜索区间,就可以使搜索区间的端点逼近到极小点。
2、算法步骤用黄金分割法求无约束问题min (),f x x R ∈的基本步骤如下:(1)选定初始区间11[,]a b 及精度0ε>,计算试探点:11110.382*()a b a λ=+-11110.618*()a b a μ=+-。
(2)若k k b a ε-<,则停止计算。
否则当()()k k f f λμ>时转步骤(3)。
当()()k k f f λμ≤转步骤(4)。
(3)置11111110.382*()k kk k k k k k k k a b b a b a λλμμ+++++++=??=??=??=+-?转步骤(5)(4)置11111110.382*()k k k k k k k k k k a a b a b a μμλλ+++++++=??=??=??=+-?转步骤(5)(5)令1k k =+,转步骤(2)。
大连理工大学优化方法上机作业
大连理工大学优化方法上机作业-标准化文件发布号:(9456-EUATWK-MWUB-WUNN-INNUL-DDQTY-KII优化方法上机大作业学院:电子信息与电气工程学部姓名:学号:指导老师:上机大作业(一)%目标函数function f=fun(x)f=100*(x(2)-x(1)^2)^2+(1-x(1))^2;end%目标函数梯度function gf=gfun(x)gf=[-400*x(1)*(x(2)-x(1)^2)-2*(1-x(1));200*(x(2)-x(1)^2)]; End%目标函数Hess矩阵function He=Hess(x)He=[1200*x(1)^2-400*x(2)+2,-400*x(1);-400*x(1), 200;];end%线搜索步长function mk=armijo(xk,dk)beta=0.5; sigma=0.2;m=0; maxm=20;while (m<=maxm)if(fun(xk+beta^m*dk)<=fun(xk)+sigma*beta^m*gfun(xk)'*dk) mk=m; break;endm=m+1;endalpha=beta^mknewxk=xk+alpha*dkfk=fun(xk)newfk=fun(newxk)%最速下降法function [k,x,val]=grad(fun,gfun,x0,epsilon)%功能:梯度法求解无约束优化问题:minf(x)%输入:fun,gfun分别是目标函数及其梯度,x0是初始点,% epsilon为容许误差%输出:k是迭代次数,x,val分别是近似最优点和最优值maxk=5000; %最大迭代次数beta=0.5; sigma=0.4;k=0;while(k<maxk)gk=feval(gfun,x0); %计算梯度dk=-gk; %计算搜索方向if(norm(gk)<epsilon), break;end%检验终止准则m=0;mk=0;while(m<20) %用Armijo搜索步长if(feval(fun,x0+beta^m*dk)<=feval(fun,x0)+sigma*beta^m*gk'*dk) mk=m;break;endm=m+1;endx0=x0+beta^mk*dk;k=k+1;endx=x0;val=feval(fun,x0);>> x0=[0;0];>> [k,x,val]=grad('fun','gfun',x0,1e-4)迭代次数:k =1033x =0.99990.9998val =1.2390e-008%牛顿法x0=[0;0];ep=1e-4;maxk=10;k=0;while(k<maxk)gk=gfun(x0);if(norm(gk)<ep)x=x0miny=fun(x)k0=kbreak;elseH=inv(Hess(x0));x0=x0-H*gk;k=k+1;endendx =1.00001.0000miny =4.9304e-030迭代次数k0 =2%BFGS方法function [k,x,val]=bfgs(fun,gfun,x0,varargin) %功能:梯度法求解无约束优化问题:minf(x)%输入:fun,gfun分别是目标函数及其梯度,x0是初始点,% epsilon为容许误差%输出:k是迭代次数,x,val分别是近似最优点和最优值N=1000;epsilon=1e-4;beta=0.55;sigma=0.4;n=length(x0);Bk=eye(n);k=0;while(k<N)gk=feval(gfun,x0,varargin{:});if(norm(gk)<epsilon), break;enddk=-Bk\gk;m=0;mk=0;while(m<20)newf=feval(fun,x0+beta^m*dk,varargin{:});oldf=feval(fun,x0,varargin{:});if(newf<=oldf+sigma*beta^m*gk'*dk)mk=m;break;endm=m+1;endx=x0+beta^mk*dk;sk=x-x0;yk=feval(gfun,x,varargin{:})-gk;if(yk'*sk>0)Bk=Bk-(Bk*sk*sk'*Bk)/(sk'*Bk*sk)+(yk*yk')/(yk'*sk);endk=k+1;x0=x;endval=feval(fun,x0,varargin{:});>> x0=[0;0];>> [k,x,val]=bfgs('fun','gfun',x0)k =20x =1.00001.0000val =2.2005e-011%共轭梯度法function [k,x,val]=frcg(fun,gfun,x0,epsilon,N)if nargin<5,N=1000;endif nargin<4, epsilon=1e-4;endbeta=0.6;sigma=0.4;n=length(x0);k=0;while(k<N)gk=feval(gfun,x0);itern=k-(n+1)*floor(k/(n+1));itern=itern+1;if(itern==1)dk=-gk;elsebetak=(gk'*gk)/(g0'*g0);dk=-gk+betak*d0; gd=gk'*dk;if(gd>=0),dk=-gk;endendif(norm(gk)<epsilon),break;endm=0;mk=0;while(m<20)if(feval(fun,x0+beta^m*dk)<=feval(fun,x0)+sigma*beta^m*gk'*dk) mk=m;break;endm=m+1;endx=x0+beta^m*dk;g0=gk; d0=dk;x0=x;k=k+1;endval=feval(fun,x);>> x0=[0;0];[k,x,val]=frcg('fun','gfun',x0,1e-4,1000)k =122x =1.00011.0002val =7.2372e-009上机大作业(二)%目标函数function f_x=fun(x)f_x=4*x(1)-x(2)^2-12;%等式约束条件function he=hf(x)he=25-x(1)^2-x(2)^2;end%不等式约束条件function gi_x=gi(x,i)switch icase 1gi_x=10*x(1)-x(1)^2+10*x(2)-x(2)^2-34;case 2gi_x=x(1);case 3gi_x=x(2);otherwiseend%求目标函数的梯度function L_grad=grad(x,lambda,cigma)d_f=[4;2*x(2)];d_g(:,1)=[-2*x(1);-2*x(2)];d_g(:,2)=[10-2*x(1);10-2*x(2)];d_g(:,3)=[1;0];d_g(:,4)=[0;1];L_grad=d_f+(lambda(1)+cigma*hf(x))*d_g(:,1);for i=1:3if lambda(i+1)+cigma*gi(x,i)<0L_grad=L_grad+(lambda(i+1)+cigma*gi(x,i))*d_g(:,i+1);continueendend%增广拉格朗日函数function LA=lag(x,lambda,cee)LA=fun(x)+lambda(1)*hf(x)+0.5*cee*hf(x)^2;for i=1:3LA=LA+1/(2*cee)*(min(0,lambda(i+1)+cee*gi(x,i))^2-lambda(i+1)^2); endfunction xk=BFGS(x0,eps,lambda,cigma)gk=grad(x0,lambda,cigma);res_B=norm(gk);k_B=0;a_=1e-4;rho=0.5;c=1e-4;length_x=length(x0);I=eye(length_x);Hk=I;while res_B>eps&&k_B<=10000dk=-Hk*gk;m=0;while m<=5000if lag(x0+a_*rho^m*dk,lambda,cigma)-lag(x0,lambda,cigma)<=c*a_*rho^m*gk'*dkmk=m;break;endm=m+1;endak=a_*rho^mk;xk=x0+ak*dk;delta=xk-x0;y=grad(xk,lambda,cigma)-gk;Hk=(I-(delta*y')/(delta'*y))*Hk*(I-(y*delta')/(delta'*y))+(delta*delta')/(delta'*y);k_B=k_B+1;x0=xk;gk=y+gk;res_B=norm(gk);end%增广拉格朗日法function val_min=ALM(x0,eps)lambda=zeros(4,1);cigma=5;alpha=10;k=1;res=[abs(hf(x0)),0,0,0];for i=1:3res(1,i+1)=norm(min(gi(x0,i),-lambda(i+1)/cigma)); endres=max(res);while res>eps&&k<1000xk=BFGS(x0,eps,lambda,cigma);lambda(1)=lambda(1)+cigma*hf(xk);for i=1:3lambda(i+1)=lambda(i+1)+min(0,lambda(i+1)+gi(x0,1)); endk=k+1;cigma=alpha*cigma;x0=xk;res=[norm(hf(x0)),0,0,0];for i=1:3res(1,i+1)=norm(min(gi(x0,i),-lambda(i+1)/cigma)); endres=max(res);endval_min=fun(xk);fprintf('k=%d\n',k);fprintf('fmin=%.4f\n',val_min);fprintf('x=[%.4f;%.4f]\n',xk(1),xk(2));>> x0=[0;0];>> val_min=ALM(x0,1e-4)k=10fmin=-31.4003x=[1.0984;4.8779]val_min =-31.4003上机大作业(三)A=[1 1;-1 0;0 -1];n=2;b=[1;0;0];G=[0.5 0;0 2];c=[2 4];cvx_solver sdpt3cvx_beginvariable x(n)minimize (x'*G*x-c*x)subject toA*x<=bcvx_enddisp(x)Status: SolvedOptimal value (cvx_optval): -2.40.40000.6000A=[2 1 1;1 2 3;2 2 1;-1 0 0;0 -1 0;0 0 -1]; n=3;b=[2;5;6;0;0;0];C=[-3 -1 -3];cvx_solver sdpt3cvx_beginvariable x(n)minimize (C*x)subject toA*x<=bcvx_enddisp(x)Status: SolvedOptimal value (cvx_optval): -5.40.20000.00001.600011。
优化方法课程大作业
优化方法课程上机大作业学部:电子信息与电气工程学部专业:生物医学工程班级:电信硕1303学号:21309210姓名:史益新大连理工大学Dalian University of Technology解:(1)MATLAB代码如下:clc;clear all;close all;[x,y]=meshgrid(-2:0.1:2,-1:0.1:3);z=(y-x.^2).^2+(1-x).^2;mesh(x,y,z)hold on;xk=[0 1]';epsilon=1e-5;plot(xk(1),xk(2),'ro');text(xk(1),xk(2),'start point');hold on;[ x,val,k ]=Newton('fun','gfun','Hess',xk,epsilon)plot(x(1),x(2),'ro');text(x(1),x(2),'end point');function [ x,val,k ] = Newton( fun,gfun,Hess,xk,epsilon ) k=0;while(1)gk=feval(gfun,xk);hk=feval(Hess,xk);sk=-inv(hk)*gk;if(norm(gk)<epsilon)break;endxk=xk+sk;k=k+1;endx=xk;val=feval(fun,x);endfunction f = fun(x)f=(x(2)-x(1)^2)^2+(1-x(1))^2;endfunction g=gfun(x)g=[-4*x(1)*(x(2)-x(1)^2)+2*(1-x(1)),2*(x(2)-x(1)^2)]'; endfunction He = Hess( x )n=length(x);He=zeros(n,n);He=[12*x(1)^2-4*x(2)+2,-4*x(1);-4*x(1), 2 ];end(2)代码运行结果如下:解:(1)MATLAB代码如下:clc;clear all;close all;x0=[0 0 0 0]';epsilon=1e-5;[ x,val,k ]=Frcg('fun','gfun',x0,epsilon)function [ x,val,k ] = Frcg( fun,gfun,x0,epsilon )rho=0.6;sigma=0.5;k=0;n=length(x0);while(1)g=feval(gfun,x0);itern=k-(n+1)*floor(k/(n+1));itern=itern+1;if(itern==1)d=-g;elsebeta=(g'*g)/(g0'*g0);d=-g+beta*d0;gd=g'*d;if(gd>=0.0)d=-g;endendif(norm(g)<epsilon)break;endm=0;mk=0;while(m<20)if(feval(fun,x0+rho^m*d)<feval(fun,x0)+sigma*rho^m*g'*d) mk=m;break;endm=m+1;endx0=x0+rho^mk*d;val=feval(fun,x0);g0=g;d0=d;k=k+1;endx=x0;val=feval(fun,x);function f = fun( x )f=x(1)^2-2*x(1)*x(2)+2*x(2)^2+x(3)^2+x(4)^2-x(2)*x(3)+2*x(1)+3*x(2)-x(3); endfunction g = gfun( x )g=[2*x(1)-2*x(2)+2,-2*x(1)+4*x(2)-x(3)+3,2*x(3)-x(2)-1,2*x(4)]';end(2)代码运行结果如下:解:(1)MATLAB代码如下:clc;clear all;close all;[x,y]=meshgrid(-2:0.1:2,-1:0.1:3);z=5*(y-x.^2).^2+(x-1).^2;mesh(x,y,z)hold on;x0=[2 0]';epsilon=1e-5;plot(x0(1),x0(2),'ro');text(x0(1),x0(2),'start point');hold on;[ x1,val1,k1 ]=grad('fun','gfun',x0,epsilon)plot(x1(1),x1(2),'ro');text(x1(1),x1(2),'end point1');hold on;[ x2,val2,k2 ]=znNewton('fun','gfun','Hess',x0,epsilon) plot(x2(1),x2(2),'bo');text(x2(1),x2(2),'end point2');hold on;[ x3,val3,k3 ]=BFGS('fun','gfun',x0,epsilon)plot(x3(1),x3(2),'go');text(x3(1),x3(2),'end point3');hold on;function [ x,val,k ] = grad( fun,gfun,x0,epsilon )rho=0.5;sigma=0.4;k=0;while(1)g=feval(gfun,x0);d=-g;if(norm(d)<epsilon)break;endm=0;mk=0;while(m<20)if(feval(fun,x0+rho^m*d)<feval(fun,x0)+sigma*rho^m*g'*d) mk=m;break;endm=m+1;endx0=x0+rho^mk*d;k=k+1;endx=x0;val=feval(fun,x0);endfunction [ x,val,k ] = znNewton( fun,gfun,Hess,x0,epsilon )rho=0.5;sigma=0.4;k=0;while(1)gk=feval(gfun,x0);hk=feval(Hess,x0);dk=-inv(hk)*gk;if(norm(gk)<epsilon)break;endm=0;mk=0;while(m<20)if(feval(fun,x0+rho^m*dk)<feval(fun,x0)+sigma*rho^m*gk'*dk) mk=m;break;endm=m+1;endx0=x0+rho^mk*dk;k=k+1;endx=x0;val=feval(fun,x0);endfunction [ x,val,k ] = BFGS( fun,gfun,x0,epsilon )rho=0.5;sigma=0.4;k=0;n=length(x0);bk=eye(n);while(1)gk=feval(gfun,x0);if(norm(gk)<epsilon)break;enddk=-inv(bk)*gk;m=0;mk=0;while(m<20)newf=feval(fun,x0+rho^m*dk);oldf=feval(fun,x0);if(newf<oldf+sigma*rho^m*gk'*dk)mk=m;break;endm=m+1;endx=x0+rho^mk*dk;sk=x-x0;yk=feval(gfun,x)-gk;if(yk'*sk>0)bk=bk-(bk*sk*sk'*bk)/(sk'*bk*sk)+(yk*yk')/(yk'*sk);endk=k+1;x0=x;endval=feval(fun,x0);endfunction f = fun(x)f=5*(x(2)-x(1)^2)^2+(x(1)-1)^2;endfunction g=gfun(x)g=[-20*x(1)*(x(2)-x(1)^2)+2*(x(1)-1),10*(x(2)-x(1)^2)]'; endfunction He = Hess( x )n=length(x);He=zeros(n,n);He=[60*x(1)^2-20*x(2)+2,-20*x(1);-20*x(1), 10 ];end(2)代码运行结果如下:解:(1)MATLAB程序如下:clc;close all;clear all;x0=[1,0]';epsilon=1e-5;[ x,mu,lambda,output ] = multphr( 'f1','h1','g1','df1','dh1','dg1',x0,epsilon )function [ x,mu,lambda,output ] = multphr( fun,hf,gf,dfun,dhf,dgf,x0,epsilon )%MULTPHR Summary of this function goes here% Detailed explanation goes heresigma=2.0;eta=2.0;theta=0.8;k=0;ink=0;x=x0;he=feval(hf,x);gi=feval(gf,x);n=length(x);l=length(he);m=length(gi);%initial of multi-vectormu=0.1*ones(l,1);lambda=0.1*ones(m,1);btak=10;btaold=10;while(btak>epsilon)[x,ival,ik]=BFGS('mpsi','dmpsi',x0,epsilon,fun,hf,gf,dfun,dhf,dgf,mu,lambda,sigma);ink=ink+ik;he=feval(hf,x);gi=feval(gf,x);btak=0;for(i=1:l)btak=btak+he(i)^2;endfor(i=1:m)temp=min(gi(i),lambda(i)/sigma);btak=btak+temp^2;endbtak=sqrt(btak);if(btak>epsilon)if(k>=2 & btak>theta*btaold)sigma=eta*sigma;endfor(i=1:l)mu(i)=mu(i)-sigma*he(i);endfor(i=1:m)lambda(i)=max(0,lambda(i)-sigma*gi(i));endendk=k+1;btaold=btak;x0=x;endf=feval(fun,x);output.fval=f;output.iter=k;output.inner_iter=ink;output.bta=btak;endfunction [ x,val,k ] = BFGS( fun,gfun,x0,varargin )rho=0.5;epsilon=1e-5;sigma=0.4;k=0;n=length(x0);bk=eye(n);while(1)gk=feval(gfun,x0,varargin{:});if(norm(gk)<epsilon)break;enddk=-inv(bk)*gk;m=0;mk=0;while(m<20)newf=feval(fun,x0+rho^m*dk,varargin{:});oldf=feval(fun,x0,varargin{:});if(newf<oldf+sigma*rho^m*gk'*dk)mk=m;break;endm=m+1;endx=x0+rho^mk*dk;sk=x-x0;yk=feval(gfun,x,varargin{:})-gk;if(yk'*sk>0)bk=bk-(bk*sk*sk'*bk)/(sk'*bk*sk)+(yk*yk')/(yk'*sk);endk=k+1;x0=x;endval=feval(fun,x0,varargin{:});endfunction psi = mpsi( x,epsilon,fun,hf,gf,dfun,dhf,dgf,mu,lambda,sigma ) %MPSI Summary of this function goes here% Detailed explanation goes heref=feval(fun,x);he=feval(hf,x);gi=feval(gf,x);l=length(he);m=length(gi);psi=f;s1=0;for(i=1:l)psi=psi-he(i)*mu(i);s1=s1+he(i)^2;endpsi=psi+0.5*sigma*s1;s2=0;for(i=1:m)s3=max(0,lambda(i)-sigma*gi(i));s2=s2+s3^2-lambda(i)^2;endpsi=psi+s2/(2*sigma);endfunction dpsi = dmpsi( x,epsilon,fun,hf,gf,dfun,dhf,dgf,mu,lambda,sigma ) %DMPSI Summary of this function goes here% Detailed explanation goes heredpsi=feval(dfun,x);he=feval(hf,x);gi=feval(gf,x);dhe=feval(dhf,x);dgi=feval(dgf,x);l=length(he);m=length(gi);for(i=1:l)dpsi=dpsi+(sigma*he(i)-mu(i))*dhe(:,i);endfor(i=1:m)dpsi=dpsi+(sigma*gi(i)-lambda(i))*dgi(:,i);endendfunction f = f1( x )f=4*x(1)-x(2)^2-12;endfunction gi = g1( x )gi=10*x(1)-x(1)^2+10*x(2)-x(2)^2-34;%unequation constrainendfunction he = h1( x )he=25-x(1)^2-x(2)^2;%equation constrainendfunction g = df1( x )g=[4,-2*x(2)]';endfunction dgi = dg1( x )dgi=[-2*x(1)+10,-2*x(2)+10]';endfunction dhe = dh1( x )dhe=[-2*x(1),-2*x(2)]';end(2)代码运行结果如下:解:(1)MATLAB代码如下:clc;close all;clear all;H=[2 0;0 2];c=[-2 -5]';Ae=[];be=[];Ai=[1 -2;-1 -2;-1 2;1 0;0 1];bi=[-2 -6 -2 0 0]';x0=[0 0]';epsilon=1e-9;[ x,lamk,exitflag,output ] = qpact( H,c,Ae,be,Ai,bi,x0,epsilon )function [ x,lamk,exitflag,output ] = qpact( H,c,Ae,be,Ai,bi,x0,epsilon ) %QPACT Summary of this function goes here% Detailed explanation goes hereerr=1e-6;k=0;x=x0;n=length(x);kmax=1e3;ne=length(be);ni=length(bi);lamk=zeros(ne+ni,1);index=ones(ni,1);for(i=1:ni)if(Ai(i,:)*x>bi(i)+epsilon)index(i)=0;endendwhile(k<=kmax)Aee=[];if(ne>0)Aee=Ae;endfor(j=1:ni)if(index(j)>0)Aee=[Aee;Ai(j,:)];endendgk=H*x+c;[m1,n1]=size(Aee);[dk,lamk]=qsubp(H,gk,Aee,zeros(m1,1));if(norm(dk)<=err)y=0;if(length(lamk)>ne)[y,jk]=min(lamk(ne+1:length(lamk)));endif(y>=0)exitflag=0;elseexitflag=1;for(i=1:ni)if(index(i)&(ne+sum(index(1:i)))==jk)index(i)=0;break;endendendk=k+1;elseexitflag=1;alpha=1;tm=1;for(i=1:ni)if((index(i)==0)&(Ai(i,:)*dk<0))tm1=(bi(i)-Ai(i,:)*x)/(Ai(i,:)*dk);if(tm1<tm)tm=tm1;ti=i;endendendalpha=min(alpha,tm);x=x+alpha*dk;if(tm<1)index(ti)=1;endendif(exitflag==0)break;end %updata the setk=k+1;endoutput.fval=0.5*x'*H*x+c'*x;output.iter=k;endfunction [ x,lambda ] = qsubp( H,c,Ae,be )%QSUBP Summary of this function goes here % Detailed explanation goes hereginvH=pinv(H);[m,n]=size(Ae);if(m>0)rb=Ae*ginvH*c+be;lambda=pinv(Ae*ginvH*Ae')*rb;x=ginvH*(Ae'*lambda-c);elsex=-ginvH*c;lambda=0;endend(2)代码运行结果如下:解:(1)MATLAB代码如下:clc;close all;clear all;x0=[0.5 0.2]';mu0=[ ]';lam0=[0 0 0 0]';epsilon=1e-6;[ x,mu,lam,val,k ] = sqpm( x0,mu0,lam0,epsilon)function [ x,mu,lam,val,k ] = sqpm( x0,mu0,lam0,epsilon ) %SQPM Summary of this function goes here% Detailed explanation goes heren=length(x0);l=length(mu0);m=length(lam0);rho=0.5;eta=0.1;B0=eye(n);x=x0;mu=mu0;lam=lam0;Bk=B0;sigma=0.8;[hk,gk]=cons(x);dfk=df1(x);[Ae,Ai]=dcons(x);Ak=[Ae;Ai];k=0;while(1)[dk,mu,lam]=qpsubp(dfk,Bk,Ae,hk,Ai,gk,epsilon);mp1=norm(hk,1)+norm(max(-gk,0),1);if (norm(dk,1)<epsilon) & (mp1<1e-5)break;enddeta=0.05;tau=max(norm(mu,inf),norm(lam,inf));if(sigma*(tau+deta)<1)sigma=sigma;elsesigma=1.0/(tau+2*deta);endim=0;while(im<=20)if(phi1(x+rho^im*dk,sigma)-phi1(x,sigma)<eta*rho^im*dphi1(x,sigma,dk)) mk=im;break;endim=im+1;if(im==20)mk=10;endendalpha=rho^mk;x1=x+alpha*dk;[hk,gk]=cons(x1);dfk=df1(x1);[Ae,Ai]=dcons(x1);Ak=[Ae;Ai];lamu=pinv(Ak)'*dfk;if(l>0 & m>0)mu=lamu(1:l);lam=lamu(l+1:l+m);endif(l==0)mu=[];lam=lamu;endif(m==0)mu=lamu;lam=[];endsk=alpha*dk;yk=dlax(x1,mu,lam)-dlax(x,mu,lam);if(sk'*yk>0.2*sk'*Bk*sk)theta=1;elsetheta=0.8*sk'*Bk*sk/(sk'*Bk*sk-sk'*yk);endzk=theta*yk+(1-theta)*Bk*sk;Bk=Bk+zk*zk'/(sk'*zk)-(Bk*sk)*(Bk*sk)'/(sk'*Bk*sk);x=x1;k=k+1;endval=f1(x);endfunction [ d,mu,lam,val,k ] = qpsubp( dfk,Bk,Ae,hk,Ai,gk,epsilon ) %QPSUBP Summary of this function goes here% Detailed explanation goes heren=length(dfk);l=length(hk);m=length(gk);gamma=0.05;rho=0.5;sigma=0.2;ep0=0.05;mu0=0.05*zeros(l,1);lam0=0.05*zeros(m,1);d0=ones(n,1);u0=[ep0;zeros(n+l+m,1)];z0=[ep0;d0;mu0;lam0,];k=0;z=z0;ep=ep0;d=d0;mu=mu0;lam=lam0;while(1)dh=dah(ep,d,mu,lam,dfk,Bk,Ae,hk,Ai,gk);if(norm(dh)<epsilon)break;endA=JacobiH(ep,d,mu,lam,dfk,Bk,Ae,hk,Ai,gk);b=beta(ep,d,mu,lam,dfk,Bk,Ae,hk,Ai,gk,gamma)*u0-dh;dz=pinv(A)*b;if(l>0 & m>0)de=dz(1);dd=dz(2:n+1);du=dz(n+2:n+l+1);dl=dz(n+l+2:n+l+m+1);endif(l==0)de=dz(1);dd=dz(2:n+1);dl=dz(n+2:n+m+1);endif(m==0)de=dz(1);dd=dz(2:n+1);du=dz(n+2:n+l+1);endi=0;while(i<=20)if(l>0 & m>0)dh1=dah(ep+rho^i*de,d+rho^i*dd,mu+rho^i*du,lam+rho^i*dl,dfk,Bk,Ae,hk,Ai,gk);endif(l==0)dh1=dah(ep+rho^i*de,d+rho^i*dd,mu,lam+rho^i*dl,dfk,Bk,Ae,hk,Ai,gk);endif(m==0)dh1=dah(ep+rho^i*de,d+rho^i*dd,mu+rho^i*du,lam,dfk,Bk,Ae,hk,Ai,gk);endif(norm(dh1)<=(1-sigma*(1-gamma*ep0)*rho^i)*norm(dh))mk=i;break;endi=i+1;if(i==20)mk=10;endendalpha=rho^mk;if(l>0 & m>0)ep=ep+alpha*de;d=d+alpha*dd;mu=mu+alpha*du;lam=lam+alpha*dl;endif(l==0)ep=ep+alpha*de;d=d+alpha*dd;lam=lam+alpha*dl;endif(m==0)ep=ep+alpha*de;d=d+alpha*dd;mu=mu+alpha*du;endk=k+1;endendfunction dh = dah( ep,d,mu,lam,dfk,Bk,Ae,hk,Ai,gk )%DAH Summary of this function goes here% Detailed explanation goes heren=length(dfk);l=length(hk);m=length(gk);dh=zeros(n+l+m+1,1);dh(1)=ep;if(l>0 & m>0)dh(2:n+1)=Bk*d-Ae'*mu-Ai'*lam+dfk;dh(n+2:n+l+1)=hk+Ae*d;for(i=1:m)dh(n+l+1+i)=phi(ep,lam(i),gk(i)+Ai(i,:)*d);endendif(l==0)dh(2:n+1)=Bk*d-Ai'*lam+dfk;for(i=1:m)dh(n+1+i)=phi(ep,lam(i),gk(i)+Ai(i,:)*d);endendif(m==0)dh(2:n+1)=Bk*d-Ae'*mu+dfk;dh(n+2:n+l+1)=hk+Ae*d;enddh=dh(:);endfunction bet = beta( ep,d,mu,lam,dfk,Bk,Ae,hk,Ai,gk,gamma )%BETA Summary of this function goes here% Detailed explanation goes heredh=dah(ep,d,mu,lam,dfk,Bk,Ae,hk,Ai,gk);bet=gamma*norm(dh)*min(1,norm(dh));endfunction [ h,g ] = cons( x )%CONS Summary of this function goes here% Detailed explanation goes hereh=[ ];g=[-x(1)^2+6*x(1)-4*x(2)+11,x(1)*x(2)-3*x(2)-exp(x(1)-1)+1,x(1),x(2)]'; endfunction [ dh,dg ] = dcons( x )%DCONS Summary of this function goes here% Detailed explanation goes heredh=[ ];dg=[-2*x(1)+6,-4;x(2)-exp(x(1)-1),x(1)-1;1,0;0,1];endfunction [ dd1,dd2,v1 ] = ddv( ep,d,lam,Ai,gk)%DDV Summary of this function goes here% Detailed explanation goes herem=length(gk);dd1=zeros(m,m);dd2=zeros(m,m);v1=zeros(m,1);for(i=1:m)fm=sqrt(lam(i)^2+(gk(i)+Ai(i,:)*d)^2+2*ep^2);dd1(i,i)=1-lam(i)/fm;dd2(i,i)=1-(gk(i)+Ai(i,:)*d)/fm;v1(i)=-2*ep/fm;endendfunction df = df1( x )%DF1 Summary of this function goes here% Detailed explanation goes heredf=[2*x(1)-16,2*x(2)-10]';endfunction dl = dlax( x,mu,lam )%DLAX Summary of this function goes here% Detailed explanation goes heredf=df1(x);[Ae,Ai]=dcons(x);[m1,m2]=size(Ai);[l1,l2]=size(Ae);if(l1==0)dl=df-Ai'*lam;endif(m1==0)dl=df-Ae'*mu;endif(l1>0 & m1>0)dl=df-Ae'*mu-Ai'*lam;endendfunction f = f1( x )%F1 Summary of this function goes here% Detailed explanation goes heref=x(1)^2+x(2)^2-16*x(1)-10*x(2);endfunction p = phi( ep,a,b )%PHI Summary of this function goes here% Detailed explanation goes herep=a+b-sqrt(a^2+b^2+2*ep^2);endfunction p = phi1( x,sigma )%PHI1 Summary of this function goes here% Detailed explanation goes heref=f1(x);[h,g]=cons(x);gn=max(-g,0);l0=length(h);m0=length(g);if(l0==0)p=f+1.0/sigma*norm(gn,1);endif(m0==0)p=f+1.0/sigma*norm(h,1);endif(l0>0 & m0>0)p=f+1.0/sigma*(norm(h,1)+norm(gn,1)); endendfunction dp = dphi1( x,sigma,d )%DPHI1 Summary of this function goes here% Detailed explanation goes heredf=df1(x);[h,g]=cons(x);gn=max(-g,0);l0=length(h);m0=length(g);if(l0==0)dp=df'*d-1.0/sigma*norm(gn,1);endif(m0==0)dp=df'*d-1.0/sigma*norm(h,1);endif(l0>0 & m0>0)dp=df'*d-1.0/sigma*(norm(h,1)+norm(gn,1)); endendfunction A = JacobiH( ep,d,mu,lam,dfk,Bk,Ae,hk,Ai,gk )%JACOBIH Summary of this function goes here% Detailed explanation goes heren=length(dfk);l=length(hk);m=length(gk);A=zeros(n+l+m+1,n+l+m+1);[dd1,dd2,v1]=ddv(ep,d,lam,Ai,gk);if(l>0 & m>0)A=[1, zeros(1,n), zeros(1,l), zeros(1,m);zeros(n,1), Bk, -Ae', -Ai';zeros(l,1), Ae, zeros(l,l), zeros(l,m);v1, dd2*Ai, zeros(m,l), dd1]; endif(l==0)A=[1, zeros(1,n), zeros(1,m);zeros(n,1), Bk, -Ai';v1, dd2*Ai, dd1];endif(m==0)A=[1, zeros(1,n), zeros(1,l);zeros(n,1), Bk, -Ae';zeros(l,1), Ae, zeros(l,l)];endend(2)代码运行结果如下:解:(1)MATLAB代码如下:clc;close all;clear all;f=[1 1 1 1 1 1 1 1];A=[1 0 0 0.5;0 1 0.2 0.3;0 0.1 1 0.2];Aeq=[A,-A];beq=[-1 0.2 1]';lb=zeros(8,1);x=linprog(f,[],[],Aeq,beq,lb);x=[x(1)-x(5) x(2)-x(6) x(3)-x(7) x(4)-x(8)]' (2)代码运行结果如下:。
汽车及发动机优化设计(2013实例部分)
5)质量要轻。
1)各个设计要求在传统的设计中 如何实现? 2)设计要求与功用的关系如何?
30/322
四、结构参数
圆柱形螺旋压缩弹簧的参数主要包括:钢丝直径d、弹簧外径
D2、弹簧中径D、工作圈数n、死圈数n2、弹簧的最大变形量λ和旋 绕比c。其中:n2 一般取决于气门弹簧在发动机上的安装情况,它 是预先确定的,即为常数;λ取决于弹簧的工作载荷、弹簧的布置 及配气机构的设计,在设计弹簧之前可以根据发动机的要求选定一
4) 后轮的加速度小于许用值
在现在的优化中是否还是 前、后轮上的相关约束条 件? 20/322
九、优化设计过程 车轮的动挠度小于许用值
f xq f
各种不同类型的汽车其动 挠度的许用值不同?
21/322
九、优化设计过程
优化模型
①设计变量
k x1 X x1 c x2
8/322
二、主要功用
1、传递作用在车轮和车架(或车身)之间的一切力和力矩; 2、缓和路面传给车架(或车身)的冲击载荷,衰减由此引起的车载系统的振动, 保证汽车的行驶平顺性; 3、保证车轮在路面不平和载荷变化时有理想的运动特性; 4、保证汽车的操纵稳定性,使汽车获得高速行驶能力。
汽车在不平路面上行驶,由于悬架的弹性作用,使汽车产生垂直振动。为了 迅速衰减这种振动和抑制车身、车轮的共振,减小车轮的振幅,悬架应装有减振 器,并使之具有合理的阻尼。利用减振器的阻尼作用。使汽车的振动振幅连续减 小,直至振动停止。
运动过程中的外界条 件。 能否定量的说明这些 边界条件?
14/322
七、失效形式或存在的不足
1)质心的加速度造成的舒适性问题; 2)动挠度过大造成的元件的寿命等问题; 3)车轮的加速度造成的操纵稳定性问题等。
fibonacci优化法 例题
文章标题:深入探讨Fibonacci优化法—理论与实践一、引言在计算机算法与数据结构领域,Fibonacci优化法是一种非常重要的优化算法。
本文将从深度和广度两个方面进行全面评估,并根据具体例题展开讨论,分析其优点和适用场景。
二、Fibonacci优化法的理论基础Fibonacci优化法源自Fibonacci数列,其数学表达式为:F(1) = F(2) = 1, F(n) = F(n-1) + F(n-2) (n > 2)。
在计算机算法中,Fibonacci优化法主要用于解决递归或动态规划中的重叠子问题,通过存储中间结果,避免重复计算,进而提高算法效率和性能。
三、Fibonacci优化法的实际应用我们以斐波那契数列求解为例。
假设有一个常见的斐波那契数列求解问题,代码如下所示:```pythondef fibonacci(n):if n <= 2:return 1else:return fibonacci(n-1) + fibonacci(n-2)```在传统的递归算法中,存在大量的重复计算,导致性能低下。
而通过Fibonacci优化法,我们可以使用一个数组来存储中间结果,避免重复计算,大大提高了算法的效率。
```pythondef fibonacci_optimized(n):memo = [0] * (n+1)return fib(n, memo)def fib(n, memo):if n <= 2:return 1if memo[n] != 0:return memo[n]memo[n] = fib(n-1, memo) + fib(n-2, memo)return memo[n]```通过Fibonacci优化法,我们能够显著减少重复计算,提高了算法的效率和性能,尤其在大规模的计算任务中效果更为明显。
四、对Fibonacci优化法的个人观点和理解Fibonacci优化法作为一种重要的优化算法,在实际应用中展现出了巨大的价值。
2013年的Hewlett-Packard安全事件优化技巧与方法说明书
AgendaIn this session, you will learn•How to enrich your security events in smart connectors by–Adding missing information from raw logs or from sources outside of the raw logs –Correcting event fields populated incorrectly–Further parsing of some event fields•Using well known mechanisms such as–Parser Overrides–Categorization Overrides–Map Files•And not so well known mechanisms such as–Conditional Mappings–Event Merging–Extra Processors–Additional Regex Parsers–External Map Files–Field ExtractorsParser overridesSupport generally provides parser overrides to customers to fix parsing and mapping issues•You can write your own parser overrides by requesting Support for–Unobfuscated parsers for a given connector and device–Location where the overrides are to be placed for the connector and device (current/user/agent/fcp/<folder-path>/) •Unobfuscated parsers are HP’s IP and are provided only based on a business justification provided by the customer•In all types of parsers, you can–Add new event field mappings for the unmapped existing tokens–Capture more tokens from raw logs and map them–Fix or enhance the existing event field mappings with the help of•Operations(documented in flex connector guide)•Conditional Mappings (discussed in a later slide)–Further parse the individual fields using extra processors (discussed in a later slide)•In regular expression parsers, you can–Combine information from individual log lines that are part of a larger event into a single event using Event Merging (discussed in a later slide) •Simple example - parser override for oracle audit db connector to correct the event time from GMT to local time zone–Add the following property to the override file current/user/agent/fcp/oracle_db/10_X.sdktbdatabase.properties event.deviceReceiptTime=__toLocalTimeStamp(TIMESTAMP)Categorization overridesFixes and enhancements to categorization come in biweekly AUP updates for subscribed customers•You can also come up with your own fixes and enhancements and place them–In a file named after the ‘Device Product’–In a subfolder named after the ‘Device Vendor’ under the folder “current/user/agent/acp/categorizer/current”–ExamplesCISCO, CiscoRouter cisco/ciscorouter.csvCISCO, Cisco VPN cisco/cisco_vpn.csvBarracuda Networks, Spam Firewall barracuda_networks/spam_firewall.csv•Categorization files are also used to set agent severities where a device does not provide device severity. You can use the same concept to provide agent severities–In the case of your own flex connector when the device does not have a concept of a device severity–In the case of some event types where the category fields and severities are either missing or populated incorrectly•Example: A categorization file the provides missing categories and agent severities for VMWare VirtualCentre–current/user/agent/acp/categorizer/current/vmware/virtualcentre.csvevent.deviceEventClassId,set.event.categoryObject,set.event.categoryBehavior,set.event.categoryTechnique,set.event.categoryDeviceGroup ,set.event.categorySignificance,set.event.categoryOutcome,set.event.originator,set.agentSeverityUserLoginSessionEvent,/Host/Operating System,/Access/Start,,/Operating System,/Informational,/Success,Source,LowUserLogoutSessionEvent,/Host/Operating System,/Access/Stop,,/Operating System,/Informational,/Success,Source,LowMap filesCSV files that allow you to set values for some event fields based on the values of other event fields event.deviceCustomString1,regex.event.deviceCustomString2,range.event.deviceCustomNumber1,set.event.customerURI North America,americas_.*,1-99,/All Customers/Americas/CustomerAEurope,emea_.*,100-199,/All Customers/EMEA/CustomerBAsia,apj_.*,200-299, /All Customers/APJ/CustomerC•Header of the CSV file defines the Getter and Setter fields–Getter Fields•Constant values - specified in the header as event.field•Regular Expressions - specified in the header as regex.event.field•Ranges (Integers, IP Addresses, MAC Addresses) – specified in the header as range.event.field–Setter Fields•Only constant values – specified in the header as set.event.field•Contents of the file come from a public or private knowledge outside of raw logs•Location and Naming of files–Are placed under current/user/agent/map folder–Named as map.N.properties where N is the next available number in the numerical sequence 0,1,2…–Caveat: If there is a missing file in the sequence, map files that follow in the sequence will not be picked upConditional mappingsAllow you to do some of the event field mappings differently based on a certain token or event field •Widely used in windows unified connector parsers to do mappings differently based on the windows event id •In firewall and IDS events, source and destination mappings can be reversed based on the direction of traffic •You can use them for use cases such as above in parser overrides or flex connectors•Example:conditionalmap.count=1conditionalmap[0].field=event.deviceEventClassIdORconditionalmap[0].token=EventIDconditionalmap[0].mappings.count=2conditionalmap[0].mappings[0].values=532,534conditionalmap[0].mappings[0].event.sourceAddress=TOKEN_ADDRESSconditionalmap[0].mappings[1].values=533conditionalmap[0].mappings[1].event.destinationAddress=TOKEN_ADDRESSconditionalmap[0].mappings[1].additionaldata.DATA=TOKEN_DATAEvent mergingAllows you to combine multiple log lines into a single event•Used when the log lines happen to be segments of a single logical event spanning over a period of time•Currently limited to regular expression parser•ExampleUnix Sendmail logssendmail[24080]: h8ULinI24080: from=<dasusr1>, size=184, class=0, nrcpts=1, msgid=<200309302144.h8ULinI24080@n030->, proto=SMTP, daemon=MTA, relay= [192.168.30.102] (may be forged)sendmail[1987]: g8AEvxpV001987:from=root,size=643,class=0,nrcpts=1,msgid=<***************************************>, relay=root@localhostsendmail[27596]: h8ULinI24080: to=<dasusr1@n030-h102>, ctladdr=<dasusr1> (111/101), delay=00:00:00, xdelay=00:00:00, mailer=local, pri=30116, dsn=2.0.0, stat=Sentsendmail[1987]: g8AEvxpV001987: to=root, ctladdr=root (0/0), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30051, relay=localhost [127.0.0.1], dsn=4.0.0, stat=Deferred: Connection refused by localhost–Individual log lines contain only partial info. Event is not complete without the info from log lines for the same email message –Log lines for the same email message need not come in order–Common token is a requirement for merging events. In this case it is the email message idEvent merging - continued•Merging Propertiesregex=.*?(?:sendmail|sm-mta).*? (\\S+?): ((from|to|ruleset|lost input channel)(?:=|\\s).*) token.count=3token[0].name=messageIdtoken[1].name=eventtoken[2].name=identifiermerge.count=1merge[0].pattern.count=1merge[0].pattern[0].token= Identifiermerge[0].pattern[0].regex=(from|to)merge[0].starts.count=1merge[0].starts[0].token=identifiermerge[0].starts[0].regex=frommerge[0].ends.count= 1merge[0].ends[0].token=Identifiermerge[0].ends[0].regex= tomerge[0].id.tokens= messageIdmerge[0].timeout=6000merge[0].sendpartialevents=falseExtra processorsChained parsers called from inside an existing parser to parsed the contents of an event field further •Used when the content of the field is in a different format than parsed by the parent parser–Example: When a portion of the log message or a database column has xml content, an xml extra processor is used extraprocessor[0].type=xmlextraprocessor[0].filename=fpsmc_db/notification/xmlforefrontextraprocessor[0].field=event.deviceCustomString1extraprocessor[0].conditionfield=event.deviceCustomString1extraprocessor[0].conditiontype=regexextraprocessor[0].conditionvalues=(^<\\?xml.*)extraprocessor[0].clearfieldafterparsing=false•Extra Processor Typesregex regular expression parserkeyvalue keyvalue parserdelimited delimited token parser (ex:- csv)xml xml parsermap map file•Condition Typesregex field content matches regular expression conditionequals field value equals one of the condition valuesAdditional Regex parsersRegex parsers deployed for a connector type to further parse the contents of one or more event fields •Contents of the parser file are very much like a regular expression parser used in a flex connector or supported connector –The only extra property is ‘source.field’ which defines the event field that needs to be further parsed–Simple Example:source.field=event.messageregex=(.*?):(.*)token.count=2token[0].name=String1token[0].name=String2event.flexString1=String1event.flexString2=String2•Location and Naming of files–Placed under current/user/agent/fcp/additionalregexparsing/<agent-type> folder where <agent-type> is the value of agents[0].type in current/user/agent/agent.properties file–Named as regex.N.properties where N is the next available number in the numerical sequence 0,1,2…–Caveat: If there is a missing file in the sequence, map files that follow in the sequence will not be picked up•Advantages over a regex extra processor–Independent of original parsers or parser overrides and kicks in after both of them in the event flow–In connectors such as syslog connector with subagents, same event enrichment may require multiple parser overrides for different subagents. Insuch cases, a single additional regex parser can be used to achieve the same resultExternal map filesA type of map file mechanism where the data to set resides in an external source like a database •Contents of the file are properties that define the mapping unlike map files which are CSV files with a header–Example Propertiestype=sql (Only database type supported currently for external source)field.getter=destinationAddress (Only one getter field can be specified)field.setter.count=2 (At least one setter field needs to be specified)field.setter[0]=deviceCustomString1field.setter[1]=deviceCustomDate1jdbc.class=oracle.jdbc.driver.OracleDriverjdbc.url=jdbc:oracle:thin:@myhost:1521:arcsight (URL syntax varies based on the jdbc driver used)ername=arcsightjdbc.password=OBFUSCATE.4.8.1:5pEDYBZpXFJGsYID9GibEA== (encrypted password)jdbc.query=select IPADDR, Info, TimeStamp from mytable where IPADDR in (?\u0000?)field.addrs.as.numbers=true (whether address columns in the database are numbers or strings) •Encrypted password–Clear text value for jdbc.password has to be encrypted using the following commandcurrent/bin/arcsight agent obfuscate -p <clear-text-password>External map files – continued•SQL Query–jdbc.query=select IPADDR, Info, TimeStamp from mytable where IPADDR in (?\u0000?)–Is executed for every batch of events (default batch size is 100)–Needs to return the getter column as the first column followed by all of the setter columns in the defined order–The where class needs to employ a condition involving the getter column with the ‘in’ operator followed by a special placeholder “?\u0000?” which will be replaced by the actual values of the defined getter field from the batch of events–The values of the setter columns returned in the query result will be set to the defined setter fields in the batch of events •Naming and Location of the files–Placed under current/user/agent/extmap/id folder, where id is the identifier for a specific destination–Named as extmap.N.properties where N is the next available number in the numerical sequence 0,1,2…Field extractorsAllow you to extract relevant information from the log file names or paths into event fields •Particularly useful when this information is missing from the raw logs•Example1: For a file reader connector–Assume that your log file name is 'myhost.1.log' and that when it rotates, it is 'myhost.2.log' and so on.–If you need to extract both the host name and the index. You can accomplish extraction by specifying the following properties agents[0].usefieldextractor=trueagents[0].extractsource=File Nameagents[0].extractregex=(\\w+)\\.(\\d+)\\.logagents[0].extractfieldnames=deviceHostName,deviceCustomNumber1•Example2: For a multiple folder follower connector–Assume that your multiple folder flex connector is processing logs from multiple web server instances running on different ports on multiple machines and the logs are deposited in a folder named after the machine IP Address and the instance’s port number (/opt/mnt/webservers/1.1.1.1/443, /opt/mnt/webservers/1.1.1.1/8443, /opt/mnt/webservers/2.2.2.2/443 …)–You can extract the IP Address and Port Number with the following properties specified for every folderagents[0].folder[N].usefieldextractor=trueagents[0].folder[N]. extractsource=File Pathagents[0].folder[N].extractregex=.*?(\\d+.\\d+.\\d+.\\d+)/(\\d+)/.*?.logagents[0].folder[N].extractfieldnames=deviceIPAddress,deviceCustomNumber1Customer use case•OriginalAgentHostName and OriginalAgentIPAddress for system health events from C1 connectors on ESM console are being set to host name and IP Address of CA2 and not CA1•When observed on Logger, the originalAgent fields were set correctly to CA1•Development agreed it is a bug and will provide a fix in the long term•Support provided short term workaroundWorkaround•Map files on C1 connectors (current/user/agent/fcp/map/map.0.properties )set.event.flexString1, set.event.flexString2 Host-name1, 192.168.10.1 •Additional regex parsers on the Logger onboard connectorunder current/user/agent/fcp/additionalregexparsing/loggerCA1 C2 = UDP encrypted CEF Syslog connectors forwarding system health events from C1 to Logger Logger is forwarding all events from C2 to ESM Connector Appliances regex.0.sdkrfilereader.propertiesregex.1.sdkrfilereader.properties。
大连理工大学优化方法上机大作业
学院:专业:班级:学号:姓名:上机大作业1:1.最速下降法:function f = fun(x)f = (1-x(1))^2 + 100*(x(2)-x(1)^2)^2; endfunction g = grad(x)g = zeros(2,1);g(1)=2*(x(1)-1)+400*x(1)*(x(1)^2-x(2)); g(2) = 200*(x(2)-x(1)^2);endfunction x_star = steepest(x0,eps) gk = grad(x0);res = norm(gk);k = 0;while res > eps && k<=1000dk = -gk;ak =1; f0 = fun(x0);f1 = fun(x0+ak*dk);slope = dot(gk,dk);while f1 > f0 + *ak*slopeak = ak/4;xk = x0 + ak*dk;f1 = fun(xk);endk = k+1;x0 = xk;gk = grad(xk);res = norm(gk);fprintf('--The %d-th iter, the residual is %f\n',k,res); endx_star = xk;end>> clear>> x0=[0,0]';>> eps=1e-4;>> x=steepest(x0,eps)2.牛顿法:function f = fun(x)f = (1-x(1))^2 + 100*(x(2)-x(1)^2)^2; endfunction g = grad2(x)g = zeros(2,2);g(1,1)=2+400*(3*x(1)^2-x(2));g(1,2)=-400*x(1);g(2,1)=-400*x(1);g(2,2)=200;endfunction g = grad(x)g = zeros(2,1);g(1)=2*(x(1)-1)+400*x(1)*(x(1)^2-x(2)); g(2) = 200*(x(2)-x(1)^2);endfunction x_star = newton(x0,eps)gk = grad(x0);bk = [grad2(x0)]^(-1);res = norm(gk);k = 0;while res > eps && k<=1000dk=-bk*gk;xk=x0+dk;k = k+1;x0 = xk;gk = grad(xk);bk = [grad2(xk)]^(-1);res = norm(gk);fprintf('--The %d-th iter, the residual is %f\n',k,res); endx_star = xk;end>> clear>> x0=[0,0]';>> eps=1e-4;>> x1=newton(x0,eps)--The 1-th iter, the residual is--The 2-th iter, the residual isx1 =法:function f = fun(x)f = (1-x(1))^2 + 100*(x(2)-x(1)^2)^2; endfunction g = grad(x)g = zeros(2,1);g(1)=2*(x(1)-1)+400*x(1)*(x(1)^2-x(2)); g(2) = 200*(x(2)-x(1)^2);endfunction x_star = bfgs(x0,eps) g0 = grad(x0);gk=g0;res = norm(gk);Hk=eye(2);k = 0;while res > eps && k<=1000dk = -Hk*gk;ak =1; f0 = fun(x0);f1 = fun(x0+ak*dk);slope = dot(gk,dk);while f1 > f0 + *ak*slopeak = ak/4;xk = x0 + ak*dk;f1 = fun(xk);endk = k+1;fa0=xk-x0;x0 = xk;go=gk;gk = grad(xk);y0=gk-g0;Hk=((eye(2)-fa0*(y0)')/((fa0)'*(y0)))*((eye(2)-(y0)*(fa0)')/((fa0)'*(y0)))+(fa0*(fa 0)')/((fa0)'*(y0));res = norm(gk);fprintf('--The %d-th iter, the residual is %f\n',k,res);endx_star = xk;End>> clear>> x0=[0,0]';>> eps=1e-4;>> x=bfgs(x0,eps)4.共轭梯度法:function f = fun(x)f = (1-x(1))^2 + 100*(x(2)-x(1)^2)^2; endfunction g = grad(x)g = zeros(2,1);g(1)=2*(x(1)-1)+400*x(1)*(x(1)^2-x(2)); g(2) = 200*(x(2)-x(1)^2);endfunction x_star =CG(x0,eps) gk = grad(x0);res = norm(gk);k = 0;dk = -gk;while res > eps && k<=1000 ak =1; f0 = fun(x0);f1 = fun(x0+ak*dk);slope = dot(gk,dk);while f1 > f0 + *ak*slope ak = ak/4;xk = x0 + ak*dk;f1 = fun(xk);endk = k+1;x0 = xk;g0=gk;gk = grad(xk);res = norm(gk);p=(gk/g0)^2;dk1=dk;dk=-gk+p*dk1;fprintf('--The %d-th iter, the residual is %f\n',k,res); endx_star = xk;end>> clear>> x0=[0,0]';>> eps=1e-4;>> x=CG(x0,eps)上机大作业2:function f= obj(x)f=4*x(1)-x(2)^2-12;endfunction [h,g] =constrains(x) h=x(1)^2+x(2)^2-25;g=zeros(3,1);g(1)=-10*x(1)+x(1)^2-10*x(2)+x(2)^2+34;g(2)=-x(1);g(3)=-x(2);endfunction f=alobj(x) %拉格朗日增广函数%N_equ等式约束个数?%N_inequ不等式约束个数N_equ=1;N_inequ=3;global r_al pena;%全局变量h_equ=0;h_inequ=0;[h,g]=constrains(x);%等式约束部分?for i=1:N_equh_equ=h_equ+h(i)*r_al(i)+(pena/2)*h(i).^2;end%不等式约束部分for i=1:N_inequh_inequ=h_inequ+pena)*(max(0,(r_al(i)+pena*g(i))).^2-r_al(i).^2); end%拉格朗日增广函数值f=obj(x)+h_equ+h_inequ;function f=compare(x)global r_al pena N_equ N_inequ;N_equ=1;N_inequ=3;h_inequ=zeros(3,1);[h,g]=constrains(x);%等式部分for i=1:1h_equ=abs(h(i));end%不等式部分for i=1:3h_inequ=abs(max(g(i),-r_al(i+1)/pena));endh1 = max(h_inequ);f= max(abs(h_equ),h1); %sqrt(h_equ+h_inequ);function [ x,fmin,k] =almain(x_al)%本程序为拉格朗日乘子算法示例算法%函数输入:% x_al:初始迭代点% r_al:初始拉格朗日乘子N-equ:等式约束个数N_inequ:不等式约束个数?%函数输出% X:最优函数点FVAL:最优函数值%============================程序开始================================ global r_al pena ; %参数(全局变量)pena=10; %惩罚系数r_al=[1,1,1,1];c_scale=2; %乘法系数乘数cta=; %下降标准系数e_al=1e-4; %误差控制范围max_itera=25;out_itera=1; %迭代次数%===========================算法迭代开始============================= while out_itera<max_iterax_al0=x_al;r_al0=r_al;%判断函数?compareFlag=compare(x_al0);%无约束的拟牛顿法BFGS[X,fmin]=fminunc(@alobj,x_al0);x_al=X; %得到新迭代点%判断停止条件?if compare(x_al)<e_aldisp('we get the opt point');breakend%c判断函数下降度?if compare(x_al)<cta*compareFlagpena=1*pena; %可以根据需要修改惩罚系数变量elsepena=min(1000,c_scale*pena); %%乘法系数最大1000disp('pena=2*pena');end%%?更新拉格朗日乘子[h,g]=constrains(x_al);for i=1:1%%等式约束部分r_al(i)= r_al0(i)+pena*h(i);endfor i=1:3%%不等式约束部分r_al(i+1)=max(0,(r_al0(i+1)+pena*g(i)));endout_itera=out_itera+1;end%+++++++++++++++++++++++++++迭代结束+++++++++++++++++++++++++++++++++ disp('the iteration number');k=out_itera;disp('the value of constrains'); compare(x_al)disp('the opt point');x=x_al;fmin=obj(X);>> clear>> x_al=[0,0];>> [x,fmin,k]=almain(x_al)上机大作业3: 1、>> clear alln=3; c=[-3,-1,-3]'; A=[2,1,1;1,2,3;2,2,1;-1,0,0;0,-1,0;0,0,-1];b=[2,5,6,0,0,0]';cvx_beginvariable x(n)minimize( c'*x)subject toA*x<=bcvx_endCalling SDPT3 : 6 variables, 3 equality constraints------------------------------------------------------------num. of constraints = 3dim. of linear var = 6*******************************************************************SDPT3: Infeasible path-following algorithms*******************************************************************version predcorr gam expon scale_dataNT 1 1 0it pstep dstep pinfeas dinfeas gap prim-obj dual-obj cputime -------------------------------------------------------------------0|||+01|+00|+02|+01 +00| 0:0:00| chol 1 11|||||+01|+00 +01| 0:0:01| chol 1 12|||||+00|+00 +01| 0:0:01| chol 1 13|||||+00|+00 +00| 0:0:01| chol 1 14||||||+00 +00| 0:0:01| chol 1 15||||||+00 +00| 0:0:01| chol 1 16||||||+00 +00| 0:0:01| chol 1 17||||||+00 +00| 0:0:01| chol 1 18||||||+00 +00| 0:0:01|stop: max(relative gap, infeasibilities) <------------------------------------------------------------------- number of iterations = 8primal objective value = +00dual objective value = +00gap := trace(XZ) =relative gap =actual relative gap =rel. primal infeas (scaled problem) =rel. dual " " " =rel. primal infeas (unscaled problem) = +00rel. dual " " " = +00norm(X), norm(y), norm(Z) = +00, +00, +00norm(A), norm(b), norm(C) = +00, +00, +00Total CPU time (secs) =CPU time per iteration =termination code = 0DIMACS: +00 +00-------------------------------------------------------------------------------------------------------------------------------Status: SolvedOptimal value (cvx_optval):2、>> clear alln=2; c=[-2,-4]'; G=[,0;0,1]; A=[1,1;-1,0;0,-1]; b=[1,0,0]'; cvx_beginvariable x(n)minimize( x'*G*x+c'*x)subject toA*x<=bcvx_endCalling SDPT3 : 7 variables, 3 equality constraintsFor improved efficiency, SDPT3 is solving the dual problem.------------------------------------------------------------num. of constraints = 3dim. of socp var = 4, num. of socp blk = 1dim. of linear var = 3*******************************************************************SDPT3: Infeasible path-following algorithms*******************************************************************version predcorr gam expon scale_dataNT 1 1 0it pstep dstep pinfeas dinfeas gap prim-obj dual-obj cputime -------------------------------------------------------------------0||||+00|+02| +01 +00| 0:0:00| chol 1 11|||||+01| +00 | 0:0:00| chol 1 12|||||+00| +00 | 0:0:00| chol 1 13|||||| | 0:0:00| chol 1 14|||||| | 0:0:00| chol 1 15|||||| | 0:0:00| chol 1 16|||||| | 0:0:00| chol 1 17|||||| | 0:0:00| chol 1 18|||||| | 0:0:00| chol 1 19|||||| | 0:0:00| chol 1 110|||||| | 0:0:00| chol 2 211|||||| | 0:0:00| chol 2 212|||||| | 0:0:00| chol 2 213|||||| | 0:0:00| chol 2 214|||||| | 0:0:00|stop: max(relative gap, infeasibilities) <------------------------------------------------------------------- number of iterations = 14primal objective value =dual objective value =gap := trace(XZ) =relative gap =actual relative gap =rel. primal infeas (scaled problem) =rel. dual " " " =rel. primal infeas (unscaled problem) = +00rel. dual " " " = +00norm(X), norm(y), norm(Z) = +00, +00, +00norm(A), norm(b), norm(C) = +00, +00, +00Total CPU time (secs) =CPU time per iteration =termination code = 0DIMACS: +00 +00-------------------------------------------------------------------------------------------------------------------------------Status: SolvedOptimal value (cvx_optval): -3。
优化方法上机作业
1. Wolfe-Powell方法建立m文件function [x,minf] = minWP(f,XMAX,sigma1,sigma2,alpha,eps) format long;if nargin == 5eps = 1.0e-6;endif sigma1<=0||sigma1>1disp('sigma1参数不对¡');x = NaN;minf = NaN;return;elseif sigma2 <= sigma1disp('sigma2参数不对¡');x = NaN;minf = NaN;return;elseif alpha <= 1disp('alpha参数不对¡');x = NaN;minf = NaN;return;endendenddf = diff(f);f0 = subs(f, findsym(f),0);df0 = subs(df, findsym(df),0);a = 0;b = XMAX;k = 0;t = (a+b)/2;while 1ft = subs(f,findsym(f),t);f1 = f0 + sigma1*t*df0;if ft <= f1f2 = sigma2*t*df0;if ft >= f2x = t;break;elsea = t;if b < XMAXt = (a+b)/2;elset = alpha*t;endendelseb = t;t = (a+b)/2;endk = k+1;endminf = subs(f,findsym(f),x);format short;在命令窗口输入:syms t;>> f=t^2-2*t+7;>> x1=minWP(f,8,0.1,0.65,2)>> x2=minWP(f,9,0.1,0.65,2)>> x3=minWP(f,10,0.1,0.65,2)输出结果:x1 = 1x2 =1.1250x3 = 1.25002.BFGSf=inline('(x1+10*x2)^2+5*(x3-10*x4)^2+(x2-2*x3)^4+10*(x1-x4)^4','x1', 'x2','x3','x4');g1=inline('2*(x1+10*x2)+30*(x1-x4)^3','x1','x2','x3','x4');g2=inline('20*(x1+10*x2)+4*(x2-2*x3)^3','x1','x2','x3','x4');g3=inline('10*(x3-10*x4)-8*(x2-2*x3)^3','x1','x2','x3','x4');g4=inline('-100*(x3-10*x4)-40*(x1-x4)^3','x1','x2','x3','x4');x1=3;x2=-1;x3=0;x4=1;maxk=500;rho=0.6;sigma=0.4;k=0;eps=1e-4;Bk=eye(4);while k<maxkg=[g1(x1,x2,x3,x4);g2(x1,x2,x3,x4);g3(x1,x2,x3,x4);g4(x1,x2,x3,x4)]; d=norm(g);if d<epsbreak;enddk=-Bk\g;m=0; mk=0;while(m<20)new=f(x1+rho^m*dk(1),x2+rho^m*dk(2),x3+rho^m*dk(3),x4+rho^m*dk(4));old=f(x1,x2,x3,x4);if new<old+sigma*rho^m*g'*dkmk=m;break;endm=m+1;endx1=x1+rho^mk*dk(1);x2=x2+rho^mk*dk(2);x3=x3+rho^mk*dk(3);x4=x4+rho^mk*dk(4);sk=rho^mk*dk;yk=[g1(x1,x2,x3,x4);g2(x1,x2,x3,x4);g3(x1,x2,x3,x4);g4(x1,x2,x3,x4)]-g;if yk'*sk>0Bk=Bk-(Bk*sk*sk'*Bk)/(sk'*Bk*sk)+(yk*yk')/(yk'*sk);endk=k+1 ;x=[x1;x2;x3;x4];endf(x1,x2,x3,x4)运行结果:1.0644e-009DFP建立fun.mfunction y=fun(x)y=(x(1)+10*x(2))^2+5*(x(3)-10*x(4))^2+(x(2)-2*x(3))^4+10*(x(1)-x(4))^ 4;建立dfun.mfunction g=dfun(x)g(1)=2*(x(1)+10*x(2))+30*(x(1)-x(4))^3;g(2)=20*(x(1)+10*x(2))+4*(x(2)-2*x(3))^3;g(3)=10*(x(3)-10*x(4))-8*(x(2)-2*x(3))^3;g(4)=-100*(x(3)-10*x(4))-40*(x(1)-x(4))^3;g=[g(1) g(2) g(3) g(4)]';建立f_search.mfunction y=f_search(t)global x py=(x(1)+t*p(1)+10*(x(2)+t*p(2)))^2+5*(x(3)+t*p(3)-10*(x(4)+t*p(4)))^2 +(x(2)+t*p(2)-2*(x(3)+t*p(3)))^4+10*(x(1)+t*p(1)-x(4)-t*p(4))^4;建立print.mfunction print()global k ;disp('The optimum solution is:');solution=DFP();disp(solution);disp('The optimum value is:');value=fun(solution);disp(value);disp('The iterative times are: ');建立主程序:function opt=DFP()global x p k Hx=[3 -1 0 1]';H=eye(4);eps=1e-4;g=dfun(x);k=0;while 1p=-1*H*g;t_min=fminbnd(@f_search,0,20);temp_x=x;x=x+t_min*p;temp_g=g;g=dfun(x);if norm(g)<epsopt=x;break;ends=x-temp_x;y=g-temp_g;H=H-(1/(y'*H*y))*(H*y*y'*H)+(1/(y'*s))*(s*s'); k=k+1;end运行结果:9.0029e-0083.。
最优化上机作业
给出x0,及精度ε=0.001,k=0计算df(xk),ddf(xk)K=k+1X(k+1)=xk-df(xk)/ddf(xk),N|Df(xk)|<εYX*=xkfunction [x,minf]=minNewton(f,x0,eps)format long;if nargin==2eps=1.0e-6;enddf=diff(f);d2f=diff(df);k=0;tol=1;while tol>epsdfx=subs(df,findsym(df),x0);d2fx=subs(d2f,findsym(d2f),x0);x1=x0-dfx/d2fx;k=k+1;tol=abs(dfx);x0=x1;endx=x1;minf=subs(f,findsym(f),x);format short;>> f=3*t^4-16*t^3+30*t^2-24*t+8;>> x=minNewton(f,2)x =2抛物线法function[x,minf]=minPWX(f,a,b,eps)format long;if nargin==3eps=1.0e-6;endt0=(a+b)/2;k=0;tol=1;while tol>epsfa=subs(f,findsym(f),a);fb=subs(f,findsym(f),b);ft0=subs(f,findsym(f),t0);tu=fa*(b^2-t0^2)+fb*(t0^2-a^2)+ft0*(a^2-b^2); td=fa*(b-t0)+fb*(t0-a)+ft0*(a-b);t1=tu/2/td;ft1=subs(f,findsym(f),t1);if ft1<=ft0if t1<=t0b=t0;t0=t1;elsea=t0;t0=t1;endk=k+1;elseif t1<=t0a=t1;elseb=t1;k=k+1;endendx=t1;minf=subs(f,findsym(f),x);format short;syms t;f=t^4/4-4*t^3/3+5*t^2/2-2*t; x=minPWX(f,0.5,4)共轭梯度法function f=conjugate_grad_2d(x0,t)x=x0;syms xi yi a;f=xi^2+2*yi^2-4*xi-2*xi*yi;f fx=diff(f,xi);fy=diff(f,yi);fx=subs(fx,{xi,yi},x0);fy=subs(fy,{xi,yi},x0);fi=[fx,fy];count=0; 0while double(sqrt(fx^2+fy^2))>ts=-fi;if count<=0s=-fi;elses=s1;endx=x+a*s;f=subs(f,{xi,yi},x);f1=diff(f);f1=solve(f1);if f1~=0ai=double(f1);elsebreakendx=subs(x,a,ai);f=xi^2+2*yi^2-4*xi-2*xi*yi;fxi=diff(f,xi);fyi=diff(f,yi);fxi=subs(fxi,{xi,yi},x);fyi=subs(fyi,{xi,yi},x);fii=[fxi,fyi];d=(fxi^2+fyi^2)/(fx^2+fy^2);s1=-fii+d*s;count=count+1;fx=fxi;fy=fyi;endx,f=subs(f,{xi,yi},x),count阻尼牛顿法function y=fun(x);y=x(1)^2+x(2)^2+x(1)*x(2);function g=gfun(x);g=2*x(1)+ x(2)+2*x(2)+ x(1);function He=Hess(x); n=length(x);He=zeros(n,n);He=1;function [x,val,k]=dampnm(fun,gfun,Hess,x0); maxk=100;rho=0.55;sigma=0.4;k=0;epsilon=1e-5;while (k<maxk)gk=feval(gfun,x0);Gk=feval(Hess,x0);dk=-Gk\gk;if (norm(dk)<epsilon)break;endm=0;mk=0;while(m<20)if (feval(fun,x0+rho^m*dk)<feval(fun,x0)+sigma*rho^m*gk'*dk) mk=m; break; end m=m+1; endx0=x0+rho^mk*dk; k=k+1; endx=x0;val=feval(fun,x);x0=[1,1]';[x,val,k]=dampnm('fun','gfun','Hess',x0)。
优化方法上机作业--2013
优化方法上机作业--2013第一题(牛顿法和不精确一维搜索)牛顿法:syms x1 x2;f=(x2-(x1)*(x1))^2+(1-x1)^2;v=[x1,x2];df=jacobian(f,v);df=df.';G=jacobian(df,v);epson=1e-12;x0=[0,1]';g1=subs(df,{x1,x2},{x0(1,1),x0(2,1)});G1=subs(G,{x1,x 2},{x 0(1,1),x0(2,1)});k=0;mul_count=0;sum_count=0;mul_count=mul_count+12;sum_count=sum_count+6;while(norm(g1)>epson)p=-G1\g1;x0=x0+p;g1=subs(df,{x1,x2},{x0(1,1),x0(2,1)});G1=subs(G,{x1,x2},{x0(1,1),x0(2,1)});k=k+1;mul_count=mul_count+16;sum_count=sum_count+11;end;kx0mul_countsum_count运行结果:k =9x0 =11mul_count =156sum_count =105不精确一维搜索:fun1.m文件function f=fun1(x)f=(x(1)-1)^2+(x(2)-x(1)^2)^2;gfun1.m文件function gf = gfun1(x)gf=[2*x(1)*(x(1)^2-x(2))+2*(x(1)-1),2*(x(2)-x(1)^2)]';wolfepowell.m文件function [k,m,opt,x]=wolfepowell(xk,sk)max=1000;c1=0.1; c2=0.6;a=0;b=inf;dk=0.2;m=0;while(m<=max)if (fun1(xk)-fun1(xk+sk*dk)<-c1*dk*gfun1(xk)'*sk)b=dk;dk=(dk+a)/2;elseif(fun1(xk)-fun1(xk+sk*dk)>=-c1*dk*gfun1(xk)'*sk)&&(gfun1(xk+sk*dk)'*skif(gd>=0.0)d=-g;endendif(norm(g)<="" p="">m=0; mk=0;while(m<20)if(feval(fun,x0+rho^m*d)<feval(fun,x0)+sigma*rho^m*g'*d) < p="">mk=m; break;endm=m+1;endx0=x0+rho^mk*d;val=feval(fun,x0);g0=g; d0=d;k=k+1;endx=x0;val=feval(fun,x);fun.m文件function f=fun(x)f=x(1)^2-2*x(1)*x(2)+2*x(2)^2+x(3)^2+x(4)^2-x(2)*x(3)+2*x(1)+3*x(2)-x(3); gfun.m文件function gf=gfun(x)gf=[2*x(1)-2*x(2)+2, -2*x(1)+4*x(2)+3,2*x(3)-x(2)-1,2*x(4)]';运行结果:>>x0=[0 0 0 0]';[x,val,k]=frcg('fun','gfun',x0)x =-3.4064-2.6515-0.7032val =-7.8338k =5000第三题(BFGS法、最速下降法和牛顿法)fun3.m文件function f=fun3(x)f=(x(1)-1)^2+5*(x(2)-x(1)^2)^2;gfun3.m文件function gf = gfun3(x)gf=[20*x(1)*(x(1)^2-x(2))+2*(x(1)-1),10*(x(2)-x(1)^2)]'; Hessen.m文件function Hess=Hessen(x)Hess=[60*x(1)^2-20*x(2)+2,(-20)*x(1);(-20)*x(1),10];Steepest.m文件function [m,opt,x] = Steepest(xk)m=0;Eps=1.0e-4;max=1000;while(m<max)< p="">g=gfun3(xk);sk=-g;dk=wolfepowell(xk,sk);x=xk+dk*sk;g0=gfun3(x);if(norm(g0)<=Eps)break;elsexk=x;endm=m+1;endmk=m;x=xk;opt=fun3(xk);newton.m文件function [x,opt,mk] = newton(xk)m=0;Eps=1e-4;max=100;H=[1,0;0,1]; while(m<max)< p="">g=gfun3(xk);M=Hess(xk);H=inv(M);sk=-H*g;dk=wolfepowell(xk,sk);x=xk+dk*sk;g0=gfun3(x);if(norm(g0)<=Eps)break;elsexk=x;endm=m+1;endmk=m;x=xk;opt=fun3(xk);bfgs.m文件function [x,opt,mk] = bfgs(xk)m=0;Eps=1e-4;max=100;H=[1,0;0,1]; while(m<max)< p="">g=gfun3(xk);sk=-H*g;dk=wolfepowell(xk,sk);x=xk+dk*sk;g0=gfun3(x);if(norm(g0)<=Eps)break;elseH=H+((1+((g0-g)'*H*(g0-g))/((x-xk)'*(g0-g)))*(x-xk)*(x-xk)'-H*(g0-g)*(x-xk)'-(x-x k)*(g0-g)'*H)/((x-xk)'*(g0-g));xk=x;endm=m+1;endmk=m;x=xk;opt=fun3(xk);wolfepowell.m文件function [k,m,opt,x]=wolfepowell(xk,sk)max=1000;c1=0.1; c2=0.6;a=0;b=inf;dk=0.2;m=0;while(m<=max)if (fun3(xk)-fun3(xk+sk*dk)<-c1*dk*gfun3(xk)'*sk)b=dk;dk=(dk+a)/2;elseif(fun3(xk)-fun3(xk+sk*dk)>=-c1*dk*gfun3(xk)'*sk)&&(gfun3(xk+sk*dk)'*skx =1.00001.0000opt =3.2601e-10mk =12最速下降法运行结果:>>xk=[2,0]';[x,opt,mk] = Steepest(xk)x =0.99990.9998opt =8.2108e-09mk =354BFGS法运行结果:>>xk=[2,0]';[x,opt,mk] = bfgs(xk)x =0.99990.9999opt =4.7725e-09mk =16(0.9999, 0.9998) 最速下降法(迭代次数354)即得数值最优点为x= (1.0000, 1.0000) 牛顿法(迭代次数12)(0.9999, 0.9999) BFGS公式(迭代次数16)最优解近似为0第四题(乘子法)multphr.m文件function[x,mu,lambda,output]=multphr(fun,hf,gf,dfun,dhf,dgf,x0) maxk=500;sigma=2.0;eta=2.0; theta=0.8;k=0; ink=0;epsilon=1e-5;x=x0; he=feval(hf,x); gi=feval(gf,x);n=length(x); l=length(he); m=length(gi);mu=0.1*ones(l,1); lambda=0.1*ones(m,1);btak=10; btaold=10;while(btak>epsilon && k<maxk)< p="">[x,~,ik]=bfgs('mpsi','dmpsi',x0,fun,hf,gf,dfun,dhf,dgf,mu,lam bda,sigma);ink=ink+ik;he=feval(hf,x); gi=feval(gf,x);btak=0.0;for (i=1:l), btak=btak+he(i)^2; endfor (i=1:m);temp=min(gi(i),lambda(i)/sigma);btak=btak+temp^2;endbtak=sqrt(btak);if(btak>epsilon)if(k>=2&&btak> theta*btaold)sigma=eta*sigma;endfor (i=1:l), mu(i)=mu(i)-sigma*he(i); endfor (i=1:m)lambda(i)=max(0.0,lambda(i)-sigma*gi(i));endendk=k+1;btaold=btak;x0=x;endf=feval(fun,x);output.fval=f;output.iter=k;output.inner_iter=ink;output.bta=btak;mpsi.m文件function psi=mpsi(x,fun,hf,gf,dfun,dhf,dgf,mu,lambda,sigma) f=feval(fun,x); he=feval(hf,x); gi=feval(gf,x);l=length(he); m=length(gi);psi=f; s1=0.0;for(i=1:l)psi=psi-he(i)*mu(i);s1=s1+he(i)^2;endpsi=psi+0.5*sigma*s1;s2=0.0;for(i=1:m)s3=max(0.0, lambda(i) - sigma*gi(i));s2=s2+s3^2-lambda(i)^2;endpsi=psi+s2/(2.0*sigma);h1.m文件function he=h1(x)he=-x(1)^2-x(2)^2+25.0;f1.m文件function f=f1(x)f=4*x(1)-x(2)^2-12;g1.m文件function gi=g1(x)gi=10*x(1)-x(1)^2+10*x(2)-x(2)^2-34;h1.m文件function dhe = dh1(x)dhe = [-1*x(1), -1*x(2)]';dg1.m文件function dgi = dg1(x)dgi = [10-2*x(1), 10-2*x(2)]';df1.m文件function g=df1(x)g = [4, -2.0*x(2)]';bfgs.m文件function [x,val,k]=bfgs(fun,gfun,x0,varargin) maxk=500;rho=0.55; sigma1=0.4; epsilon1=1e-5;k=0; n=length(x0);Bk=eye(n);while(k<maxk)< p="">gk=feval(gfun,x0,varargin{:});if(norm(gk)<="" p="">dk=-Bk\gk;m=0; mk=0;while(m<20)newf=feval(fun,x0+rho^m*dk,varargin{:});oldf=feval(fun,x0,varargin{:});if(newf<oldf+sigma1*rho^m*gk'*dk)< p="">mk=m; break;endm=m+1;endx=x0+rho^mk*dk;sk=x-x0; yk=feval(gfun,x,varargin{:})-gk;if(yk'*sk>0)Bk=Bk-(Bk*sk*sk'*Bk)/(sk'*Bk*sk)+(yk*yk')/(yk'*sk); endk=k+1; x0=x;endval=feval(fun,x0,varargin{:});dmpsi.m文件functiondpsi=dmpsi(x,fun,hf,gf,dfun,dhf,dgf,mu,lambda,sigma)dpsi=feval(dfun,x);he=feval(hf,x); gi=feval(gf,x);dhe=feval(dhf,x); dgi=feval(dgf,x);l=length(he); m=length(gi);for(i=1:l)dpsi=dpsi+(sigma*he(i)-mu(i))*dhe(:,i);endfor(i=1:m)dpsi=dpsi+(sigma*gi(i)-lambda(i))*dgi(:,i);end运行结果:>>x0=[1,1]';[x,mu,lambda,output]=multphr('f1','h1','g1','df1','dh1','dg1', x0) x =1.00134.8987mu =2.0312lambda =0.7545output =fval: -31.9923iter: 5inner_iter: 58bta: 4.3187e-07第五题(有效集法)qpact.m文件function [x,lamk,exitflag,output]=qpact(H,c,Ae,be,Ai,bi,x0) epsilon=1.0e-9; err=1.0e-6;k=0; x=x0; n=length(x); kmax=1.0e3;ne=length(be); ni=length(bi); lamk=zeros(ne+ni,1);index=ones(ni,1);for (i=1:ni)if(Ai(i,:)*x>bi(i)+epsilon), index(i)=0; endendwhile (k<=kmax)Aee=[ ];if(ne>0), Aee=Ae; endfor(j=1:ni)if(index(j)>0), Aee=[Aee; Ai(j,:)]; endendgk=H*x+c;[m1,n1] = size(Aee);[dk,lamk]=qsubp(H,gk,Aee,zeros(m1,1));if(norm(dk)<=err)y=0.0;if(length(lamk)>ne)[y,jk]=min(lamk(ne+1:length(lamk))); endif(y>=0)exitflag=0;elseexitflag=1;for(i=1:ni)if(index(i) & (ne+sum(index(1:i)))==jk)index(i)=0; break;endendendk=k+1;elseexitflag=1;alpha=1.0; tm=1.0;for(i=1:ni)if((index(i)==0)&(Ai(i,:)*dk<0))tm1=(bi(i)-Ai(i,:)*x)/(Ai(i,:)*dk); if(tm1<tm)< p=""> tm=tm1; ti=i;endendendalpha=min(alpha,tm);x = x+alpha*dk;if(tm<1), index(ti)=1; endendif(exitflag==0), break; endk=k+1;endoutput.fval=0.5*x'*H*x+c'*x;output.iter=k;qsubp.m文件function [x,lambda]=qsubp(H,c,Ae,be)ginvH=pinv(H);[m,n]=size(Ae);if (m>0)rb = Ae*ginvH*c + be;lambda = pinv(Ae*ginvH*Ae')*rb;x = ginvH*(Ae'*lambda-c);elsex = -ginvH*c;lambda = zeros(m,1);endcallqpact.m文件function callqpactH=[2 0; 0 2];c=[-2 -5]';Ae=[ ]; be=[ ];Ai=[1 -2; -1 -2; -1 2;1 0;0 1];bi=[-2 -6 -2 0 0]';x0=[0 0]';[x, lambda, exitflag,output]=qpact(H,c,Ae,be,Ai,bi,x0) 运行结果:>>callqpactx =1.40001.7000lambda =0.8000exitflag =output =fval: -6.4500iter: 7第七题fun文件function f=fun(x)f=abs(x(1))+abs(x(2))+abs(x(3))+abs(x(4));主程序x0=[1;1;1;1];A=[]; b=[];Aeq=[1 0 0 0.5;0 1 0.2 0.3;0 0.1 1 0.2];beq=[-1;0.2;1]; VLB=[]; VUB=[];[x,fval]=fmincon('fun',x0,A,b,Aeq,beq,VLB,VUB)运行结果x =-1.0000-0.00001.00000.0000 fval =2.0000</tm)<></oldf+sigma1*rho^m*gk'*dk)<></maxk)<></maxk)<></max)<></max)<></max)<></feval(fun,x0)+sigma*rho^m*g'*d)<></maxk)<>。
最优化方法练习题答案
练习题一1、建立优化模型应考虑哪些要素? 答:决策变量、目标函数和约束条件。
2、讨论优化模型最优解的存在性、迭代算法的收敛性及停止准则。
答:针对一般优化模型()()min ()..0,1,2, 0,1,,i j f x s t g x i m h x j p≥===,讨论解的可行域D ,若存在一点*X D ∈,对于X D ∀∈ 均有*()()f X f X ≤则称*X 为优化模型最优解,最优解存在;迭代算法的收敛性是指迭代所得到的序列(1)(2)(),,,K X X X ,满足(1)()()()K K f X f X +≤,则迭代法收敛;收敛的停止准则有(1)()k k x x ε+-<,(1)()()k k k x x x ε+-<,()()(1)()k k f x f x ε+-<,()()()(1)()()k k k f x f x f x ε+-<,()()k f x ε∇<等等。
练习题二1、某公司看中了例2.1中厂家所拥有的3种资源R 1、R2、和R 3,欲出价收购(可能用于生产附加值更高的产品)。
如果你是该公司的决策者,对这3种资源的收购报价是多少?(该问题称为例2.1的对偶问题)。
解:确定决策变量 对3种资源报价123,,y y y 作为本问题的决策变量。
确定目标函数 问题的目标很清楚——“收购价最小”。
确定约束条件 资源的报价至少应该高于原生产产品的利润,这样原厂家才可能卖。
因此有如下线性规划问题:123min 170100150w y y y =++1231231235210..23518,,0y y y s t y y y y y y ++≥⎧⎪++≥⎨⎪≥⎩ *2、研究线性规划的对偶理论和方法(包括对偶规划模型形式、对偶理论和对偶单纯形法)。
答:略。
3、用单纯形法求解下列线性规划问题:(1)⎪⎪⎩⎪⎪⎨⎧≥≤+-≤++≤-++-=0,,43222..min32131321321321x x x x x x x x x x x t s x x x z ; (2)⎪⎪⎩⎪⎪⎨⎧=≥=++=+-=+-+-=)5,,2,1(052222..4min53243232132 i x x x x x x x x x x t s x x z i解:(1)引入松弛变量x 4,x 5,x 6123456min 0*0*0*z x x x x x x =-++++12341232 =22 5 =3..13 6=41,2,3,4,5,60x x x x x x x x s t x x x x x x x x x +-+⎧⎪+++⎪⎨-++⎪⎪≥⎩因检验数σ2<0,故确定x 2为换入非基变量,以x 2的系数列的正分量对应去除常数列,最小比值所在行对应的基变量x 4作为换出的基变量。
优化方法作业第二版
优化方法上机大作业院系:化工与环境生命学部姓名:李翔宇学号:31607007 指导教师:肖现涛第一题:编写程序求解下述问题min y(z) = (1 —xi)2+ 100(x2 —xj)2.初始点取/ = 0,精度取s = le - 4,步长由Armijo线捜索生成,方向分别由下列方法生成:最速下降法BFGS方法共辄梯度法1.最速下降法源程序如下:function x_star = ZSXJ (x0,eps) gk = grad(xO);res = norm(g k);k = 0;while res > eps && k<=10000 dk = -gk;ak =1; f0 = fun(xO);f1 = fun(xO+ak*dk);slope = dot(gk,dk);while f1 > f0 + 0.0001*ak*slope ak = ak/2;xk = x0 + ak*dk;f1 = fun(xk);endk = k+1;x0 = xk;gk = grad(xk);res = norm(gk);fprintf('--The %d-th iter, the residual is %f\n',k,res);end x_star = xk; end function f = fun(x)f = (1-x(1))A2 + 100*以(2)咲(1)八2)八2;end function g = grad(x) g = zeros(2,1);g(1)=2*(x(1)-1)+400*x(1)*(x(1)A2-x(2)); g(2) = 200*(x(2)-x(1)A2);end 运行结果:>> x0=[0,0]';>> esp=1e-4;>> xk= ZSXJ(x0,eps)--The 1-th iter, the residual is 13.372079 --The 2-th iter, the residual is 12.079876 --The 3-th iter, the residual is 11.054105 --The 9144-th iter, the residual is 0.000105 --The 9145-th iter, the residual is 0.000102 --The 9146-th iter, the residual is0.000100 xk =0.99990.9998MATLAB 截屏:>> xO= [Qj 01’ :>> esp=le-4:>> xk=ZSXJ txOj eps)一The I-th it er?the residual is13.372075一The2-th it er j the residual is12,07&876一Iht3-th it亡「■the residual is11. 054105一The Wh it er j the residual is10. 421221一一Ih?5-th it er^the residual is10. 020369Tl_ -■:丄一一丄ll_ ______ I; Ji ___ '1J _一The9142-th iter,the residual is0.000111-- The9143-th iterj the residual is0.000108一一The9144-th iter>the匸esidual is D. 000105一-The9145-th iter f th?residual is0. 000102一Th?9143-th iter,the residual is0. 0001000.99990.99932. 牛顿法源程序如下:function x_star = NEWTON (x0,eps) gk = grad(x0);bk = [grad2(xO)]A(-1);res = norm(gk);k = 0;while res > eps && k<=1000dk=-bk*gk; xk=x0+dk;k = k+1;x0 = xk;gk = grad(xk);bk = [grad2(xk)]A(-1);res = norm(gk);fprintf('--The %d-th iter, the residual is %f\n',k,res); end x_star = xk;endfunction f = fun(x)f = (1-x(1))A2 + 100*(x(2)-x(1)A2)A2;end function g = grad2(x)g = zeros(2,2); g(1,1)=2+400*(3*x(1)A2-x(2));g(1,2)=-400*x(1);g(2,1)=-400*x(1);g(2,2)=200;endfunction g = grad(x)g = zeros(2,1);g(1)=2*(x(1)-1)+400*x(1)*(x(1)A2-x(2));g(2) = 200*(x(2)-x(1)A2);end运行结果:>> xO=[O,O]';eps=1e-4;>> xk= NEWTON (x0,eps)--The 1-th iter, the residual is 447.213595--The 2-th iter, the residual is 0.000000xk =1.00001.0000MATALB 截屏;»x0= [0, DY :>> esp=1;»xk^NEirONtaO, eps)—The 1-th iter, the residual is 447,213595 一The 2-th iter, the residual is0.OOQOQOxk =L 0000|L 00003. BFGS 方法源程序如下:function x_star = Bfgs(x0,eps) g0 = grad(x0);gk=g0;res = norm(gk);Hk=eye(2);k = 0;while res > eps && k<=1000dk = -Hk*gk;ak =1; f0 = fun(x0);f1 = fun(x0+ak*dk);slope = dot(gk,dk);while f1 > f0 + 0.1*ak*slopeak = ak/2;xk = x0 + ak*dk;f1 = fun(xk);endk = k+1;fa0=xk-x0;x0 = xk;g0=gk;gk = grad(xk); y0=gk-g0;Hk=((eye(2)-fa0*(y0)')/((fa0)'*(y0)))*((eye(2)-(y0)*(fa0)')/((fa0)'*(y0)))+(fa0*(fa0)')/((fa0)'*(y0));res = norm(gk);fprintf('--The %d-th iter, the residual is %f\n',k,res); end x_star = xk; end function f=fun(x)f=(1-x(1))A2 + 100*債(2)咲(1)八2)八2;endfunction g = grad(x)g = zeros(2,1);g(1)=2*(x(1)-1)+400*x(1)*(x(1)A2-x(2));g(2) = 200*(x(2)-x(1)A2);end运行结果:>> x0=[0,0]';>> esp=1e-4;>> xk= Bfgs(x0,eps)--The 1-th iter, the residual is 3.271712--The 2-th iter, the residual is 2.381565--The 3-th iter, the residual is 3.448742--The 1516-th iter, the residual is 0.000368 --The 1517-th iter, the residual is0.000099 xk =1.00011.0002MATLAB 截屏:x0= [0, 0]J:esp=le-4: xk=Bfgs (xOj eps)一The1th it erj the residual is3. 271712一The2th it erj the residual is2.381565一The3th it the residual is3.448742一The4th it er,the residual is3, 162431— The5th it erj the residual is2. 989084—The 1515-th iter, the residual is 0.000108—The 1516-th iter, the residual is 0.000368—The 1517-th iter, the residual is 0. 0000991.00011.00024. 共轭梯度法源程序如下:function x_star =Conj (x0,eps)gk = grad(x0);res = norm(gk);k = 0;dk = -gk;while res > eps && k<=1000ak =1; f0 = fun(x0);f1 = fun(x0+ak*dk);slope = dot(gk,dk);while f1 > f0 + 0.1*ak*slopeak = ak/2;xk = x0 + ak*dk;f1 = fun(xk);endd0=dk;g0=gk;k=k+1;xO=xk;gk=grad(xk);f=(norm(gk)/norm(gO))A2;res=norm(gk);dk=-gk+f*d0;fprintf('--The %d-th iter, the residual is %f\n',k,res);endx_star = xk;endfunction f=fun(x)f=(1-x(1))A2+100*(x(2)-x(1)A2)A2;endfunction g=grad(x)g=zeros(2,1);g(1)=400*x(1)A3-400*x(1)*x(2)+2*x(1)-2;g(2)=-200*x(1)A2+200*x(2);end运行结果:>> xO=[O,O]';>> eps=1e-4;>> xk=Conj(xO,eps)--The 1-th iter, the residual is 3.271712--The 2-th iter, the residual is 1.380542--The 3-th iter, the residual is 4.527780--The 4-th iter, the residual is 0.850596--The 73-th iter, the residual is 0.001532 --The 74-th iter, the residual is 0.000402 --The 75-th iter, the residual is0.000134 --The 76-th iter, the residual is 0.000057 xk =0.99990.9999MATLAB 截屏:» ito= O]T:>> esp=l*-4 :>> xk=Conj eps)—The l-th it efj the residual is 3. 271712~Ihe2-th it erj the residual IS 1. 3805423-th it er^the residual IS 4. 527780—I he4-th it er^the residual IS0.8505&6—I he5-th it erj the residual is0.559005——Tin 尺一+ Vi■i十戶T十Ha■r 戶 uT H IT-al H QRR7J.il■■丄丄比U 3^111丄LBL,LILS丄M th UUUUZJ一The70-th iter,the residual is0. 001423一The71-th iter,the residual is0. 002841---The72-th iter^the residual is0. 002945一The73-th itetj the residual is0. 001532—Th?74-th iter,the residual is0. 000403—The75-th it the residual is0. 000134一Iht76-th iter,the r*sidual is0.0000570.99990-9999第二题:编写程序利用增广拉格朗日方法求解下述问题初始点取= 0,精度取s= lc-4.解:目标函数文件fl.mfunction f=f1(x)f=4*x(1)-x(2)A2-12;等式约束函数文件hl.mfunction he=h1(x)he=25-x(1)A2-x(2)A2;不等式约束函数文件gl.mfunction gi=g1(x)gi=10*x(1)-x(1)A2+10*x(2)-x(2)A2-34;目标函数的梯度文件dfl.mfunction g=df1(x)g = [4, 2.0*x(2)]';等式约束(向量)函数的Jacobi矩阵(转置)文件dhl.mfunction dhe=dh1(x)dhe = [-2*x(1), -2*x(2)]';不等式约束(向量)函数的Jacobi矩阵(转置)文件dg1.mfunction dgi=dg1(x)dgi = [10-2*x(1), 10-2*x(2)]';然后在Matlab命令窗口输入如下命令:x0=[0,0]';[X,mu,lambda,output]=multphr('f1','h1','g1','df1','dh1','dg1',x0);得到如下输出:x =4.898717426488211.00128197198571算法编程利用程序调用格式function [x, mu, lambda, out put ] =mul t phr (furij hf, gf, dfun, dhfj dgf, xO)paxk=500;si gma=2* 0; eta z2. 0; theta z O* 8;k=0; ink=0;epsilon=le-4;x=xO; he-feval (hf f x); gi=feval (gf> x);n=length(K); 1=1 eng th (he); nFlength(gi),rru=O> l*ones(lj 1); lairbda=O» l*ones(n A 1); btak=10; btaold=10;^hile(btak>epsilon & k<maxk)[爲ival, ik]=bfgsC 叩si"," dirpsi", xO* fun, hf, gf, dfun, dhf, dgf, m f 1 ambda, sigma); ink二ink+ik;he=feval (h£ x); gi=feval (gf, x);btak-0,0; 丹for (1=1:1)^ btak=btak+he(i) "2; endfor i=l:mteitp=min(gi (i), lairbda(i)/sigma); btak-btak+temp"2;end btak z sqrt(btak); if btak>epsilon if(k>=2 & btak > theta*btaold) sig^eta+sigma, end for iDu[i)z mu(i)-sigina*he(i); endfor (i=l:nOlambda(i)z max(0.0, lairbda(i)-signia*gi(i));endendk二k+1;btaold=btak;xO=x;endf=f eval (fun, x);output. fral=f; output. iter=k;output* inner_iter-ink;output,bta-btak;function [x, val, k]=bfgs (fun^ gfun, xO, varargin) maxk=500;rho=0. 55; sigmal=O. 4; epsi1onl = le—5;k=0; n=length(xO);Bk=eye (n) ; %Bk=feval (' Hess^ , xO);while(k<maxk)gk=feval (gfun, xO, varargin {: });if(norm(gk)<epsilonl), break; enddk=-Bk\gk;n»=0; ink=O;while(m<20) 亠newf=feval (fun, xO+rho^irF^dk, varacrgin {: }); oldf=feval (fun, xO, varargin {: });i f (newf <ol df+si gir)al*rho m*gk,*dk) rnk=m; break;endm=irrH;endx= xO+rho ^ink*dk;sk=x—xO; yk=feval (gfun^ x, varargin {: } ) -gk;i f (yk^ *sk>0)Bk=Bk—(Bk*sk*sk J *Bk)/(sk‘ *Bk*sk)+(yk*yk J )/(yk‘ *sk); end k=k+l;xO=x;endval = f eval (fun, xO, v ar argin {: });第三题:■下载安装 CVX h http: /cvx/■利用CVX 编写代码求解下述问题迟1 + 牝 一 1 < 0. xi > 0.工2 > 0 ■利用CVX 编写代码求解下述问题min —3J :I — X2 — 3帀S.t. 2淤]十念2 +②3 W 2x\ + 2x2 + 3x3 < 5 2叭 +2x 2 + 巧 W6 37 > 0.1.解:将目标函数改写为向量形式:x'*a*x-b*x程序代码:n=2;a=[0.5,0;0,1];b=[2 4];c=[1 1];cvx_beginvariable x(n)minimize( x'*a*x-b*x)subject toc * x <= 1x>=0cvx_end运算结果:Calling SDPT3 4.0: 7 variables, 3 equality constraintsFor improved efficiency, SDPT3 is solving the dual problem.nun 1-2—2x1 —■4J *2 subject tonum. of constraints = 3dim. of socp var = 4, num. of socp blk = 1dim. of linear var = 3*******************************************************************SDPT3: Infeasible path-following algorithms*******************************************************************version predcorr gam expon scale_dataNT 1 0.000 1 0it pstep dstep pinfeas dinfeas gap prim-obj dual-obj cputime0|0.000|0.000|8.0e-001|6.5e+000|3.1e+002| 1.000000e+001 0.000000e+000| 0:0:00| chol 1 1 1|1.000|0.987|4.3e-007|1.5e-001|1.6e+001| 9.043148e+000 -2.714056e-001| 0:0:01| chol 1 1 2|1.000|1.000|2.6e-007|7.6e-003|1.4e+000| 1.234938e+000 -5.011630e-002| 0:0:01| chol 1 1 3|1.000|1.000|2.4e-007|7.6e-004|3.0e-001| 4.166959e-001 1.181563e-001| 0:0:01| chol4|0.892|0.877|6.4e-008|1.6e-004|5.2e-002| 2.773022e-001 2.265122e-001| 0:0:01| chol5|1.000|1.000|1.0e-008|7.6e-006|1.5e-002| 2.579468e-001 2.427203e-001| 0:0:01| chol6|0.905|0.904|3.1e-009|1.4e-006|2.3e-003| 2.511936e-001 2.488619e-001| 0:0:01| chol7|1.000|1.000|6.1e-009|7.7e-008|6.6e-004| 2.503336e-001 2.496718e-001| 0:0:01| chol8|0.903|0.903|1.8e-009|1.5e-008|1.0e-004| 2.500507e-001 2.499497e-001| 0:0:01| chol9|1.000|1.000|4.9e-010|3.5e-010|2.9e-005| 2.500143e-001 2.499857e-001| 0:0:01| chol10|0.904|0.904|5.7e-011|1.3e-010|4.4e-006| 2.500022e-001 2.499978e-001| 0:0:01| chol11|1.000|1.000|5.2e-013|1.1e-011|1.2e-006| 2.500006e-001 2.499994e-001| 0:0:01| chol12|1.000|1.000|5.9e-013|1.0e-012|1.8e-007| 2.500001e-001 2.499999e-001| 0:0:01| chol13|1.000|1.000|1.7e-012|1.0e-012|4.2e-008| 2.500000e-001 2.500000e-001| 0:0:01| chol14|1.000|1.000|2.3e-012|1.0e-012|7.3e-009| 2.500000e-001 2.500000e-001| 0:0:01|stop: max(relative gap, infeasibilities) < 1.49e-008number of iterations = 14primal objective value = 2.50000004e-001dual objective value = 2.49999996e-001gap := trace(XZ) = 7.29e-009relative gap = 4.86e-009actual relative gap = 4.86e-009rel. primal infeas (scaled problem) = 2.33e-012rel. dual " " " = 1.00e-012rel. primal infeas (unscaled problem) = 0.00e+000rel. dual " " " = 0.00e+000norm(X), norm(y), norm(Z) = 3.2e+000, 1.5e+000, 1.9e+000norm(A), norm(b), norm(C) = 3.9e+000, 4.2e+000, 2.6e+000 Total CPU time (secs) = 0.99CPU time per iteration = 0.07termination code = 0DIMACS: 3.3e-012 0.0e+000 1.3e-012 0.0e+000 4.9e-009 4.9e-009Status: SolvedOptimal value (cvx_optval): -32. 程序代码:n=3; a=[-3 -1 -3];b=[2;5;6];C=[2 1 1;1 2 3;2 2 1]; cvx_beginvariable x(n) minimize( a*x)subject toC * x <= bx>=0 cvx_end 运行结果:Calling SDPT3 4.0: 6 variables, 3 equality constraintsnum. of constraints = 3dim. of linear var = 6*******************************************************************SDPT3: Infeasible path-following algorithms*******************************************************************version predcorr gam expon scale_dataNT 1 0.000 1 0it pstep dstep pinfeas dinfeas gap prim-obj dual-obj cputime0|0.000|0.000|1.1e+001|5.1e+000|6.0e+002|-7.000000e+001 0.000000e+000| 0:0:00| chol 1 1 1|0.912|1.000|9.4e-001|4.6e-002|6.5e+001|-5.606627e+000 -2.967567e+001| 0:0:00| chol 1 1 2|1.000|1.000|1.3e-007|4.6e-003|8.5e+000|-2.723981e+000 -1.113509e+001| 0:0:00| chol 1 1 3|1.000|0.961|2.3e-008|6.2e-004|1.8e+000|-4.348354e+000 -6.122853e+000| 0:0:00| chol 1 1 4|0.881|1.000|2.2e-008|4.6e-005|3.7e-001|-5.255152e+000 -5.622375e+000| 0:0:00| chol 1 1 5|0.995|0.962|1.6e-009|6.2e-006|1.5e-002|-5.394782e+000 -5.409213e+000| 0:0:00| chol 1 1 6|0.989|0.989|2.7e-010|5.2e-007|1.7e-004|-5.399940e+000 -5.400100e+000| 0:0:00| chol 1 1 7|0.989|0.989|5.3e-011|5.8e-009|1.8e-006|-5.399999e+000 -5.400001e+000| 0:0:00| chol 1 1 8|1.000|0.994|2.8e-013|4.3e-011|2.7e-008|-5.400000e+000 -5.400000e+000| 0:0:00|stop: max(relative gap, infeasibilities) < 1.49e-008 number of iterations = 8primal objective value = -5.39999999e+000 dual objective value = -5.40000002e+000 gap := trace(XZ) = 2.66e-008 relative gap = 2.26e-009 actual relative gap = 2.21e-009 rel. primal infeas (scaled problem) = 2.77e-013 rel. dual " " " = 4.31e-011 rel. primal infeas (unscaled problem) = 0.00e+000norm(X), norm(y), norm(Z) = 4.3e+000, 1.3e+000, 1.9e+000norm(A), norm(b), norm(C) = 6.7e+000, 9.1e+000, 5.4e+000Total CPU time (secs) = 0.11CPU time per iteration = 0.01termination code = 0DIMACS: 3.6e-013 0.0e+000 5.8e-011 0.0e+000 2.2e-009 2.3e-009Status: Solvedrel. dual = 0.00e+000Optimal value (cvx_optval): -5.4。
高中数学必修一《优化方案》答案 第一章
错误!1.1.1集合的含义与表示[读教材·填要点]1.元素与集合(1)元素与集合的定义:一般地,把研究对象统称为元素,把一些元素组成的总体叫做集合(简称为集).(2)集合中元素的性质:①确定性:即给定的集合,它的元素是确定的.②互异性:即给定集合的元素是互不相同的.③无序性.(3)集合相等:只要构成两个集合的元素是一样的,就称这两个集合是相等的.(4)元素与集合的关系:a是集合A的元素,记作a∈A,a不是集合A的元素,记作a∉A.2.集合的表示方法除了用自然语言表示集合外,还可以用列举法和描述法表示集合.(1)列举法:把集合中的元素一一列举出来,并用花括号“{}”括起来表示集合的方法.(2)描述法:用集合所含元素的共同特征表示集合的方法.3.常用数集及其记法1.著名数学家能否构成一个集合?提示:不能,没有一定的评定标准,故著名数学家是不确定的对象,所以不能构成集合.2.一个集合能表示成{s,k,t,k}吗?提示:不能,集合中的元素是互不相同的,任何两个相同的对象在同一个集合中,只能算作这个集合的一个元素.3.集合{-5,-8}和{(-5,-8)}是同一集合吗?提示:不是同一集合.集合{-5,-8}中元素有2个,为数.而集合{(-5,-8)}中有一个元素为坐标(-5,-8).[例1](1)某校2013年在校的所有高个子同学;(2)不超过20的非负数;(3)帅哥;(4)直角坐标系平面内第一象限的一些点;(5)错误!的近似值的全体.[自主解答]“高个子"没有明确的标准,因此(1)不能构成集合.(2)任给一个实数x,可以明确地判断是不是“不超过20的非负数”,即“0≤x≤20"与“x〉20或x<0”,两者必居其一,且仅居其一,故“不超过20的非负数”能构成集合;(3)“帅哥"没有一个明确的标准,不能构成集合;(4)“一些点”无明确的标准,对于某个点是否在“一些点”中无法确定,因此“直角坐标平面内第一象限的一些点”不能构成集合;(6)“错误!的近似值"不明确精确到什么程度,因此很难判断一个数如“2”是不是它的近似值,所以(5)不能构成集合.——-——-—----—-———--判断指定的对象能不能构成集合,关键在于能否找到一个明确标准,对于任何一个对象,都能确定它是不是给定集合的元素,同时还要注意集合中元素的互异性、无序性.——--———————-———-————-—-—--—-———-————-——-1.下列能构成集合的是()A.中央电视台著名节目主持人B.2013年沈阳全运会比赛的所有项目C.2010年上海世博园中所有漂亮的展馆D.世界上的高楼答案:B[例2]a的值.[自主解答]若a+2=1,则a=-1,所以A={1,0,1},与集合中元素的互异性矛盾,应舍去;若(a+1)2=1,则a=0或a=-2,当a=0时,A={2,1,3},满足题意.当a=-2时,A={0,1,1},与集合中元素的互异性矛盾,舍去;若a2+3a+3=1,则a=-1或a=-2(均舍去).综上可知,a=0.例2中1∈A改为4∈A,则结果如何?解:若a+2=4,则a=2.∴A={4,9,13}满足题意.若(a+1)2=4,则a=1或a=-3。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第一题(牛顿法和不精确一维搜索)牛顿法:syms x1 x2;f=(x2-(x1)*(x1))^2+(1-x1)^2;v=[x1,x2];df=jacobian(f,v);df=df.';G=jacobian(df,v);epson=1e-12;x0=[0,1]';g1=subs(df,{x1,x2},{x0(1,1),x0(2,1)});G1=subs(G,{x1,x2},{x 0(1,1),x0(2,1)});k=0;mul_count=0;sum_count=0;mul_count=mul_count+12;sum_count=sum_count+6;while(norm(g1)>epson)p=-G1\g1;x0=x0+p;g1=subs(df,{x1,x2},{x0(1,1),x0(2,1)});G1=subs(G,{x1,x2},{x0(1,1),x0(2,1)});k=k+1;mul_count=mul_count+16;sum_count=sum_count+11;end;kx0mul_countsum_count运行结果:k =9x0 =11mul_count =156sum_count =105不精确一维搜索:fun1.m文件function f=fun1(x)f=(x(1)-1)^2+(x(2)-x(1)^2)^2;gfun1.m文件function gf = gfun1(x)gf=[2*x(1)*(x(1)^2-x(2))+2*(x(1)-1),2*(x(2)-x(1)^2)]';wolfepowell.m文件function [k,m,opt,x]=wolfepowell(xk,sk)max=1000;c1=0.1; c2=0.6;a=0;b=inf;dk=0.2;m=0;while(m<=max)if (fun1(xk)-fun1(xk+sk*dk)<-c1*dk*gfun1(xk)'*sk)b=dk;dk=(dk+a)/2;elseif(fun1(xk)-fun1(xk+sk*dk)>=-c1*dk*gfun1(xk)'*sk)&&(gfun1(xk+sk*dk)'*sk<c2*gf un1(xk)'*sk);a=dk;if 2*dk<(dk+b)/2dk=2*dk;elsedk=(dk+b)/2;endelsemk=m;break;endm=m+1;endm=mk;k=dk;x=xk+sk*dk;opt=fun1(x);运行结果:>>xk=[0,1]';sk=[1,-1]';[k,m,opt,x]= wolfepowell(xk,sk)k =0.8000m =2opt =1.0080x =0.80000.2000第二题(共轭梯度法)frcg.m文件function [x,val,k]=frcg(fun,gfun,x0)maxk=5000;rho=0.6;sigma=0.4;k=0; epsilon=1e-4;n=length(x0);while(k<maxk)g=feval(gfun,x0);itern=k-(n+1)*floor(k/(n+1));itern=itern+1;if(itern==1)d=-g;elsebeta=(g'*g)/(g0'*g0);d=-g+beta*d0; gd=g'*d;if(gd>=0.0)d=-g;endendif(norm(g)<epsilon), break; endm=0; mk=0;while(m<20)if(feval(fun,x0+rho^m*d)<feval(fun,x0)+sigma*rho^m*g'*d)mk=m; break;endm=m+1;endx0=x0+rho^mk*d;val=feval(fun,x0);g0=g; d0=d;k=k+1;endx=x0;val=feval(fun,x);fun.m文件function f=fun(x)f=x(1)^2-2*x(1)*x(2)+2*x(2)^2+x(3)^2+x(4)^2-x(2)*x(3)+2*x(1)+3*x(2)-x(3); gfun.m文件function gf=gfun(x)gf=[2*x(1)-2*x(2)+2, -2*x(1)+4*x(2)+3,2*x(3)-x(2)-1,2*x(4)]';运行结果:>>x0=[0 0 0 0]';[x,val,k]=frcg('fun','gfun',x0)x =-3.4064-2.6515-0.7032val =-7.8338k =5000第三题(BFGS法、最速下降法和牛顿法)fun3.m文件function f=fun3(x)f=(x(1)-1)^2+5*(x(2)-x(1)^2)^2;gfun3.m文件function gf = gfun3(x)gf=[20*x(1)*(x(1)^2-x(2))+2*(x(1)-1),10*(x(2)-x(1)^2)]'; Hessen.m文件function Hess=Hessen(x)Hess=[60*x(1)^2-20*x(2)+2,(-20)*x(1);(-20)*x(1),10];Steepest.m文件function [m,opt,x] = Steepest(xk)m=0;Eps=1.0e-4;max=1000;while(m<max)g=gfun3(xk);sk=-g;dk=wolfepowell(xk,sk);x=xk+dk*sk;g0=gfun3(x);if(norm(g0)<=Eps)break;elsexk=x;endm=m+1;endmk=m;x=xk;opt=fun3(xk);newton.m文件function [x,opt,mk] = newton(xk)m=0;Eps=1e-4;max=100;H=[1,0;0,1];while(m<max)g=gfun3(xk);M=Hess(xk);H=inv(M);sk=-H*g;dk=wolfepowell(xk,sk);x=xk+dk*sk;g0=gfun3(x);if(norm(g0)<=Eps)break;elsexk=x;endm=m+1;endmk=m;x=xk;opt=fun3(xk);bfgs.m文件function [x,opt,mk] = bfgs(xk)m=0;Eps=1e-4;max=100;H=[1,0;0,1];while(m<max)g=gfun3(xk);sk=-H*g;dk=wolfepowell(xk,sk);x=xk+dk*sk;g0=gfun3(x);if(norm(g0)<=Eps)break;elseH=H+((1+((g0-g)'*H*(g0-g))/((x-xk)'*(g0-g)))*(x-xk)*(x-xk)'-H*(g0-g)*(x-xk)'-(x-x k)*(g0-g)'*H)/((x-xk)'*(g0-g));xk=x;endm=m+1;endmk=m;x=xk;opt=fun3(xk);wolfepowell.m文件function [k,m,opt,x]=wolfepowell(xk,sk)max=1000;c1=0.1; c2=0.6;a=0;b=inf;dk=0.2;m=0;while(m<=max)if (fun3(xk)-fun3(xk+sk*dk)<-c1*dk*gfun3(xk)'*sk)b=dk;dk=(dk+a)/2;elseif(fun3(xk)-fun3(xk+sk*dk)>=-c1*dk*gfun3(xk)'*sk)&&(gfun3(xk+sk*dk)'*sk<c2*gf un3(xk)'*sk);a=dk;if 2*dk<(dk+b)/2dk=2*dk;elsedk=(dk+b)/2;endelsemk=m;break;endm=m+1;endm=mk;k=dk;x=xk+sk*dk;opt=fun3(x);输入参数:牛顿法运行结果:>> xk=[2,0]';[x,opt,mk] = newton(xk)x =1.00001.0000opt =3.2601e-10mk =12最速下降法运行结果:>>xk=[2,0]';[x,opt,mk] = Steepest(xk)x =0.99990.9998opt =8.2108e-09mk =354BFGS法运行结果:>>xk=[2,0]';[x,opt,mk] = bfgs(xk)x =0.99990.9999opt =4.7725e-09mk =16(0.9999, 0.9998) 最速下降法(迭代次数354)即得数值最优点为x= (1.0000, 1.0000) 牛顿法(迭代次数12)(0.9999, 0.9999) BFGS公式(迭代次数16)最优解近似为0第四题(乘子法)multphr.m文件function [x,mu,lambda,output]=multphr(fun,hf,gf,dfun,dhf,dgf,x0)maxk=500;sigma=2.0;eta=2.0; theta=0.8;k=0; ink=0;epsilon=1e-5;x=x0; he=feval(hf,x); gi=feval(gf,x);n=length(x); l=length(he); m=length(gi);mu=0.1*ones(l,1); lambda=0.1*ones(m,1);btak=10; btaold=10;while(btak>epsilon && k<maxk)[x,~,ik]=bfgs('mpsi','dmpsi',x0,fun,hf,gf,dfun,dhf,dgf,mu,lambda,sigma);ink=ink+ik;he=feval(hf,x); gi=feval(gf,x);btak=0.0;for (i=1:l), btak=btak+he(i)^2; endfor (i=1:m);temp=min(gi(i),lambda(i)/sigma);btak=btak+temp^2;endbtak=sqrt(btak);if(btak>epsilon)if(k>=2&&btak> theta*btaold)sigma=eta*sigma;endfor (i=1:l), mu(i)=mu(i)-sigma*he(i); endfor (i=1:m)lambda(i)=max(0.0,lambda(i)-sigma*gi(i));endendk=k+1;btaold=btak;x0=x;endf=feval(fun,x);output.fval=f;output.iter=k;output.inner_iter=ink;output.bta=btak;mpsi.m文件function psi=mpsi(x,fun,hf,gf,dfun,dhf,dgf,mu,lambda,sigma) f=feval(fun,x); he=feval(hf,x); gi=feval(gf,x);l=length(he); m=length(gi);psi=f; s1=0.0;for(i=1:l)psi=psi-he(i)*mu(i);s1=s1+he(i)^2;endpsi=psi+0.5*sigma*s1;s2=0.0;for(i=1:m)s3=max(0.0, lambda(i) - sigma*gi(i));s2=s2+s3^2-lambda(i)^2;endpsi=psi+s2/(2.0*sigma);h1.m文件function he=h1(x)he=-x(1)^2-x(2)^2+25.0;f1.m文件function f=f1(x)f=4*x(1)-x(2)^2-12;g1.m文件function gi=g1(x)gi=10*x(1)-x(1)^2+10*x(2)-x(2)^2-34;h1.m文件function dhe = dh1(x)dhe = [-1*x(1), -1*x(2)]';dg1.m文件function dgi = dg1(x)dgi = [10-2*x(1), 10-2*x(2)]';df1.m文件function g=df1(x)g = [4, -2.0*x(2)]';bfgs.m文件function [x,val,k]=bfgs(fun,gfun,x0,varargin)maxk=500;rho=0.55; sigma1=0.4; epsilon1=1e-5;k=0; n=length(x0);Bk=eye(n);while(k<maxk)gk=feval(gfun,x0,varargin{:});if(norm(gk)<epsilon1), break; enddk=-Bk\gk;m=0; mk=0;while(m<20)newf=feval(fun,x0+rho^m*dk,varargin{:});oldf=feval(fun,x0,varargin{:});if(newf<oldf+sigma1*rho^m*gk'*dk)mk=m; break;endm=m+1;endx=x0+rho^mk*dk;sk=x-x0; yk=feval(gfun,x,varargin{:})-gk;if(yk'*sk>0)Bk=Bk-(Bk*sk*sk'*Bk)/(sk'*Bk*sk)+(yk*yk')/(yk'*sk);endk=k+1; x0=x;endval=feval(fun,x0,varargin{:});dmpsi.m文件function dpsi=dmpsi(x,fun,hf,gf,dfun,dhf,dgf,mu,lambda,sigma) dpsi=feval(dfun,x);he=feval(hf,x); gi=feval(gf,x);dhe=feval(dhf,x); dgi=feval(dgf,x);l=length(he); m=length(gi);for(i=1:l)dpsi=dpsi+(sigma*he(i)-mu(i))*dhe(:,i);endfor(i=1:m)dpsi=dpsi+(sigma*gi(i)-lambda(i))*dgi(:,i);end运行结果:>>x0=[1,1]';[x,mu,lambda,output]=multphr('f1','h1','g1','df1','dh1','dg1',x0) x =1.00134.8987mu =2.0312lambda =0.7545output =fval: -31.9923iter: 5inner_iter: 58bta: 4.3187e-07第五题(有效集法)qpact.m文件function [x,lamk,exitflag,output]=qpact(H,c,Ae,be,Ai,bi,x0) epsilon=1.0e-9; err=1.0e-6;k=0; x=x0; n=length(x); kmax=1.0e3;ne=length(be); ni=length(bi); lamk=zeros(ne+ni,1);index=ones(ni,1);for (i=1:ni)if(Ai(i,:)*x>bi(i)+epsilon), index(i)=0; endendwhile (k<=kmax)Aee=[ ];if(ne>0), Aee=Ae; endfor(j=1:ni)if(index(j)>0), Aee=[Aee; Ai(j,:)]; endendgk=H*x+c;[m1,n1] = size(Aee);[dk,lamk]=qsubp(H,gk,Aee,zeros(m1,1));if(norm(dk)<=err)y=0.0;if(length(lamk)>ne)[y,jk]=min(lamk(ne+1:length(lamk))); endif(y>=0)exitflag=0;elseexitflag=1;for(i=1:ni)if(index(i) & (ne+sum(index(1:i)))==jk)index(i)=0; break;endendendk=k+1;elseexitflag=1;alpha=1.0; tm=1.0;for(i=1:ni)if((index(i)==0)&(Ai(i,:)*dk<0))tm1=(bi(i)-Ai(i,:)*x)/(Ai(i,:)*dk); if(tm1<tm)tm=tm1; ti=i;endendendalpha=min(alpha,tm);x = x+alpha*dk;if(tm<1), index(ti)=1; endendif(exitflag==0), break; endk=k+1;endoutput.fval=0.5*x'*H*x+c'*x;output.iter=k;qsubp.m文件function [x,lambda]=qsubp(H,c,Ae,be)ginvH=pinv(H);[m,n]=size(Ae);if (m>0)rb = Ae*ginvH*c + be;lambda = pinv(Ae*ginvH*Ae')*rb;x = ginvH*(Ae'*lambda-c);elsex = -ginvH*c;lambda = zeros(m,1);endcallqpact.m文件function callqpactH=[2 0; 0 2];c=[-2 -5]';Ae=[ ]; be=[ ];Ai=[1 -2; -1 -2; -1 2;1 0;0 1];bi=[-2 -6 -2 0 0]';x0=[0 0]';[x, lambda, exitflag,output]=qpact(H,c,Ae,be,Ai,bi,x0)运行结果:>>callqpactx =1.40001.7000lambda =0.8000exitflag =output =fval: -6.4500iter: 7第七题fun文件function f=fun(x)f=abs(x(1))+abs(x(2))+abs(x(3))+abs(x(4));主程序x0=[1;1;1;1];A=[]; b=[];Aeq=[1 0 0 0.5;0 1 0.2 0.3;0 0.1 1 0.2];beq=[-1;0.2;1]; VLB=[]; VUB=[];[x,fval]=fmincon('fun',x0,A,b,Aeq,beq,VLB,VUB)运行结果x =-1.0000-0.00001.00000.0000 fval =2.0000。