第一篇:matlab程序设计与应用(第二版)第三章部分课后答案解析
第三章1.(1A=eye(3(2A=100+100*rand(5,6(3A=1+sqrt(0.2*randn(10,50(4B=ones(size(A(5A+30*eye(size(A(6B=diag(diag(A 2.B=rot90(A C=rot90(A,-1 3.B=inv(A;A的逆矩阵 C=det(A;A的行列式的值 D=A*B E=B*A D=E 因此A与A-1是互逆的。4.A=[4 2-1;3-1 2;12 3 0];b=[2;10;8];
x=inv(A*b x =-6.0000 26.6667 27.3333 5.(1 diag(A;主对角线元素 ans = 1 1 5 9 triu(A;上三角阵 ans = 1-1 2 3 0 1-4 2 0 0 5 2
0 0 0 9 tril(A;下三角阵 ans = 1 0 0 0 5 1 0 0 3 0 5 0 11 15 0 9 rank(A;秩 ans = 4 norm(A;范数 ans = 21.3005 cond(A;条件数 ans = 11.1739 trace(A;迹 ans = 16(2略
6.A=[1 1 0.5;1 1 0.25;0.5 0.25 2] A = 1.0000 1.0000 0.5000 1.0000 1.0000 0.2500 0.5000 0.2500 2.0000 [V,D]=eig(A V = 0.7212 0.4443 0.5315-0.6863 0.5621 0.4615-0.0937-0.6976 0.7103 D =-0.0166 0 0 0 1.4801 0 0 0 2.5365
第二篇:C#应用程序设计教程 第二版+课后习题答案
第一章
1.判断题
(1)×(2)√(3)√(4)×(5)×
(6)√ 2.选择题
(1)C(2)B(3)B(4)C(5)D
(6)C 3.编程题
using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace ConsoleApplication1 { class Program { static void Main(string[] args){ Console.WriteLine(“Hello C#”);} } }
第二章
1.判断下列符号哪些是合法的C#变量名称
北京 abc _xyz x_y 2.根据要求写出C#表达式(1)N%3==0 & N%5==0(2)(char)new Random().Next(67,74)(3)Math.Pow(x,3)*y/(x+y)(4)Math.Sqrt(b*b-4*a*c)3.判断题
(1)×(2)√(3)√(4)√(5)√
(6)×(7)√(8)√(9)√(10)×(11)√(12)× 4.选择题
(1)B(2)C(3)B(4)B
(5)C
(6)B(7)D(8)B(9)C(10)A 5.分析并写出下列程序运行结果(1)a=21,b=9,c=16
y=4(2)b=41(3)s=6(4)a[0]=28
a[1]=15
a[2]=39
a[3]=48
a[4]=39 6.程序填空
(1)① 3 ② a[i] ③ a[i] ④ a[5-i] ⑤ a[5-i](2)① i % 5 == 0 | i % 7 == 0 ② s+ i 7.编程题(1)static void Main(string[] args){ int s,i,j;for(i = 2;i < 1000;i++){ s = 0;for(j = 1;j < i;j++){ if(i % j == 0)s += j;} if(i == s)Console.WriteLine(“{0}”,i);} }(2)static void Main(string[] args){ int[,] a = {{2,5,18,4 },{3,4,9,2},{4,1,16,8},{5,2,14,6}};int i,j,k,max,maxj;for(i = 0;i < 4;i++){ max = a[i,0];maxj = 0;for(j = 1;j < 4;j++){ if(max < a[i,j]){max = a[i,j];maxj = j;} } for(k = 0;k < 4;k++){ if(a[k, maxj] < max)break;} if(k == 4)Console.WriteLine(“鞍点:第{0}行,第{1}列”,i+1,maxj+1);} }(3)
static void Main(string[] args){ int a = 20, b = 16,i=20;do { if(i % a == 0 & i % b == 0)break;i++;} while(true);Console.WriteLine(“最小公倍数:{0}”,i);i = 16;do { if(a % i == 0 & b % i == 0)break;i--;} while(true);Console.WriteLine(“最大公约数:{0}”,i);}(4)
static void Main(string[] args){ int i, j,k;float s = 1.0f;for(i = 2;i < 11;i++){ k=1;for(j = 2;j <= i;j++)k += j;s += 1.0f / k;} Console.WriteLine(“s={0}”,s);}(5)
static void Main(string[] args){ String s = “a,b2>4[AG6p(”;int i, n1=0,n2=0,n3=0,n4=0;for(i=0;i
static void Main(string[] args){ int i,s = 1;for(i = 0;i < 5;i++){ s = 2 *(s + 1);} Console.WriteLine(“小猴子第一天摘了{0}个桃子”,s);}
说明:由最后一天往前推算。后一天吃了前一天桃子数量的一半多1个,剩余桃子数量是前一天桃子数量的一半减1,则,前一天的桃子数量是后一天的桃子数量加1的2倍。
第三章
1.判断题
(1)√(2)×(3)√
(4)√(5)√(6)×(7)√(8)×(9)×
(10)√
(11)√(12)√(13)√(14)×(15)√(16)√(17)×(18)√(19)√(20)×(21)×(22)√(23)√(24)√(25)×(26)×(27)× 2.选择题
(1)C(2)B(3)C(4)D
(5)C
(6)B(7)D(8)C(9)A(10)A(11)B(12)A(13)D(14)B
(15)C
3.分析下列程序的运行结果(1)s=32,s1=32,s2=34
x=11,y=21(2)x1=3,x2=4
y1=1,y2=4 4.编程题(1)
class Student { public string studentid;public string studentname;private string birthplace;private DateTime birthdate;public Student(string id, string name){ studentid = id;studentname = name;} public string StudentId { get { return studentid;} set { studentid = value;} } public string StudentName { get { return studentname;} set { studentname = value;} } public string BirthPlace { get { return birthplace;} set { birthplace = value;} } public DateTime BirthDate { get { return birthdate;} set { birthdate = value;} } public int Age { get { return DateTime.Now.Year1, p1.yp1.x)*(p2.xp1.y)*(p2.yv2;if(radioButton3.Checked ==true)v = v1 * v2;if(radioButton4.Checked ==true){ if(v2!= 0)v = v1 / v2;} textBox3.Text = v.ToString();} 3.利用4.6节知识,参考图4-29设计具有菜单、工具栏和状态栏的Windows窗口应用程序。
第五章
1.判断题
(1)√(2)×(3)√(4)√(5)×
(6)√(7)×
2.选择题
(1)A(2)无答案,应该是TextReader(3)A(4)C
(5)A(6)C 3.编程题
创建Windows窗口用于程序,在窗口上放置一个Button控件,名称为button1,编写其Click事件处理方法:
private void button1_Click(object sender, EventArgs e){ int i, j;int[,] a=new int[3,3];int[,] b = new int[3, 3];int[,] c = new int[3, 3];string s1;string[] ss;FileStream fs = new FileStream(@“d:data1.txt”,FileMode.Open);StreamReader sr=new StreamReader(fs,Encoding.Default);StreamWriter sw;BinaryWriter bw;for(i = 0;i < 3;i++){ s1 = sr.ReadLine();ss = s1.Split(' ');for(j = 0;j < 3;j++){ a[i, j] = int.Parse(ss[j]);} } for(i = 0;i < 3;i++){ s1 = sr.ReadLine();ss = s1.Split(' ');for(j = 0;j < 3;j++){ b[i, j] = int.Parse(ss[j]);} } sr.Close();fs.Close();for(i = 0;i < 3;i++)for(j = 0;j < 3;j++)c[i, j] = a[i, j] + b[i, j];SaveFileDialog sfd = new SaveFileDialog();sfd.Filter = “文本文件(*.txt)|*.txt”;if(sfd.ShowDialog()== DialogResult.OK){ fs = new FileStream(sfd.FileName, FileMode.OpenOrCreate);sw = new StreamWriter(fs,Encoding.Default);for(i = 0;i < 3;i++){ for(j = 0;j < 3;j++){ sw.Write(c[i, j]);sw.Write(“ ”);} sw.Write(“n”);} sw.Close();fs.Close();} sfd.Filter = “二进制文件(*.bin)|*.bin”;if(sfd.ShowDialog()== DialogResult.OK){ fs = new FileStream(sfd.FileName, FileMode.OpenOrCreate);bw = new BinaryWriter(fs);for(i = 0;i < 3;i++)for(j = 0;j < 3;j++)bw.Write(c[i, j]);bw.Close();fs.Close();} } 说明:data1.txt文件中共9行整数,每行3个整数,每个数据之间以一个空格隔开。
第六章
1.判断题
(1)√(2)×(3)×(4)×(5)√
(6)√(7)√(8)×(9)×(10)√
2.选择题
(1)C(2)D(3)C(4)A
(5)B(6)C(7)B(8)D 3.编程题
创建Windows窗口应用程序,在窗口上放置一个toolStrip控件,并在其中添加两个组合框,名称分别为toolStripComboBox1和toolStripComboBox2,给toolStripComboBox1添加选项:短划线、点划线、双点划线、虚线、实线,给toolStripComboBox2添加选项:1、2、3、4。给窗口添加如下私有字段:
private Graphics g1;private DashStyle dashstyle=DashStyle.Solid;private float penwidth=1;private Pen pen1;private bool beDraw = false;private Point p1, p2;编写comboBox1和comboBox2的SelectedIndexChanged事件处理方法:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e){ dashstyle =(DashStyle)comboBox1.SelectedIndex;}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e){ penwidth =float.Parse(comboBox2.Text);} 编写Form3的Load、MouseDown、MouseMove和MouseUp以及工具栏上两个组合框的SelectedIndexChanged事件处理方法:
private void Form3_Load(object sender, EventArgs e){ toolStripComboBox1.SelectedIndex = toolStripComboBox1.Items.Count-1;toolStripComboBox2.SelectedIndex = 0;} private void Form3_MouseDown(object sender, MouseEventArgs e){ pen1 = new Pen(Color.Red, penwidth);pen1.DashStyle = dashstyle;beDraw = true;p1 = e.Location;g1 = this.CreateGraphics();} private void Form3_MouseMove(object sender, MouseEventArgs e){ if(beDraw == true){ p2 = e.Location;g1.DrawLine(pen1, p1, p2);p1 = p2;} } private void Form3_MouseUp(object sender, MouseEventArgs e){ beDraw = false;} private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e){ dashstyle =(DashStyle)toolStripComboBox1.SelectedIndex;} private void toolStripComboBox2_SelectedIndexChanged(object sender, EventArgs e){ penwidth = float.Parse(toolStripComboBox2.Text);}
第七章
1.判断题
(1)√(2)×(3)×(4)×(5)√
(6)×(7)√(8)×(9)√(10)×(11)√(12)×(13)√(14)√(15)√(16)√
2.选择题
(1)A(2)D(3)C(4)A
(5)D(6)B(7)C(8)D(9)B(10)C(11)C 12)C 13)A 14)B 15)D 16)D 3.编程题
(1)创建Windows窗口应用程序,在窗口上放置两个Button控件,名称分别为button1和button2,再放置一个ComboBox控件和DataGridView控件,名称分别为comboBox1和dataGridView1。
编写button1的Click事件处理方法:
private void button1_Click(object sender, EventArgs e){ OleDbConnection con = new OleDbConnection();OleDbCommand com = new OleDbCommand();con.ConnectionString =@“Provider=microsoft.jet.oledb.4.0;data source=d:aaa.mdb”;con.Open();com.CommandType = CommandType.Text;com.CommandText = “select * from Score where score>80”;com.Connection = con;OleDbDataReader dr = com.ExecuteReader();comboBox1.Items.Clear();while(dr.Read()== true){ if(dr.IsDBNull(dr.GetOrdinal(“StudentID”))== false)
comboBox1.Items.Add(dr[“StudentID”]);} } 说明:为了使用ADO.NET的OLEDB访问方式,需要添加下列引用:
using System.Data.OleDb;(2)界面与(1)相同。
编写button2的Click事件处理方法:
private void button2_Click(object sender, EventArgs e){ OleDbConnection con = new OleDbConnection();OleDbCommand com = new OleDbCommand();con.ConnectionString = @“Provider=microsoft.jet.oledb.4.0;data source=d:aaa.mdb”;con.Open();OleDbDataAdapter da = new OleDbDataAdapter(“select Class as 班级,avg(Score)as平均成绩 from Score group by Class”, con);DataSet ds = new DataSet();da.Fill(ds, “平均成绩表”);dataGridView1.DataSource =ds.Tables[“平均成绩表”];}(3)参考7.7节的学生信息管理程序,编写通讯录管理程序,细节略。
第三篇:离散数学及其应用集合论部分课后习题答案
作业答案:集合论部分
P90:习题六
5、确定下列命题是否为真。(2)(4){}
(6){a,b}{a,b,c,{a,b}} 解答:(2)假(4)真(6)真
8、求下列集合的幂集。(5){{1,2},{2,1,1},{2,1,1,2}}(6){{,2},{2}} 解答:
(5)集合的元素彼此互不相同,所以{2,1,1,2}{1,2},所以该题的结论应该为
{,{{1,2}},{{2,1,1}},{{1,2},{2,1,1}}}
(6){,{{,2}},{{2}},{{,2},{2}}}
9、设E{1,2,3,4,5,6},A{1,4},B{1,2,5},C{2,4},求下列集合。(1)A(2)解答:(1)A(2)
31、设A,B,C为任意集合,证明 B
(AB)
B{1,4}{3,4,6}{4}
(AB){1}{2,3,4,5,6}
(AB)证明:
(BA)(AB)(AB)
(AB)(BA){x|xABxBA}{x|(xAxB)(xBxA)}{x|(xAxB)(xBxB)(xAxA)(xBxA)} {x|(xAxB)(xBxA)}{x|(xA{x|(xAA
B)(xAxB)}{x|(xAB)(xABB)}{x|(xAB)(xAxB)}B)}B)(xABA34、设A,B为集合,证明:如果(AB)证明:(反证法)
设aA(BA)AB,则AB。
B,则aA,aB,所以aAB,aBA; 所以a(AB)但是aA与(AB)
37、设A,B,C为任意集合,证明:CACBC(A证明:
对任意xC,由于CA,CB,所以xA且xB所以xA因此,C(A
(BA)
B矛盾。
B)。
B B。
(BA)AB)。
P121:习题七
5、设A,B为任意集合,证明
若AABB,则AB。
证明:
xAx,xAA
x,xBBxB所以有AB
9、设A{1,2,4,6},列出下列关系R(2)R{x,y|x,yA|xy|1}(3)R{x,y|x,yAy为素数} 解答:
11、Ri是X上的二元关系,对于xX定义集合(2)R{1,2,2,1}
(3)R{1,2,2,2,4,2,6,2}
Ri(x){y|xRy}
显然Ri(x)X。如果X{4,3,2,1,0,1,2,3,4},且令
R1{x,y|x,yXxy}
R2{x,y|x,yXy1xy2} R3{x,y|x,yXx2y}
求R1(0),R1(1),R2(0),R2(1),R3(3)。解答:
R1(0){1,2,3,4}R1(1){2,3,4}R2(0){1,0}R2(1){2,1}R3(3),B{1,3,2,4,4,2}。求A13、设A{1,2,2,4,3,3}
B,AB,domA,domB,dom(A解答:
B),ranA,ranB,ran(AB),fld(AB).AAB{1,2,2,4,3,3,1,3,4,2} B{2,4}
domA{1,2,3} domB{1,2,4} dom(AB){1,2,3,4}
ranA{2,3,4} ranB{2,3,4} ran(A B){4}
fld(AB){1,2,3}
16、设A{a,b,c,d},R1,R2为A上的关系,其中
R1{a,a,a,b,b,d},R2{a,d,b,c,b,d,c,b}。求R1R2,R2R1,R12,R23。
解答:
R1R2{a,d,a,c,a,d} R2R1{c,d}
R12{a,a,a,b,a,d} R22{b,b,c,c,c,d} R23{b,c,b,d,c,b}
20、给定A{1,2,3,4},A上的关系R{1,3,1,4,2,3,2,4,3,4}(1)画出R的关系图。(2)说明R的性质。解答:
(1)
(2)R具有反自反性,反对称性,传递性
21、设A{1,2,3},图7.11给出12种A上的关系,对于每种关系写出相应的关系矩阵,并说明它所具有的性质。
解答:
110(a)111,具有自反性。101110(b)001,具有反对称性和传递性。100111(c)111,具有自反性,对称性和传递性。111
23、设R的关系图如图7.12所示,试给出r(R),s(R)和t(R)的关系图。
25、设A{1,2,3,4},R是A上的等价关系,且R是A上所构成的等价类为{1},{2,3,4}。(1)求R。(2)求RR(3)求R传递闭包。解答:
(1)R{1,1,2,2,3,3,4,4,2,3,3,2,2,4,4,2,3,4, 14,3}
(2)由于等价关系满足对称性,所以R所以RR11R
R
(3)由于等价关系满足传递性,所以传递闭包为其自身,即t(R)R
26、对于给定的A和R,判断R是否为A上的等价关系。(1)A为实数集,x,yA,xRyxy2。(2)A{1,2,3},x,yA,xRyxy3。(3)AZ,x,yA,xRyxy为奇数。
(5)AP(X),CX,x,yA,xRyxyC 解答:
(1)不是,不满足自反性、对称性、传递性。(2)不是,由于A{1,2,3}集合较小,①自反性:xA,xx3x,xR
②对称性,x,yR,xy3yx3y,xR 但是传递性不满足,1,3,3,2R,但是1,2R。(3)不是,满足对称性、传递性,但是不满足自反性 取x2,但是224不为奇数,所以2,2R。
(5)满足
①自反性:xAxXxxCx,xR ②对称性:x,yRyxxyCy,xR ③传递性:x,y,y,zR
xyC,yzC (xy)(yx)C,(yz)(zy)C(xy)C,(yx)C,(yz)C,(zy)C下面证明(xz)C
a(xz)ax,az
若ay,则ayz,所以aC 若ay,则axy,所以aC
所以(xz)C,同理可证,(zx)C 所以xz(xz)(zx)C 所以x,zR。因此满足传递性。
27、设A{a,b,c,d},A上的等价关系
R{a,b,b,a,c,d,d,c}IA
画出R的关系图,并求出A中各元素的等价类。解答:关系图为
等价类[a][b]{a,b};[c][d]{c,d}
30、设A{1,2,3,4},,在AA上定义二元关系R,u,v,x,yAA,u,vRx,yuyxv。
(1)证明R为AA上的等价关系。(2)确定由R引起的对AA的划分。解答:(1)证明:
①自反性:x,yAA,由于xyxy,所以x,y,x,yR; ②对称性:x,y,u,vR
有xvuy,所以uyxv 因此u,v,x,yR
③传递性:x,y,u,v,u,v,s,tR
有xvuy,utsv,所以xsty 因此x,y,s,tR。
(2)等价类有
[1,1]{1,1,2,2,3,3,4,4} [1,2]{1,2,2,3,3,4} [1,3]{1,3,2,4} [1,4]{1,4} [2,1]{2,1,3,2,4,3} [3,1]{3,1,4,2} [4,1]{4,1}
37、对于下列集合与整除关系画出哈斯图。(1){1,2,3,4,6,8,12,24}(2){1,2,3,4,5,6,7,8,9,10,11,12} 解答:(1)
(2)
38、针对图7.14中的每个哈斯图,写出集合以及偏序关系的表达式。
解答:
(a)集合为A{1,2,3,4,5},偏序关系为{1,3,1,5,2,4,2,5,3,5,4,5}IA(b)集合为B{a,b,c,d,e,f},偏序关系为{a,b,c,d,e,f}IB(c)集合为C{1,2,3,4,5},偏序关系{1,2,1,3,1,4,1,5,2,4,2,5,3,4,3,5,4,5}IC
40、分别画出下列偏序集A,R的哈斯图,并找出A的极大元、极小元、最大元和最小元。
(1)A{a,b,c,d,e,f},R{a,d,a,c,a,b,a,e,b,e,c,e,d,e}IA
R{c,d}IA(2)A{a,b,c,d,e}
解答:
(1)哈斯图为
极小元为a,f,极大元为e,f,无最大元、最小元(2)哈斯图为
极小元为a,b,c,e,极大元为a,b,d,e,无最大元、最小元
41、A{1,2,3....,12},R为整除关系,B{x|2x4},在偏序集A,R中求B的上界、下界、最小上界和最大下界。
解:下界即为公约数,2,3,4的公约数只有1,所以下界为1,最大下界也为1;
下界即为公倍数,2,3,4的公倍数只有12,所以上界为1,最大上界也为12;
P141:习题八
4、判断下列函数中哪些是满射?哪些是单射?哪些是双射?(2)f:NN,f(x)x22(4)f:N{0,1},f(x)01xisodd
xiseven(6)f:RR,f(x)x22x15
解答:(2)单射;(3)满射;(4)既不为单射也不为满射。
{1,2,3}
5、设X{a,b,c,d},Y,f{a,1,b,2,c,3},判断下列命题的真假。
(1)f是从X到Y的二元关系,但不是X到Y的函数。(3)f是从X到Y的满射,但不是单射。解答:(1)真;(3)假
15、设A{a,b,c},R为A上的等价关系,且R{a,b,b,a}IA,求自然映射g:AA/R。
解答: A/R{{a,b},{c}}
{a,b}g(x)cxa,b xc19、设f,g是从N到N的函数,且
x1f(x)0x(1)求f(2)说明f解答: x0,1,2,3x4x5g
x
g(x)23xiseven
xisoddg是否为单射、满射、双射?
(1)f3x1gg(f(x))20x2x0,2,5,7,9......x1,3x4x6,8,10,12.....(2)为满射,但是不为单射。
20、设f:NNN,f(x)x,x1(1)说明f是否为单射和满射,说明理由。
(2)f的反函数是否存在,如果存在,求出f的反函数;(3)求ranf。解答:
(1)xy时,x,x1y,y1,所以为单射; 而对1,3NN,不存在xN,使得f(x)x,x1,所以不为满射。
(2)不存在反函数,因为不是双射函数;(3)ranf{x,x1|xN}
22、对于以下集合A和B,构造从A到B的双射函数。(1)A{1,2,3},B{a,b,c}(2)A(0,1),B(0,2)
(3)A{x|xZx0},BN(4)AR,BR 解答: a(1)f(x)bc(2)f(x)2xx1x2 x3x(0,1)(3)f(x)x1
(4)f(x)ax(a0,a1)
第四篇:控制系统的Matlab仿真与设计课后答案
MATLAB课后习题答案 2.1 x=[15 22 33 94 85 77 60] x(6)x([1 3 5])x(4:end)x(find(x>70))2.3 A=zeros(2,5);
A(:)=-4:5
L=abs(A)>3 islogical(L)
X=A(L)2.4 A=[4,15,-45,10,6;56,0,17,-45,0] find(A>=10&A<=20)2.5 p1=conv([1,0,2],conv([1,4],[1,1]));p2=[1 0 1 1];[q,r]=deconv(p1,p2);cq='商多项式为
';cr='余多项式为
';disp([cq,poly2str(q,'s')]),disp([cr,poly2str(r,'s')])2.6 A=[11 12 13;14 15 16;17 18 19];PA=poly(A)
PPA=poly2str(PA,'s')3.1 n=(-10:10)';y=abs(n);plot(n,y,'r.','MarkerSize',20)axis equal grid on xlabel('n')3.2 x=0:pi/100:2*pi;y=2*exp(-0.5*x).*sin(2*pi*x);plot(x,y),grid on;3.3 t=0:pi/50:2*pi;x=8*cos(t);y=4*sqrt(2)*sin(t);z=-4*sqrt(2)*sin(t);plot3(x,y,z,'p');
title('Line in 3-D Space');text(0,0,0,'origin');
xlabel('X'),ylable('Y'),zlable('Z');grid;3.4
theta=0:0.01:2*pi;
rho=sin(2*theta).*cos(2*theta);polar(theta,rho,'k');3.5
[x,y,z]=sphere(20);z1=z;
z1(:,1:4)=NaN;c1=ones(size(z1));surf(3*x,3*y,3*z1,c1);hold on z2=z;
c2=2*ones(size(z2));
c2(:,1:4)=3*ones(size(c2(:,1:4)));surf(1.5*x,1.5*y,1.5*z2,c2);colormap([0,1,0;0.5,0,0;1,0,0]);grid on hold off 第四章
function f=factor(n)if n<=1 f=1;else
f=factor(n-1)*n;end
function[s,p]=fcircle(r)s=pi*r*r;p=2*pi*r;
function k=jcsum1(n)k=0;i=0;while i<=n k=k+2^i;i=i+1;end
function k=jcsum(n)k=0;
for i=0:n k=k+2^i;end
4.1for m=100:999
m1=fix(m/100);m2=rem(fix(m/10),10);m3=rem(m,10);
if
m==m1*m1*m1+m2*m2*m2+m3*m3*m3 disp(m)
end end
4.2[s,p]=fcircle(10)4.3y=0;n=100;for i=1:n
y=y+1/i/i;end y
4.4s=0;for i=1:5
s=s+factor(i);end s
4.5sum=0;i=1;while sum<2000 sum=sum+i;i=i+1;end;n=i-2
4.6jcsum(63)jcsum1(63)4.1 for m=100:999
m1=fix(m/100);
m2=rem(fix(m/10),10);
m3=rem(m,10);
if m==m1*m1*m1+m2*m2*m2+m3*m3*m3
disp(m)
end end 4.3 y=0;n=100;
for i=1:n
y=y+1/i/i;end y 4.4 s=0;for i=1:5
s=s+factor(i);end s 4.5
sum=0;i=1;
while sum<2000
sum=sum+i;
i=i+1;end;n=i-2 4.6
i=0;k=0;while i<=63
k=k+2^i;
i=i+1;end k i
i=0;k=0;for i=0:63
k=k+2^i;end i k
第五章
function f=fxyz(u)x=u(1);y=u(2);z=u(3);f=x+y.^2./x/4+z.^2./y+2./z;
5.1A=[2,1,-5,1;1,-5,0,7;0,2,1,-1;1,6,-1,-4];
b=[13,-9,6,0]';x=Ab
5.2[U,fmin]=fminsearch('fxyz',[0.5,0.5,0.5])
5.3X=linspace(0,2*pi,50);Y=sin(X);
P=polyfit(X,Y,3)AX=linspace(0,2*pi,50);Y=sin(X);Y1=polyval(P,X)
plot(X,Y,':O',X,Y1,'-*')
5.4x=0:2.5:10;h=[0:30:60]';
T=[95,14,0,0,0;88,48,32,12,6;67,64,54,48,41];xi=[0:0.5:10];hi=[0:10:60]';
temps=interp2(x,h,T,xi,hi,'cubic');
mesh(xi,hi,temps);
5.1 A=[2,1,-5,1;1,-5,0,7;0,2,1,-1;1,6,-1,-4];b=[13,-9,6,0]';x=Ab 5.3 X=linspace(0,2*pi,50);Y=sin(X);P=polyfit(X,Y,3)AX=linspace(0,2*pi,50);Y=sin(X);Y1=polyval(P,X)plot(X,Y,':O',X,Y1,'-*')6.1syms x
y=finverse(1/tan(x))6.2syms x y
f=1/(1+x^2);g=sin(y);fg=compose(f,g)6.3syms x
g=(exp(x)+x*sin(x))^(1/2);dg=diff(g)
6.4F=int(int('x*exp(-x*y)','x'),'y')
6.5syms x
F=ztrans(x*exp(-x*10))6.6a=[0 1;-2-3];syms s
inv(s*eye(2)-a);
6.7 f=solve('a*x^2+b*x+c')6.8 f=solve('x+y+z=1','x-y+z=2','2*x-y-z=1')
6.9y=dsolve('D2y+2*Dy+2*y=0','y(0)=1','Dy(0)=0')ezplot(y),grid on
6.10a=maple('simplify(sin(x)^2+cos(x)^2);')
6.11f=maple('laplace(exp(-3*t)*sin(t),t,s);')6.12 syms t x
F=sin(x*t+2*t);L=laplace(F)第七章
function
[sys,x0,str,ts]=ww(t,x,u,flag)%¶¨ÒåÁ¬ÐøϵͳµÄSº¯Êý A=[0,1;-0.4,-0.2];B=[0;0.2];C=[1,0];D=0;
switch flag, case 0,[sys,x0,str,ts]=mdlInitializeSizes(A,B,C,D);case 1,sys=mdlDerivatives(t,x,u,A,B,C,D);case 2,sys=mdlUpdate(t,x,u);case 3,sys=mdlOutputs(t,x,u,A,B,C,D);case 4,sys=mdlGetTimeOfNextVarHit(t,x,u);case 9,sys=mdlTerminate(t,x,u);otherwise
error(['Unhandled flag = ',num2str(flag)]);end
%=============================== function
[sys,x0,str,ts]=mdlInitializeSizes(A,B,C,D)sizes = simsizes;sizes.NumContStates = 2;sizes.NumDiscStates = 0;sizes.NumOutputs = 1;sizes.NumInputs = 1;sizes.DirFeedthrough = 1;sizes.NumSampleTimes = 1;sys = simsizes(sizes);x0 = [0;0];str = [];ts = [0 0];
%=============================== function
sys=mdlDerivatives(t,x,u,A,B,C,D)
sys = A*x+B*u;
%===============================
function sys=mdlUpdate(t,x,u)sys = [];
%=============================== function
sys=mdlOutputs(t,x,u,A,B,C,D)sys = C*x+D*u;
%=============================== function
sys=mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 1;sys = t + sampleTime;
%===============================
function sys=mdlTerminate(t,x,u)sys = [];
7.1
7.2
7.3
7.4
7.5
7.6
7.7
第八章
8.1num=[5];den=[1,2,2];sys=tf(num,den)8.1.2s = tf('s');
H = [5/(s^2+2*s+2)];H.inputdelay =2
8.1.3h=tf([0.5,0],[1,-0.5,0.5],0.1)
8.2num=2*[1,0.5];den=[1,0.2,1.01];
sys=tf(num,den)
[z,p,k]=tf2zp(num,den);zpk(z,p,k)
[A,B,C,D]=tf2ss(num,den);ss(A,B,C,D)
8.3 num=[1,5];den=[1,6,5,1];ts=0.1;
sysc=tf(num,den);sysd=c2d(sysc,ts,'tustin')8.4.0
8.4.1 %¶Ôϵͳ·½¿òͼÿ¸ö»·½Ú½øÐбàºÅ,ÓÐ8¸öͨµÀ,ÁÐдÿ¸öͨµÀ´«µÝº¯Êý r1=1;r2=2;c1=3;c2=4;G1=r1;G2=tf(1,[c1,0]);
G3=1;%ÊÇ·ÖÀëµãºÍ»ãºÏµãµÄÁ¬Ïß,²»Äܺϲ¢,´«º¯Îª1 G4=-1;G5=1/r2;
G6=tf(1,[c2,0]);G7=-1;G8=-1;%½¨Á¢ÎÞÁ¬½ÓµÄ״̬¿Õ¼äÄ£ÐÍ G=append(G1,G2,G3,G4,G5,G6,G7,G8)
%д³öϵͳµÄÁ¬½Ó¾ØÕó
Q=[1 4 0 %ͨµÀ1µÄÊäÈëÊÇͨµÀ4 2 1 7 %ͨµÀ2µÄÊäÈëÊÇͨµÀ1,7 3 2 0
2 0 5 3 8 6 5 0 7 5 0
6 0];%¸ººÅÔÚ´«º¯ÖÐÌåÏÖ %ÁгöϵͳµÄ×ܵÄÊäÈëºÍÊä³ö¶ËµÄ±àºÅ
inputs=1;outputs=6;
%Éú³É×éºÏºóϵͳµÄ״̬¿Õ¼äÄ£ÐÍ sys=connect(G,Q,inputs,outputs)
8.4.2r1=1;r2=2;c1=3;c2=4;[A,B,C,D]=linmod('x84');[num,den]=ss2tf(A,B,C,D);sys=tf(num,den)
8.5A=[1,1,0;0,1,0;0,0,2];B=[0,0;1,0;0,-2];n=size(A)
Tc=ctrb(A,B);if n==rank(Tc)
disp('ϵͳÍêÈ«ÄÜ¿Ø');else
disp('ϵͳ²»ÍêÈ«ÄÜ¿Ø');end 第九章
function [rtab,info]=routh(den)info=[];
vec1=den(1:2:length(den));
nrT=length(vec1);
vec2=den(2:2:length(den)-1);rtab=[vec1;vec2,zeros(1,nrT-length(vec2))];for k=1:length(den)-2, alpha(k)=vec1(1)/vec2(1);
for i=1:length(vec2),a3(i)=rtab(k,i+1)-alpha(k)*rtab(k+1,i+1);
end
if sum(abs(a3))==0 a3=polyder(vec2);
info=[info,'All elements in row ',...int2str(k+2)' are zeros;'];
elseif abs(a3(1)) info=[info,'Replaced first element;']; end rtab=[rtab;a3, zeros(1,nrT-length(a3))];vec1=vec2;vec2=a3;end 9.1num=[2,5,1];den=[1,2,3];bode(num,den);grid on;figure; nyquist(num,den); 9.2num=5*[1,5,6];den=[1,6,10,8];step(num,den);grid on;figure; impulse(num,den);grid on;9.3kosi=0.7;wn=6; num=wn^2;den=[1,2*kosi*wn,wn^2];step(num,den);grid on;figure; impulse(num,den);grid on;9.4den=[1,2,8,12,20,16,16];[rtab,info]=routh(den)a=rtab(:,1) if all(a>0) disp('ϵͳÊÇÎȶ¨µÄ'); else disp('ϵͳÊDz»Îȶ¨µÄ'); end 9.5num=7*[1,5];den=conv([1,0,0],conv([1,10],[1,1])); [gm,pm,wg,wc]=margin(num,den) 9.1 >> sys=tf([2,5,1],[1,2,3])Transfer function: 2 s^2 + 5 s + 1---------------s^2 + 2 s + 3 >> rlocus(sys)>> nyquist(sys)>> bode(sys)9.2 >> G=tf(conv([5],[1,5,6]),[1,6,10,8]);>> step(G)>> impulse(G) sys=tf([5,25,30],[1,6,10,8]);>> step(sys)>> impulse(sys)9.4>> GH=tf(conv([7],[1,5]),conv([1,0,0],conv([1,10],[1,1]))); >> [Gm,Pm,Wcg,Wcp]=margin(GH)Gm = 0 Pm = -47.2870 Wcg = 0 Wcp = 1.4354 >> GH=tf(conv([7],[1,5]),conv([1,0,0],conv([1,10],[1,1]))); >> [Gm,Pm,Wcg,Wcp]=margin(GH)GH_close=feedback(GH,1)step(GH_close),grid on Gm = 0 Pm = -47.2870 Wcg = 0 Wcp = 1.4354 第十章 function s=bpts2s(bp,ts,delta)kosi=sqrt(1-1./(1+((1./pi).*log(1./bp)).^2)); wn=-log(delta.*sqrt(1-kosi.^2))/(kosi.*ts); s=-kosi.*wn+j.*wn.*sqrt(1-kosi.^2); function [ngc,dgc]=fa_lead(ng0,dg0,Pm,wc,w) ngv=polyval(ng0,j*wc);dgv=polyval(dg0,j*wc);g=ngv/dgv; thetag=angle(g);mg=abs(g);thetar=Pm*pi/180; tz=(1+mg*cos(thetar-thetag))/(-wc*mg*sin(thetar-thetag));tp=(cos(thetar-thetag)+mg)/(wc*sin(thetar-thetag));ngc=[tz,1];dgc=[tp,1]; function [ngc,dgc]=fg_lag_pm(ng0,dg0,w,Pm) [mu,pu]=bode(ng0,dg0,w);wgc=spline(pu,w,Pm+5-180);%²åÖµÇóÈ¡Âú×ãÏà½ÇÔ£¶ÈµÄ½ÇƵÂÊ×÷ΪÆÚÍûµÄ¼ôÇÐƵÂÊ ngv=polyval(ng0,j*wgc);dgv=polyval(dg0,j*wgc);g=ngv/dgv; alph=abs(1/g);T=10/alph*wgc, ngc=[alph*T,1];dgc=[T,1]; function [ngc,dgc]=fg_lag_wc(ng0,dg0,w,wc) ngv=polyval(ng0,j*wc);dgv=polyval(dg0,j*wc);g=ngv/dgv; alph=abs(1/g);T=10/(alph*wc);ngc=[alph*T,1];dgc=[T,1]; function [ngc,dgc]=fg_lead_pd(ng0,dg0,wc)ngv=polyval(ng0,j*wc);dgv=polyval(dg0,j*wc);g=ngv/dgv;mg0=abs(g); t=sqrt(((1/mg0)^2-1)/(wc^2));%·ùÖµÏà¼ÓΪÁã ngc=[t,1];dgc=[1];%Gc(s)=1+Ts function [ngc,dgc]=fg_lead_pm(ng0,dg0,Pm,w) [mu,pu]=bode(ng0,dg0,w);%¼ÆËãÔ-ϵͳµÄ¶ÔÊýƵÂÊÏìÓ¦Êý¾Ý [gm,pm,wcg,wcp]=margin(mu,pu,w);%ÇóÈ¡Ô-ϵͳµÄÏà½ÇÔ£¶ÈºÍ¼ôÇÐƵÂÊ alf=ceil(Pm-pm+5);%¼ÆËã¿ØÖÆÆ÷ÌṩµÄ×î´ó³¬Ç°½Ç¶È£¬ phi=(alf)*pi/180;%½«×î´ó³¬Ç°½Çת»»Îª»¡¶Èµ¥Î» a=(1+sin(phi))/(1-sin(phi));%¼ÆËãaÖµ dbmu=20*log10(mu);%ϵͳµÄ¶ÔÊý·ùÖµ mm=-10*log10(a);%wm´¦µÄ¿ØÖÆÆ÷¶ÔÊý·ùÖµ wgc=spline(dbmu,w,mm);%²îÖµÇóÈ¡wm£¬ÈÏΪwm£½wc T=1/(wgc*sqrt(a));%¼ÆËãT ngc=[a*T,1];dgc=[T,1]; function [ngc,dgc]=fg_lead_pm_wc(ng0,dg0,Pm,wc,w) [mu,pu]=bode(ng0,dg0,w);ngv=polyval(ng0,j*wc);dgv=polyval(dg0,j*wc); g=ngv/dgv;%ÇóÔ-ϵͳÔÚÆÚÍûµÄ¼ôÇÐƵÂÊ´¦µÄƵÂÊÏìÓ¦Êý¾ÝG0(jwc) theta=180*angle(g)/pi;%Ô-ϵͳÔÚÆÚÍûµÄ¼ôÇÐƵÂÊ´¦µÄÏà½ÇÔ£¶È£¬µ¥Î»Îª¶È alf=ceil(Pm-(theta+180)+5);% ×î´ó³¬Ç°½Ç phi=(alf)*pi/180; a=(1+sin(phi))/(1-sin(phi));dbmu=20*log10(mu);mm=-10*log10(a);wgc=spline(dbmu,w,mm);T=1/(wgc*sqrt(a)); KK=128;s1=-2+i*2*sqrt(3);a=2 ng0=[10];dg0=conv([1,0],conv([1,2],[1,8]));g0=tf(ng0,dg0); [ngc,dgc,k]=rg_lag(ng0,dg0,KK,s1,a); gc=tf(ngc,dgc)function s=kw2s(kosi,wn)s=-kosi.*wn+j*wn.*sqrt(1-kosi.^2); 10.1ng0=[1];dg0=10000*[1 0-1.1772]; g0=tf(ng0,dg0);%Âú×㿪»·ÔöÒæµÄΪУÕýϵͳµÄ´«µÝº¯Êý s=kw2s(0.7,0.5)%ÆÚÍûµÄ±Õ»·Ö÷µ¼¼«µã ngc=rg_lead(ng0,dg0,s);gc=tf(ngc,1)g0c=tf(g0*gc);rlocus(g0,g0c); b1=feedback(g0,1);%δУÕýϵͳµÄ±Õ»·´«µÝº¯Êý b2=feedback(g0c,1);%УÕýºóϵͳµÄ±Õ»·´«µÝº¯Êý figure,step(b1,'r--',b2,'b');grid on %»æÖÆУÕýÇ°ºóϵͳµÄµ¥Î»½×Ô¾ KK=20;s1=-2+i*sqrt(6);a=1 ng0=[10];dg0=conv([1,0],[1,4]);g0=tf(ng0,dg0); [ngc,dgc,k]=rg_lag(ng0,dg0,KK,s1,a);gc=tf(ngc,dgc)g0c=tf(KK*g0*gc); b1=feedback(k*g0,1);b2=feedback(g0c,1);step(b1,'r--',b2,'b');grid on g0c=tf(KK*g0*gc);rlocus(g0,g0c); b1=feedback(k*g0,1); b2=feedback(g0c,1);figure,step(b1,'r--',b2,'b');grid on ng0=[1];dg0=conv([1,0,0],[1,5]);g0=tf(ng0,dg0);w=logspace(-3,3);KK=1;Pm=50; [ngc,dgc]=lead4(ng0,dg0,KK,Pm,w); gc=tf(ngc,dgc);g0c=tf(KK*g0*gc);bode(KK*g0,w);hold on,bode(g0c,w);grid on,hold off [gm,pm,wcg,wcp]=margin(g0c)Kg=20*log10(gm)g1=feedback(g0c,1);bode(g1),grid on, [mag,phase,w]=bode(g1);a=find(mag<=0.707*mag(1));wb=w(a(1))max(mag) b=find(mag==max(mag))wr=w(b) KK=40;Pm=50;ng0= KK *[1]; dg0=conv([1,0],conv([1,1],[1,4])); g0=tf(ng0,dg0); w=logspace(-2,4); [ngc,dgc]=fg_lead_pm(ng0,dg0,Pm, w)gc=tf(ngc,dgc),g0c=tf(g0*gc); b1=feedback(g0,1);b2=feedback(g0c,1); step(b1,'r--', b2,'b');grid on figure, bode(g0,'r--',g0c,'b',w), grid on,[gm,pm,wcg,wcp]=margin(g0c), Km=20*log10(gm) KK=200;bp=0.3;ts=0.7;delta=0.05; ng0=[1];dg0=conv([1,0],conv([0.1,1],conv([0.02 1],conv([0.01,1],[0.005 1]))));g0=tf(ng0,dg0); w=logspace(-4,3);t=[0:0.1:3];[mag,phase]=bode(KK*g0,w);[gm0,pm0,wg0,wc0]=margin(mag,phase,w),gm0=20*log10(gm0)%gm0 =-15.6769 %2¡¢È·¶¨ÆÚÍûµÄ¿ª»·´«µÝº¯Êý mr=0.6+2.5*bp; wc=ceil((2+1.5*(mr-1)+2.5*(mr-1)^2)*pi/ts), h=(mr+1)/(mr-1)w1=2*wc/(h+1), w2=h*w1 w1=wc/10;w2=25;ng1=[1/w1,1];dg1=conv([1/w2,1],conv([1,0],[1,0])); g1=tf(ng1,dg1); g=polyval(ng1,j*wc)/polyval(dg1,j*wc);K=abs(1/g);%¼ôÇÐƵÂÊ´¦·ùֵΪ1£¬ÇóKÖµ g1=tf(K*g1) %3¡¢È·¶¨·´À¡»·½Ú´«µÝº¯Êý h=tf(dg1,ng1);Kh=1/K;h=tf(Kh*h)%ÆÚÍûƵÂÊÌØÐԵĵ¹ÌØÐÔ %4¡¢ÑéËãÐÔÄÜÖ¸±ê g2=feedback(KK*g0,h);%УÕýºó£¬ÏµÍ³µÄ¿ª»·´«µÝº¯Êý b1=feedback(KK*g0,1);b2=feedback(g2,1); bode(KK*g0,'r--',g2,'b',h,'g',w);grid on figure,step(b1, 'r--',b2, 'b',t);grid on,[pos,tr,ts,tp]=stepchar(b2,delta) function [ngc,dgc]=lag2(ng0,dg0,w,KK,Pm)[mu,pu]=bode(KK*ng0,dg0,w);wgc=spline(pu,w,Pm+5-180), ngv=polyval(KK*ng0,j*wgc);dgv=polyval(dg0,j*wgc);g=ngv/dgv; alph=abs(1/g), T=10/alph*wgc, ngc=[alph*T,1];dgc=[T,1]; function [ngc,dgc]=lead4(ng0,dg0,KK,Pm,w)[mu,pu]=bode(KK*ng0,dg0,w);[gm,pm,wcg,wcp]=margin(mu,pu,w);alf=ceil(Pm-pm+5);phi=(alf)*pi/180; a=(1+sin(phi))/(1-sin(phi)), dbmu=20*log10(mu);mm=-10*log10(a); wgc=spline(dbmu,w,mm), T=1/(wgc*sqrt(a)),ngc=[a*T,1];dgc=[T,1]; function [ngc,dgc]=ra_lead(ng0,dg0,s1)ngv=polyval(ng0,s1);dgv=polyval(dg0,s1);g=ngv/dgv;thetag=angle(g);mg=abs(g);thetas=angle(s1);ms=abs(s1); tz=(sin(thetas)-mg*sin(thetag-thetas))/(mg*ms*sin(thetag));tp=-(mg*sin(thetas)+sin(thetag+thetas))/(ms*sin(thetag));ngc=[tz,1];dgc=[tp,1]; function [ngc,dgc,k]=rg_lag(ng0,dg0,KK,s1,a) ngv=polyval(ng0,s1);dgv=polyval(dg0,s1);g=dgv/ngv; k=abs(g);%ÆÚÍûÖ÷µ¼¼«µã´¦µÄ¸ù¹ì¼£ÔöÒæ beta=k/KK; [kosi1,wn1]=s2kw(s1); zc=-wn1*sin(a*pi/180)/sin(pi-atan(sqrt(1-kosi1^2)/kosi1)-(a*pi/180));%ÀûÓÃÕýÏÒ¶¨Àí pc=beta*zc; ngc=beta*[1,-zc];dgc=[1,-pc]; function varargout=rg_lead(ng0,dg0,s1)if nargout==1 ngv=polyval(ng0,s1);dgv=polyval(dg0,s1);g=ngv/dgv; thetal=pi-angle(g); zc=real(s1)-imag(s1)/tan(thetal); t=-1/zc; varargout{1}=[t,1];elseif nargout==2 ngv=polyval(ng0,s1);dgv=polyval(dg0,s1); g=ngv/dgv;theta=angle(g);phi=angle(s1); if theta>0 phi_c=pi-theta; end if theta<0;phi_c=-theta end theta_z=(phi+phi_c)/2;theta_p=(phi-phi_c)/2; z_c=real(s1)-imag(s1)/tan(theta_z); p_c=real(s1)-imag(s1)/tan(theta_p); nk=[1-z_c];varargout{2}=[1-p_c];kc=abs(p_c/z_c); if theta<0 kc=-kc end varargout{1}=kc*nk;else error('Êä³ö±äÁ¿ÊýÄ¿²»ÕýÈ·£¡');end function [bp,ts]=s2bpts(s,delta)[kosi,wn]=s2kw(s); bp=exp(-kosi.*pi./sqrt(1-kosi.^2)); ts=-1./(kosi.*wn)*log(delta.*sqrt(1-kosi.^2)); function [kosi,wn]=s2kw(s)kosi=1./sqrt(1+(imag(s)/real(s)).^2); wn=-real(s)./kosi; %Èç¹ûwnΪ¸ºÖµ£¬ÔòwnÈ¡Õý£¬²¢ÇÒkosiÈ¡·´ iwn=(wn<0);wn(iwn)=-wn(iwn);kosi(iwn)=-kosi(iwn); function [pos,tr,ts,tp]=stepchar(g0,delta) [y,t]=step(g0);[mp,ind]=max(y);dimt=length(t);yss=y(dimt); pos=100*(mp-yss)/yss;tp=t(ind);for i=1:dimt if y(i)>=1 tr=t(i); break; end end; for i=1:length(y) if y(i)<=(1-delta)*yss|y(i)>=(1+delta)*yss ts=t(i); end end 第十一章 11.1a=[0 1 0;0 0 1;-1-5-6];b=[0 0 1]'; p=[-2+4j;-2-4j;-10];K=acker(a,b,p)eig(a-b*K) 11.2a=[0 1 0;0 0 1;-6-11-6];b=[1,0,0]'; p=[-2+2*sqrt(3)*j;-2-2*sqrt(3)*j;-10]; K=acker(a,b,p)eig(a-b*K) 11.6A=[-1 0 0;0-2-3;0 0-3];B=[1 0;2 3;-3-3];C=[1 0 0;1 1 1 ]; [G,K,L]=decoupling(A,B,C) 11.8A=[0 20.6;1 0];b=[0 1]';c=[0 1];d=0; G=ss(A,b,c,d); Q=diag([1,0,0,0,0]);R=1; p=[-1.8+2.4j;-1.8-2.4j];[k,P]=lqr(A,b,Q,R);l=(acker(A',c',p))' Gc=-reg(G,k,l);zpk(Gc), eig(Gc.a), t=0:0.05:2; G_1=feedback(G*Gc,1);a1=eig(G_1.a), y_1=step(G_1,t); 第十二章 function [t,xx]=diffstate(G,H,x0,u0,N,T)xk=x0;u=u0;t=0 for k=1:N xk=G*xk+H*u;x(:,k)=xk;t=[t,k*T];end;xx=[x0,x]; 12.1 function sys=M601(t,x)u=1; sys=[x(2);x(3);-800*x(1)-80*x(2)-24*x(3)+u]; function [t,y]=ode4(A,B,C,D,x0,h,r,v,t0,tf) Ab=A-B*v*C;B=B;C=C;x=x0';y=0;t=t0; N=round((tf-t0)/h);for i=1:N k1=Ab*x+B*r; k2=Ab*(x+h*k1/2)+B*r;k3=Ab*(x+h*k2/2)+B*r;k4=Ab*(x+h*k3)+B*r;x=x+h*(k1+2*k2+2*k3+k4)/6;y=[y,C*x];t=[t,t(i)+h];end 12.1 tspan=[0,10];x0=[0,0,0]'; [t,y]=ode45('M601',tspan,x0);y1=800*y(:,1);plot(t,y1); 12.2 num=10;den=conv([1,0],conv([1,2],[1,3])); [A,B,C,D]=tf2ss(num,den);x0=[0,0,0];v=1;t0=0;tf=10;h=0.01;r=1; [t,y]=ode4(A,B,C,D,x0,h,r,v,t0,tf); plot(t,y),grid 12.3 12.4 g=[-2.8-1.4 0 0;1.4 0 0 0;-1.8-0.3-1.4-0.6;0 0 0.6 0];h=[1 0 1 0]';c=[0 0 0 1];d=0; x0=[0 0 0 0]';u=1;N=30;T=0.1; [t,xx]=diffstate(g,h,x0,u,N,T);plot(t,xx);y=c*xx;figure stairs(t,y)grid on 12.6 第十四章 14.1 clear all;load optcar.mat; t=signals(1,:);p=signals(2,:);v=signals(3,:);a=signals(4,:);theta=signals(5,:); subplot(4,1,1);plot(t,p);grid on;ylabel('λÖÃ(m)');subplot(4,1,2);plot(t,v);grid on;ylabel('ËÙ¶È(m/s)');subplot(4,1,3);plot(t,a);grid on;ylabel('¼ÓËÙ¶È(m/s2)');subplot(4,1,4);plot(t,theta);grid on;ylabel('½Ç¶È(¶È)'); 14.1 clear all load car.mat %½«µ¼Èëµ½car.matÖеķÂÕæʵÑéÊý¾Ý¶Á³ö t=signals(1,:);x=signals(2,:);theta=signals(3,:);x1=signals(4,:);theta1=signals(5,:); plot(t,x,t,x1);ylabel('С³µÎ»ÖÃ(m)'),grid on;% »æÖÆ¿ØÖÆÁ¦×÷ÓÃϽüËÆÄ£Ðͺ;«È·Ä£ÐÍxµÄµ¥Î»½×Ô¾ÏìÓ¦ÇúÏß figure % »æÖÆ¿ØÖÆÁ¦×÷ÓÃϽüËÆÄ£Ðͺ;«È·Ä£ ÐÍthetaµÄµ¥Î»½×Ô¾ÏìÓ¦ÇúÏß plot(t,theta,t,theta1);ylabel('°Ú½ÇÖµ(rad)'),grid on; 第二章 1>>x=[15 22 33 94 85 77 60] >>x(6)>>x([1 3 5])>>x(4:end)>>x(find(x>70))2>>T=[1-2 3-4 2-3];>>n=length(T);>>TT=T';>>for k=n-1:-1:0 >>B(:,n-k)=TT.^k;>>end >>B >>test=vander(T)3>>A=zeros(2,5);>>A(:)=-4:5 >>L=abs(A)>3 >>islogical(L)>>X=A(L)4>>A=[4,15,-45,10,6;56,0,17,-45,0] >>find(A>=10&A<=20)5>>p1=conv([1,0,2],conv([1,4],[1,1]));>>p2=[1 0 1 1];>>[q,r]=deconv(p1,p2);>>cq='商多项式为 ';cr='余多项式为 ';>>disp([cq,poly2str(q,'s')]),disp([cr,poly2str(r,'s')])6>>A=[11 12 13;14 15 16;17 18 19];>>PA=poly(A)>>PPA=poly2str(PA,'s')第三章 1>>n=(-10:10)';>>y=abs(n);>>plot(n,y,'r.','MarkerSize',20)>>axis equal >>grid on >>xlabel('n')2>>x=0:pi/100:2*pi;>>y=2*exp(-0.5*x).*sin(2*pi*x);>>plot(x,y),grid on;3>>t=0:pi/50:2*pi;>>x=8*cos(t);>>y=4*sqrt(2)*sin(t); >>z=-4*sqrt(2)*sin(t);>>plot3(x,y,z,'p');>>title('Line in 3-D Space');>>text(0,0,0,'origin');>>xlabel('X'),ylable('Y'),zlable('Z');grid;4>>theta=0:0.01:2*pi;>>rho=sin(2*theta).*cos(2*theta);>>polar(theta,rho,'k');5>>[x,y,z]=sphere(20);>>z1=z;>>z1(:,1:4)=NaN;>>c1=ones(size(z1));>>surf(3*x,3*y,3*z1,c1);>>hold on >>z2=z;>>c2=2*ones(size(z2));>>c2(:,1:4)=3*ones(size(c2(:,1:4)));>>surf(1.5*x,1.5*y,1.5*z2,c2);>>colormap([0,1,0;0.5,0,0;1,0,0]);>>grid on >>hold off 第四章 1>>for m=100:999 m1=fix(m/100);m2=rem(fix(m/10),10);m3=rem(m,10);if m==m1*m1*m1+m2*m2*m2+m3*m3*m3 disp(m)end end M文件: function[s,p]=fcircle(r)s=pi*r*r;p=2*pi*r;主程序: [s,p]=fcircle(10)3>>y=0;n=100;for i=1:n y=y+1/i/i;end >>y M文件: function f=factor(n)if n<=1 f=1;else f=factor(n-1)*n;end 主程序: >>s=0;for i=1:5 s=s+factor(i);end >>s 5>>sum=0;i=1;while sum<2000 sum=sum+i;i=i+1;end;>>n=i-2 6 for循环M文件: function k=jcsum(n)k=0;for i=0:n k=k+2^i;end 主程序: >>jcsum(63) While循环M文件: function k=jcsum1(n)k=0;i=0;while i<=n k=k+2^i;i=i+1;end 主程序: >>jcsum1(63)第五章 1>>A=[2,1,-5,1;1,-5,0,7;0,2,1,-1;1,6,-1,-4];>>b=[13,-9,6,0]';>>x=Ab M文件: function f=fxyz(u)x=u(1);y=u(2);z=u(3);f=x+y.^2./x/4+z.^2./y+2./z;主程序: [U,fmin]=fminsearch('fxyz',[0.5,0.5,0.5])3>>X=linspace(0,2*pi,50);>>Y=sin(X);>>P=polyfit(X,Y,3)>>AX=linspace(0,2*pi,50);>>Y=sin(X);>>Y1=polyval(P,X)>>plot(X,Y,':O',X,Y1,'-*')4>>x=0:2.5:10;>>h=[0:30:60]';>>T=[95,14,0,0,0;88,48,32,12,6;67,64,54,48,41];>>xi=[0:0.5:10];>>hi=[0:10:60]';>>temps=interp2(x,h,T,xi,hi,'cubic');>>mesh(xi,hi,temps);第六章 1>>syms x >>y=finverse(1/tan(x))2>>syms x y >>f=1/(1+x^2);g=sin(y);>>fg=compose(f,g)3>>syms x >>g=(exp(x)+x*sin(x))^(1/2);>>dg=diff(g)4>>F=int(int('x*exp(-x*y)','x'),'y')5>>syms x >>F=ztrans(x*exp(-x*10))6>>a=[0 1;-2-3];>>syms s >>inv(s*eye(2)-a);7>>f=solve('a*x^2+b*x+c')8>>f=solve('x+y+z=1','x-y+z=2','2*x-y-z=1')9>>y=dsolve('D2y+2*Dy+2*y=0','y(0)=1','Dy(0)=0')>>ezplot(y),grid on 10>>a=maple('simplify(sin(x)^2+cos(x)^2);')11>>f=maple('laplace(exp(-3*t)*sin(t),t,s);') 12>>syms t x >>F=sin(x*t+2*t);>>L=laplace(F)第七章 第八章 1-1>>h=tf([5,0],[1,2,2])1-2>>s = tf('s');>>H = [5/(s^2+2*s+2)];>>H.inputdelay =2 1-3>>h=tf([0.5,0],[1,-0.5,0.5],0.1)2>>num=2*[1,0.5];den=[1,0.2,1.01];>>sys=tf(num,den)>>[z,p,k]=tf2zp(num,den);>>zpk(z,p,k)>>[A,B,C,D]=tf2ss(num,den);>>ss(A,B,C,D)3 >>num=[1,5];den=[1,6,5,1];ts=0.1;>>sysc=tf(num,den);>>sysd=c2d(sysc,ts,'tustin') >>r1=1;r2=2;c1=3;c2=4;>>[A,B,C,D]=linmod('x84');>>[num,den]=ss2tf(A,B,C,D);>>sys=tf(num,den)5>>A=[1,1,0;0,1,0;0,0,2];B=[0,0;1,0;0,-2];>>n=size(A)>>Tc=ctrb(A,B);if n==rank(Tc)disp('系统完全能控');else disp('系统不完全能控');end 第九章 1>>num=[2,5,1];den=[1,2,3];>>bode(num,den);grid on;>>figure;>>nyquist(num,den);2>>num=5*[1,5,6];den=[1,6,10,8];>>step(num,den);grid on;>>figure;>>impulse(num,den);grid on;3>>kosi=0.7;wn=6;>>num=wn^2;den=[1,2*kosi*wn,wn^2];>>step(num,den);grid on;>>figure;>>impulse(num,den);grid on;4 M文件: function [rtab,info]=routh(den)info=[];vec1=den(1:2:length(den));nrT=length(vec1);vec2=den(2:2:length(den)-1);rtab=[vec1;vec2, zeros(1,nrT-length(vec2))];for k=1:length(den)-2, alpha(k)=vec1(1)/vec2(1);for i=1:length(vec2), a3(i)=rtab(k,i+1)-alpha(k)*rtab(k+1,i+1); end if sum(abs(a3))==0 a3=polyder(vec2);info=[info,'All elements in row ',...int2str(k+2)' are zeros;'];elseif abs(a3(1)) rtab=[rtab;a3, zeros(1,nrT-length(a3))];vec1=vec2;vec2=a3;end 主程序: >>den=[1,2,8,12,20,16,16];>>[rtab,info]=routh(den)>>a=rtab(:,1)if all(a>0)disp('系统是稳定的');else disp('系统是不稳定的');end 5>>num=7*[1,5];den=conv([1,0,0],conv([1,10],[1,1]));>>[gm,pm,wg,wc]=margin(num,den)第十章 M文件: function varargout=rg_lead(ng0,dg0,s1)if nargout==1 ngv=polyval(ng0,s1);dgv=polyval(dg0,s1);g=ngv/dgv;thetal=pi-angle(g);zc=real(s1)-imag(s1)/tan(thetal);t=-1/zc;varargout{1}=[t,1];elseif nargout==2 ngv=polyval(ng0,s1);dgv=polyval(dg0,s1);g=ngv/dgv;theta=angle(g);phi=angle(s1);if theta>0 phi_c=pi-theta;end if theta<0;phi_c=-theta end theta_z=(phi+phi_c)/2;theta_p=(phi-phi_c)/2;z_c=real(s1)-imag(s1)/tan(theta_z);p_c=real(s1)-imag(s1)/tan(theta_p);nk=[1-z_c];varargout{2}=[1-p_c];kc=abs(p_c/z_c);if theta<0 kc=-kc end varargout{1}=kc*nk;else error('输出变量数目不正确!');end 主程序: >> ng0=[1];dg0=10000*[1 0-1.1772];>>g0=tf(ng0,dg0);%满足开环增益的为校正系统的传递函数 >>s=kw2s(0.7,0.5)%期望的闭环主导极点 >>ngc=rg_lead(ng0,dg0,s);>>gc=tf(ngc,1)>>g0c=tf(g0*gc);>>rlocus(g0,g0c);>>b1=feedback(g0,1);%未校正系统的闭环传递函数 >>b2=feedback(g0c,1);%校正后系统的闭环传递函数 >>figure,step(b1,'r--',b2,'b');grid on %绘 2 M文件: function [ngc,dgc,k]=rg_lag(ng0,dg0,KK,s1,a)ngv=polyval(ng0,s1);dgv=polyval(dg0,s1);g=dgv/ngv;k=abs(g);%期望主导极点处的根轨迹增益 beta=k/KK;[kosi1,wn1]=s2kw(s1);zc=-wn1*sin(a*pi/180)/sin(pi-atan(sqrt(1-kosi1^2)/kosi1)-(a*pi/180));%利用正弦定理 pc=beta*zc;ngc=beta*[1,-zc];dgc=[1,-pc];主程序: >>KK=20;s1=-2+i*sqrt(6);a=1 >>ng0=[10];dg0=conv([1,0],[1,4]);>>g0=tf(ng0,dg0);>>[ngc,dgc,k]=rg_lag(ng0,dg0,KK,s1,a);>>gc=tf(ngc,dgc)>>g0c=tf(KK*g0*gc);>>b1=feedback(k*g0,1);>>b2=feedback(g0c,1);>>step(b1,'r--',b2,'b');grid on M文件: function [ngc,dgc,k]=rg_lag(ng0,dg0,KK,s1,a)ngv=polyval(ng0,s1);dgv=polyval(dg0,s1);g=dgv/ngv;k=abs(g);%期望主导极点处的根轨迹增益 beta=k/KK;[kosi1,wn1]=s2kw(s1);zc=-wn1*sin(a*pi/180)/sin(pi-atan(sqrt(1-kosi1^2)/kosi1)-(a*pi/180));%利用正弦定理 pc=beta*zc;ngc=beta*[1,-zc];dgc=[1,-pc];主程序: >>KK=128;s1=-2+i*2*sqrt(3);a=2 >>ng0=[10];dg0=conv([1,0],conv([1,2],[1,8]));>>g0=tf(ng0,dg0);>>[ngc,dgc,k]=rg_lag(ng0,dg0,KK,s1,a);>>gc=tf(ngc,dgc)>>g0c=tf(KK*g0*gc);>>rlocus(g0,g0c);>>b1=feedback(k*g0,1);>>b2=feedback(g0c,1);>>figure,step(b1,'r--',b2,'b');grid on 4 M文件: function [ngc,dgc]=lead4(ng0,dg0,KK,Pm,w)[mu,pu]=bode(KK*ng0,dg0,w);[gm,pm,wcg,wcp]=margin(mu,pu,w);alf=ceil(Pm-pm+5);phi=(alf)*pi/180;a=(1+sin(phi))/(1-sin(phi)), dbmu=20*log10(mu);mm=-10*log10(a);wgc=spline(dbmu,w,mm), T=1/(wgc*sqrt(a)), ngc=[a*T,1];dgc=[T,1];主程序: >>ng0=[1];dg0=conv([1,0,0],[1,5]);>>g0=tf(ng0,dg0);>>w=logspace(-3,3);>>KK=1;Pm=50;>>[ngc,dgc]=lead4(ng0,dg0,KK,Pm,w);>>gc=tf(ngc,dgc);g0c=tf(KK*g0*gc);>>bode(KK*g0,w);hold on,bode(g0c,w);grid on,hold off >>[gm,pm,wcg,wcp]=margin(g0c)>>Kg=20*log10(gm)>>g1=feedback(g0c,1);>>bode(g1),grid on, >>[mag,phase,w]=bode(g1);>>a=find(mag<=0.707*mag(1));>>wb=w(a(1)) >>max(mag)>>b=find(mag==max(mag))>>wr=w(b)5 M文件: function [ngc,dgc]=fg_lead_pm(ng0,dg0,Pm,w)[mu,pu]=bode(ng0,dg0,w);%计算原系统的对数频率响应数据 [gm,pm,wcg,wcp]=margin(mu,pu,w);%求取原系统的相角裕度和剪切频率 alf=ceil(Pm-pm+5);%计算控制器提供的最大超前角度,phi=(alf)*pi/180;%将最大超前角转换为弧度单位 a=(1+sin(phi))/(1-sin(phi));%计算a值 dbmu=20*log10(mu);%系统的对数幅值 mm=-10*log10(a);%wm处的控制器对数幅值 wgc=spline(dbmu,w,mm);%差值求取wm,认为wm=wc T=1/(wgc*sqrt(a));%计算T ngc=[a*T,1];dgc=[T,1];主程序: >>KK=40;Pm=50;>>ng0= KK *[1];dg0=conv([1,0],conv([1,1],[1,4]));>>g0=tf(ng0,dg0);>>w=logspace(-2,4);>>[ngc,dgc]=fg_lead_pm(ng0,dg0,Pm,w)>>gc=tf(ngc,dgc),g0c=tf(g0*gc);>>b1=feedback(g0,1);b2=feedback(g0c,1);>>step(b1,'r--', b2,'b');grid on >>figure, bode(g0,'r--',g0c,'b',w), grid on, >>[gm,pm,wcg,wcp]=margin(g0c), Km=20*log10(gm)第五篇:控制系统的MATLAB仿真与设计课后答案