收藏 分销(赏)

面向数学建模的MATLAB基础REV市公开课一等奖百校联赛特等奖课件.pptx

上传人:快乐****生活 文档编号:11701891 上传时间:2025-08-06 格式:PPTX 页数:76 大小:1.02MB 下载积分:16 金币
下载 相关 举报
面向数学建模的MATLAB基础REV市公开课一等奖百校联赛特等奖课件.pptx_第1页
第1页 / 共76页
面向数学建模的MATLAB基础REV市公开课一等奖百校联赛特等奖课件.pptx_第2页
第2页 / 共76页


点击查看更多>>
资源描述
,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,*,Click to edit Master title style,面向数学建模,MATLAB,基础,第1页,MATLAB,环境,Command Window(命令窗口),Current Folder(当前工作路径),Workspace 和Command History,2,第2页,取得系统帮助,help,help sin,doc,doc eye,lookfor,lookfor identity,3,第3页,使用MATLAB程序,脚本(,Scripts,),按一定次序排列可执行命令集合,扩展名为,.m,不要使用数字作为文件名,在命令行中新建脚本文件,edit print.m%,调用,fprintf,函数,或者点击,快捷键,ctrl+N,4,第4页,在一个脚本中全部被创建和修改变量会一直存在于工作区(,workspace,)中,即使该脚本已经停顿运行,同一命令在脚本中运行与在命令窗口中运行没有本质区分。,5,第5页,简单输出,disp,函数,输出变量值、输出字符串,比如,disp(Hello World!);,disp(I am going to learn MATLAB!);,x=100;,disp(x);,6,第6页,变量类型,需要创建变量时,能够直接对其赋值,不用预先定义!,最惯用类型:,double,:,5.8,var1=3.14,字符型:,a,myString=hello world,7,第7页,变量和命名规则,变量名命名规则:,第一个字符必须是英文字母,由英文字母、数字和下划线组成,大小写敏感,内建变量,i,和,j,为虚数单位,pi,为圆周率,ans,自动存放最近一次未被保留运算结果,Inf,、,-Inf,分别为正、负无穷,8,第8页,标量,定义方法,取名,然后赋值,a=10,c=1.3*45-2*a,cool_dude=13/3;,注意:分号能够用来抑制输出,9,第9页,数组,两种类型数组,矩阵,每个元素都是数值(实数或复数),元胞数组,元素能够有不一样类型,10,第10页,向量,定义向量,行向量,row=1 2 5.4-6.6,row=1,2,5.4,-6.6;,列向量,column=4;2;7;4,11,第11页,使用size和length函数,计算给定向量维数,size,length,12,第12页,矩阵,定义矩阵,给出各个元素值,M=1 2 3;4 5 6,;,对已经有矩阵进行拼接,13,第13页,基本标量运算,算术运算,(,+,-,*,/,),7/45,(1+i)*(2+i),指数运算,(),42,(3+4*j)2,括号不能隐式地表示乘法运算,3(1+0.7)gives an error,To clear command window,clc,14,第14页,内建函数,MATLAB,中提供了海量内建函数,利用括号进行调用,sqrt(2),log(2),log10(0.23),cos(1.2),atan(-.8),exp(2+4*i),round(1.4),floor(3.3),ceil(4.23),angle(i);abs(1+i);,15,第15页,转置,矩阵转置,a=1 2 3 4+i,transpose(a),a,a.,对于实数矩阵来说,,.and,运算结果相同,而对于复数矩阵,,还要进行共轭运算。,16,第16页,向量化,逐元素进行运算,两个运算对象必须维数相同,除非有一个是标量。,全部可用于标量函数也可用于向量,t=1 2 3;,f=exp(t);,等同于,f=exp(1)exp(2)exp(3);,17,第17页,运算符,(,*/,),有两种运算模式,逐一元素(,element-wise,),标准模式,18,第18页,Element-wise,use the dot:,.,(,.*,./,.,).,两运算对象维数必须相同,除非有一个是标量,19,第19页,标准模式,标准乘法*,线性代数规则,注意,:,矩阵维数要相匹配!,标准指数运算,只能作用于方阵或标量,左除,/and,右除,右除,:相当于乘以运算对象逆,20,第20页,矩阵自动初始化,惯用函数,ones,O=ones(4,5),zeros,Z=zeros(6,6),eye,I=eye(7),rand,R=rand(2,8),元素服从,(0,1),均匀分布,21,第21页,向量生成,linspace,a=linspace(0,10,5),0,为起点,,10,为终点,共,5,个数,等差数列,冒号运算符,:,b=0:2:10,0,为起点,增量为,2,,不超出,10,增量能够是小数或负数,c=1:5,默认增量为,1,22,第22页,向量元素检索方法,向量元素下标从,1,开始,a(n),为,a,中第,n,个元素,下标参数能够是向量,x=12 13 5 8;,a=x(2:3);,b=x(1:end-1);,a(1),a(2),a(3),a(4),a=13 5 9 10,a=13 5;,b=12 13 5;,23,第23页,矩阵元素检索方法,两种方式,利用元素行数和列数(行优先),利用元素编号,选取子阵,A=rand(5),A(1:3,1:2),A(1 5 3,1 4),24,第24页,冒号通配符,选取整行或整列,使用冒号:,25,第25页,有用函数,求最大值、最小值,max,min,查找函数,find,vec=5 3 1 9 7;,end=find(vec=9);,ind=find(vec 2,26,第26页,绘图功效,已知,n,个点,可使用,plot,函数绘制曲线,各点之间用直线连接,x=linspace(0,4*pi,10);,y=sin(x);,plot(x,y);,x=linspace(0,4*pi,1000);,plot(x,sin(x);,27,第27页,Plot,参数选项,能够经过字符串参数指定线条颜色,,marker,格调和线条格调,plot(x,y,k.-);,Look at,help plot,for a full list of colors,markers,andlinestyles,color,marker,line-style,28,第28页,三维和其它绘图方式,其它绘图函数,plot3,surf,见,peaks,mesh,ezplot,ezplot3,29,第29页,高级数据结构,前面学习了二维矩阵,能够扩展到,n,维,每个元素类型必须相同(如整型,字符型),存放高效、计算方便,含有许多零大矩阵可被转化为稀疏矩阵,a=zeros(100);a(1,3)=10;a(21,5)=pi;,b=sparse(a);,30,第30页,元胞数组和结构体,在一些情况下,使用更复杂数据结构愈加方便,Cell array,:,与数组类似,但元素能够含有不一样类型,Structs,:,能够将变量名和变量值绑定在一起,31,第31页,Cells:organization,元胞非常类似矩阵,,但其每个,field,能够容纳任何东西(甚至其它矩阵):,一个元胞能够包含:人名,年纪和他孩子年纪。,32,第32页,Cells:initialization,能够经过指定,size,来自动初始化一个元胞,a=cell(3,10);,a will be a cell with 3 rows and 10 columns,或者使用花括号,进行手动初始化,c=hello world,1 5 6 2,rand(3,2);,c is a cell with 1 row and 3 columns,Each element of a cell can be,anything,使用花括号来访问元胞中元素,a1,1=1 3 4-10;,a2,1=hello world 2;,a1,2=c3;,33,第33页,Structs,Structs,允许我们命名一些变量并将其绑在一起,要初始化一个空,struct,:,s=struct();,初始化不是必须,增加,fields,:,s.name=Jack Bauer;,s.scores=95 98 67;,s.year=G3;,Fields can be anything:matrix,cell,even struct,For more information,see,doc struct,34,第34页,工作区变量保留、读入和去除,save,可将工作区中部分或全部变量保留到文件中,save myfile x y,save myfile,load,load myfile,clear,去除部分或全部变量,clear x y,clear all,35,第35页,数据输入输出,MATLAB,提供了一些接口函数,能够方便经过文件进行数据输入输出,dlmwrite,能够把数据写为,.txt,格式数据,而且数和数之间间隔符号能够自己定义,如空格,逗号等等。,dlmread,读取,.txt,格式文件。,xlswrite,把数据直接写到一个,excel,文件中。,xlsread,能够从,excel,文件中读数据。,36,第36页,dlmwrite,和,dlmread,将矩阵,M,存入文件,myfile.txt,中,数据分隔符为,tab,,精度为,6,位有效数字,:,dlmwrite(myfile.txt,M,delimiter,t,.precision,6),0.8938980.2844090.5827920.432907 0.1991380.4692240.4234960.22595 0.2987230.06478110.5155120.579807 0.6614430.9883350.3339510.760365,从文件,myfile.txt,中读取数据,并存入,M,M=dlmread(myfile.txt),函数使用方法详见,help,文档,37,第37页,importdata函数,设文本文件,textFile.txt,中含有数据,:,利用,importdata,来导入数据,a=importdata(textFile.txt);,a,是结构体,包含,data,textdata,和,colheaders,x=a.data;,names=a.colheaders,38,第38页,importdata函数,使用,importdata,时,能够指定分隔符,a=importdata(filename,);,指定逗号为分隔符,假如需要更灵活读取数据,能够使用,fscanf,(similar to C/Java),textread,textscan,.,See,help,or,doc,for information on how to use these functions,39,第39页,写入,Excel Files,xlswrite,函数,xlswrite(randomNumbers,rand(10,4),.,Sheet1);,Sheet1,为指定表单名称,假如有一堆混合类型数据,能够将其组成元胞数组再写入文件,C=hello,goodbye;10,-2;-3,4;,xlswrite(randomNumbers,C,.,mixedData);,40,第40页,读取,Excel Files,xlsread,函数,num,txt,raw=xlsread(randomNumbers.xls);,读取第一个表单,num,中为数字,txt,中为字符串,raw,包含整个,元胞数组,内容,num,txt,raw=xlsread(randomNumbers.xls,.,mixedData);,Reads the mixedData sheet,num,txt,raw=xlsread(randomNumbers.xls,-1);,Opens the file in an Excel window and lets you click on the data you want!,41,第41页,导入数据最轻易方式是使用,Import Wizard,一个图形用户接口,Import Wizard,允许直接控制要创建变量,导入向导位置,File Import Data,42,第42页,符号计算,告别讨厌手工演算过程!,Advantages,Disadvantages,Symbolic,解析解,给出解直观形式,有时不可解,解形式可能相当复杂,Numeric,总能得到一个解,能确保解准确性,易于编程实现,对解深入了解比较困难,有时数值方法会失效,可能花费较长时间来计算,Symbolics vs.Numerics,43,第43页,符号变量,符号变量一样含有类型(类型为符号),类似,double,或,char,使用,sym,定义符号变量,a=sym(1/3);,b=sym(4/5);,分数不会被求值,mat=sym(1 2;3 4);,c=sym(c,positive);,能够添加标识以缩小取值范围,see,help sym,for a list of tags,44,第44页,另一个使用方法,使用,syms,syms x y real,等价于,x=sym(x,real);y=sym(y,real);,45,第45页,符号表示式,乘法、加法、除法表示式,d=a*b,即,1/3*4/5=4/15;,expand(a-c)2);,展开,factor(ans),因式分解,inv(mat),以符号形式计算逆矩阵,46,第46页,几个函数,pretty(ans),makes it look nicer,collect(3*x+4*y-1/3*x2-x+3/2*y),合并同类项,simplify(cos(x)2+sin(x)2),化简,subs(c2,c,5),将符号变量替换为数值或表示式,subs(c2,c,x/7),ans=,25,ans=,1/49*x2,47,第47页,矩阵能够实现符号运算,mat=sym(a b;c d);,mat2=mat*1 3;4-2;,计算乘积,d=det(mat),计算行列式,i=inv(mat),计算逆矩阵,一样矩阵元素访问方法,i(1,2),48,第48页,例子,已知圆方程,求解,y,syms a b r x y,solve(x-a)2+(y-b)2=r2,y),计算,syms a b x,Q=int(x*exp(x),a,b),subs(Q,a,b,0,2),49,第49页,编写函数,函数很像脚本,but for ONE difference,函数必须有函数申明!,Outputs,Inputs,Function declaration,Help file,50,第50页,If more than one output,User-defined Functions,关于,function declaration,几点说明:,No need for return:,MATLAB returns,申明中出现输出变量,任何函数内创建变量仅限于函数内使用,它们不会被返回,函数运行结束后即消失,function,x,y,z=,funName,(,in1,in2,),Must have the reserved,Function name should,word:function,match MATLAB file,name,must be in brackets,Inputs must be specified,51,第51页,例子,已知函数申明为,function plotSin(k),在,0,2,上绘制一个正弦波,sin(kx),在文件,plotSin.m,中,write the following:,function plotSin(k),x=linspace(0,2*pi,k*20);,figure,plot(x,sin(k*x),52,第52页,程序流程控制,关系运算符,流程控制语句,53,第53页,关系运算符,MATLAB uses,mostly,standard relational operators,equal,not,equal,greater than,less than,greater or equal,less or equal,=,=,=,=,short-circuit(scalars),&,|,Logical operators,And,Or,Not,Xor,All true,Any true,elementwise,&,|,xor,all,any,Boolean values:zero is false,nonzero is true,See,help.,for a detailed list of operators,54,第54页,if/else/elseif,Basic flow-control,common to all languages,MATLAB syntax is somewhat unique,IF,if,cond,commands,end,ELSE,if,cond,commands1,else,commands2,end,ELSEIF,if,cond1,commands1,elseif,cond2,commands2,else,commands3,end,No need for parentheses:,command blocks are between,reserved words,Conditional statement:,evaluates to true or false,55,第55页,for,for,loops:,主要应用于已知循环次数情形,MATLAB syntax:,for,n=1:100,commands,end,The loop variable,被定义为向量,在,command block,中作为标量使用,无须为连续整数,The command block,Anything between the,for,line and the,end,Loop variable,Command block,56,第56页,while,The while is like a more general for loop:,主要应用于循环次数未知情形,The command block will execute while the conditional,expression is true,防止死循环!,WHILE,while,cond,commands,end,57,第57页,方程组求解,考虑求解线性方程组,x+2y-3z=5,-3x-y+z=-8,x-y+z=0,结构矩阵,将方程组改写为,Ax=b,A=1 2-3;-3-1 1;1-1 1;,b=5;-8;0;,And solve with a single line of code!,x=Ab;,x is a 3x1 vector containing the values of x,y,and z,The,will work with square or rectangular systems.,Gives least squares,(最小二乘),solution for rectangular systems.,Solution depends on whether the system is over or underdetermined,(超定或欠定),.,MATLAB makes linear,algebra fun!,58,第58页,More Linear Algebra,Given a matrix,mat=1 2-3;-3-1 1;1-1 1;,Calculate the rank of a matrix,r=rank(mat);,Calculate the determinant,d=det(mat);,mat must be square,if determinant is nonzero,matrix is invertible,(,可逆,),Get the matrix inverse,E=inv(mat);,if an equation is of the form A*x=b with A a square matrix,x=Ab is the same as x=inv(A)*b,59,第59页,Matrix Decompositions,MATLAB,提供了惯用矩阵分解方法,The most common ones are,V,D=eig(X),Eigenvalue decomposition,U,S,V=svd(X),Singular value decomposition,Q,R=qr(X),QR decomposition,60,第60页,Exercise:Linear Algebra,Solve the following systems of equations:,System 1:,System 2:,x,+,4,y,=,34,3,x,+,y,=,2,2,x,2,y,=,4,x,+,y,=,3,3,x,+,4,y,=,2,61,第61页,Exercise:Linear Algebra,Solve the following systems of equations:,System 1:,x,+,4,y,=,34,3,x,+,y,=,2,System 2:,2,x,2,y,=,4,x,+,y,=,3,3,x,+,4,y,=,2,A=1 4;-3 1;,b=34;2;,rank(A),x=inv(A)*b;,A=2-2;-1 1;3 4;,b=4;3;2;,rank(A),rectangular matrix,x1=Ab;,gives least squares solution,error=abs(A*x1-b),62,第62页,多项式(,Polynomials,),许多函数能够被近似为高阶多项式,MATLAB,利用系数向量来表示多项式,if vector P describes a polynomial,ax,3,+bx,2,+cx+d,P=1 0-2 represents the polynomial x,2,-2,P=2 0 0 0 represents the polynomial 2x,3,P(1),P(2),P(3),P(4),63,第63页,Polynomial Operations,设,P,描述一个,N,阶多项式,显然它是长为,N+1,向量,To get the roots of a polynomial,r=roots(P),r is a vector of length N,Can also get the polynomial from the roots,P=poly(r),r is a vector length N,求给定点多项式值,y0=polyval(P,x0),x0 is a single value;y0 is a single value,To evaluate a polynomial at many points,y=polyval(P,x),y=polyval(1 0 1,4 5),x is a vector;y is a vector of the same size,y0=polyval(1 0 1,4),64,第64页,多项式拟合(,Polynomial Fitting,),MATLAB,能够轻松实现给定数据多项式拟合,Given data vectors X=-1 0 2 and Y=0-1 3,p2=polyfit(X,Y,2);,finds the best second order polynomial that fits the points,(-1,0),(0,-1),and(2,3),see,help polyfit,for more information,plot(X,Y,o,MarkerSize,10);,hold on;,x=-3:.01:3;,plot(x,polyval(p2,x),r-);,65,第65页,Exercise:Polynomial Fitting,x=-4:0.1:4;,y=x.2;,给采样点添加随即噪声,.Use,randn,.,Plot the noisy signal with,.,markers,y=y+randn(size(y);,plot(x,y,.);,Fit a 2,nd,degree polynomial to the noisy data,p=polyfit(x,y,2);,Plot the fitted polynomial on the same plot,using the same,x values and a red line,hold on;,plot(x,polyval(p,x),r),Evaluate,for x=-4:0.1:4.,y,=,x,2,66,第66页,优化问题,非线性方程求根,求函数最值,优化工具箱,67,第67页,Nonlinear Root Finding,许多实际问题可归结为求解方程,f(x)=0,Can use,fzero,to calculate roots for,any,arbitrary function,fzero,needs a function passed to it.,Make a separate function file,x=fzero(myfun,1),x=fzero(myfun,1),1 specifies a,point close to where,you think the root is,Courtesy of The MathWorks,Inc.Used with permission.,68,第68页,Minimizing a Function,fminbnd,:,在有界区间内寻找最小值点,x=fminbnd(myfun,-1,2);,myfun takes a scalar input and returns a scalar output,myfun(x)will be the minimum of myfun for-1,x,2,fminsearch,:unconstrained interval,x=fminsearch(myfun,.5),finds the local minimum of myfun near 0.5,69,第69页,Anonymous Functions,(匿名函数),假如函数相当简单,那么无须单独创建文件去定义它,x=fzero(myfun,1),What if myfun is really simple?,Instead,you can make an anonymous function,x=fzero(,(x)(cos(exp(x)+x2-1),1 );,x=fminbnd(,(x)(cos(exp(x)+x2-1),-1,2);,input,function to evaluate,70,第70页,Optimization Toolbox,If you are familiar with optimization methods,use the,optimization toolbox,Useful for larger,more structured optimization problems,Sample functions(see,help,for more info),linprog,linear programming using interior point methods,quadprog,quadratic programming solver,graphshortestpath,solves the shortest path problem in graph,71,第71页,应用一、用,linprog(),求解线性规划模型,x=linprog(f,A,b,Aeq,beq,LB,UB),目标函数系数,约束矩阵,右端项,解,x,上界和下界,注意:模型中向量全部为列向量,假如求最大化问题需要转化,72,第72页,线性规划详细例子,一个线性规划模型归纳为:,f=3 1 3;%,转为列向量,A=2 1 1;1 2 3;2 2 1;,b=2 5 6;,LB=zeros(3,1);,x,=linprog(-f,A,b,LB),z=f*x%,目标函数值,73,第73页,应用二、用,quadprog(),求解二次规划模型,x=quadprog(H,f,A,b,Aeq,beq,LB,UB),目标函数系数矩阵,约束矩阵,右端项,解,x,上界和下界,74,第74页,二次规划详细例子,一个二次规划模型归纳为:,H=4-2 0 0;-2 4 0 0;0 0 0 0;0 0 0 0;,f=-4;6;0;0;,A=;,b=;,Aeq=1 1 1 0;1 5 0 1;,beq=2;5;,LB=zeros(size(f);,x=QUADPROG(H,f,A,b,Aeq,beq,LB,UB),z=0.5*x*H*x+f*x,75,第75页,Thank You!,第76页,
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服