ImageVerifierCode 换一换
格式:DOCX , 页数:19 ,大小:98.86KB ,
资源ID:6388856      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6388856.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(matlabR第六章答案.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

matlabR第六章答案.docx

1、matlabR第六章答案第 6 章 M文件和函数句柄.1 MATLAB控制流.1.1 if-else-end条件控制【例6.1-1】(1)function y=exm060101(x)% n=length(x);for k=1:n if x(k)=1 y(k)=exp(1-x(k); else y(k)=x(k)3; endend(2)x=-2,-1.2,-0.4,0.8,1,6y=exm060101(x) x = -2.0000 -1.2000 -0.4000 0.8000 1.0000 6.0000y = -2.0000 -1.2000 -0.0640 0.5120 1.0000 0.00

2、67 .1.2 switch-case控制结构【例6.1-2】clear;% for k=1:10 a(k)=89+k;b(k)=79+k;c(k)=69+k;d(k)=59+k;end;c=d,c;% A=cell(3,5); %A(1,:)=Jack,Marry,Peter,Rose,Tom;% A(2,:)=72,83,56,94,100; % % for k=1:5 switch A2,k % case 100 % r=满分; case a % r=优秀; case b % r=良好; case c % r=及格; otherwise % r=不及格; end A(3,k)=r; en

3、dAA = Jack Marry Peter Rose Tom 72 83 56 94 100 及格 良好 不及格 优秀 满分 .1.3 for循环和while循环【例6.1-3】 (1)K=5;A = zeros(K,K) ; %for m = 1:K % for n = 1:K % A(m,n) = 1/(m+n -1); endendformat ratAformat short g A = 1 1/2 1/3 1/4 1/5 1/2 1/3 1/4 1/5 1/6 1/3 1/4 1/5 1/6 1/7 1/4 1/5 1/6 1/7 1/8 1/5 1/6 1/7 1/8 1/9 (

4、2)%cleartic %K=1000;for m = 1:K for n = 1:K A1(m,n) = 1/(m+n -1); endendt1=toc %t1 = 17.434 %ticK=1000;A2 = zeros(K,K); %for m = 1:K for n = 1:K A2(m,n) = 1/(m+n -1); endendt2=toc t2 = 2.1895 (3)%ticN=1000;n=repmat(1:N,N,1);m=n;A3=1./(n+m-1);t3=toc t3 = 0.13909 【例6.1-4】(1)function S,N=exm060104(epsi

5、lon)%k=0;s=0;d=inf;S=0;while depsilon k=k+1; s=s+k; % d=1/s; S=S+d;endN=k;(2)S,N=exm060104(0.0001) S = 1.9859N = 141 syms k n NSINF=limit(symsum(1/symsum(k,1,n),n,1,N),N,inf) SINF =2 N=141;SN=vpa(symsum(1/symsum(k,1,n),n,1,N) SN =1.9859154929577464788732394366197 【例6.1-5】 (1)clearclcwhile 1 % n=inpu

6、t(请输入一个能被4整除的正整数! n = ); if mod(n,4)=0 % break % end %end %G=logical(eye(4,4)+rot90(eye(4,4); % m=n/4;K=repmat(G,m,m); %N=n2;A=reshape(1:N,n,n); A(K)=N-A(K)+1 % 请输入一个能被4整除的正整数! n = A = 144 13 25 108 96 61 73 60 48 109 121 12 2 131 119 38 50 83 71 86 98 35 23 134 3 130 118 39 51 82 70 87 99 34 22 135

7、 141 16 28 105 93 64 76 57 45 112 124 9 140 17 29 104 92 65 77 56 44 113 125 8 6 127 115 42 54 79 67 90 102 31 19 138 7 126 114 43 55 78 66 91 103 30 18 139 137 20 32 101 89 68 80 53 41 116 128 5 136 21 33 100 88 69 81 52 40 117 129 4 10 123 111 46 58 75 63 94 106 27 15 142 11 122 110 47 59 74 62 95

8、 107 26 14 143 133 24 36 97 85 72 84 49 37 120 132 1 (2)s0=round(n*(n*n+1)/2); % disp(int2str(n), 阶魔方矩阵的标称和是 ,int2str(s0)Ns0=round(2*(n+1); % B=A;SC=sum(A); % SR=sum(B); % Sd=sum(diag(A); % Sdi=sum(diag(B); % LS=SC,SR,Sd,Sdi=s0; % NS=round(sum(LS); %if NS=Ns0 disp(经验证,A是魔方矩阵。)else disp(经验证,A不是魔方矩阵。)

9、end 12 阶魔方矩阵的标称和是 870经验证,A是魔方矩阵。 .1.4 控制程序流的其它常用指令.2 脚本文件和函数文件.2.1 M脚本文件.2.2 M函数文件.2.3 局部变量和全局变量.2.4 M函数文件的一般结构【例6.2-1】(1)function S,L=exm060201(N,R,str)% switch nargin case 0 N=100;R=1;str=-b; case 1 R=1;str=-b; case 2 str=-b; case 3 ; % otherwise error(输入量太多。);end;t=0:2*pi/N:2*pi;x=R*sin(t);y=R*co

10、s(t); if nargout=0 plot(x,y,str);elseif nargout2 error(输出量太多。);else S=N*R*R*sin(2*pi/N)/2; % L=2*N*R*sin(pi/N); % fill(x,y,str)endaxis equal squarebox onshg (2) S,L=exm060201(6,2,-g) %S = 10.3923L = 12.0000图 6.2-1 绿色正六边形.3 MATLAB的函数类别.3.1 主函数(Primary function).3.2 子函数(Subfunction)【例6.3-1】(1)function

11、 Hr=exm060301(flag )% t=(0:50)/50*2*pi;x=sin(t);y=cos(t);Hr=cirline; % feval(Hr,flag,x,y,t)% function cirline(wd,x,y,t)% switch wdcase line plot(t, x, b ,t , y, r, LineWidth, 2)case circle plot(x, y, -g, LineWidth, 8), axis square offotherwise error(输入宗量只能取 line 或 circle ! )endshg(2) HH=exm060301(ci

12、rcle) HH = cirline图 6.3-1 绿色圆周线(3) t=0:2*pi/5:2*pi;x=cos(t);y=sin(t); % HH(circle,x,y,t) %图 6.3-2 由子函数绘制的绿色正五边形.3.3 匿名函数(Anonymous function).4 函数句柄.4.1 函数句柄的创建和观察【例6.4-1】(1)hm=magic hm = magic (2)class(hm)isa(hm, function_handle) ans =function_handleans = 1 (3)CC=functions(hm) CC = function: magic t

13、ype: simple file: C:MATLAB R2010atoolboxmatlabelmatmagic.m (4)M1=hm(4) M1 = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 (5)M2=feval(hm,4) M2 = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 .4.2 函数句柄的基本用法【例6.4-2】(1)S,L=exm060201(3,2,-r) % S = 5.1962L = 10.3923图 6.4-1 直接调用函数exm060201所画的红色等边三角形(2)Hexm=exm060201, %

14、 Hexm = exm060201 (3)which(exm060201) exm060201 not found. (5)S,L=exm060201(3,2,-r) ? Undefined command/function exm060201. (6)S,L=Hexm(3,2,-r) % S = 5.1962L = 10.3923图 6.4-2 在新视野中里用函数句柄Hexm所画的红色等边三角形【例6.4-3】(1)clear (2)Hexm_2=exm060201 Hexm_2 = exm060201 (3)class(Hexm_2)isa(Hexm_2,function_handle)

15、ans =function_handleans = 1 (4)S,L=Hexm_2(3,2,-r) ? Undefined command/function exm060201. 习题61. 请分别写出用for 和while 循环语句计算的程序。此外,还请写出避免循环的数值、符号计算程序。(提示:sum和“指数采用数组”配合; tic, toc可用以记录计算所花的时间。)1. for循环求解tica=0;for i=0:1000000; a=a+0.2i;endat1=toca = 1.2500t1 =0.1316While循环求解:clearticK=0;i=0;while i=100000

16、0 K=K+0.2i; i=i+1;endKt2=toc K = 1.2500t2 = 0.1407数值法求解:tici=0:1000000;d=0.2.i;K2=sum(d)t3=tocK2 = 1.2500t3 = 0.0981符号法求解:clearticsyms ifi=0.2i;K=vpa(symsum(fi,i,0,inf)t4=tocK = 1.25 t4 = 3.03572. 编写一个函数M文件,它的功能:没有输入量时,画出单位圆(见图p6-1);输入量是大于2的自然数N时,绘制正N边形,图名应反映显示多边形的真实边数(见图p6-2);输入量是“非自然数”时,给出“出错提示”。此

17、外,函数M文件应有H1行、帮助说明和程序编写人姓名。(提示:nargin, error, int2str)function =xitiliu(N)%画出N边形%xitiliu(),画出单位圆%xitiliu(N),画出大于的N边形switch nargin case 0 N=100; R=1; t=0:2*pi/N:2*pi; x=R*cos(t); y=R*sin(t); plot(x,y,-r,lineWidth,5); title(circle) axis equal axis off shg case 1 if N=round(N)|N=2|N=0 error(请输入大于2的整数) e

18、lseif N=round(N) R=1; t=0:2*pi/N:2*pi; x=R*cos(t); y=R*sin(t); plot(x,y,-r,lineWidth,4) title(poly gon nit,int2str(N),edges) axis equal %axis off shg endendxitiliu(6)3. 用泛函指令fminbnd求在x=0附近的极小值。fminbnd的第一个输入量要求使用匿名函数表达。(提示:注意搜索范围的选择;假如极值在边界附近,进一步扩大搜索范围是合理的选择。).clearsyms xx1=-20;x2=20;yx=(x)(-exp(-x)*

19、abs(sin(cos(x);xn0,feal,exitflag,output=fminbnd(yx,x1,x2)xn0 = -19.7129feal = -2.2031e+008exitflag = 1output = iterations: 16 funcCount: 17 algorithm: golden section search, parabolic interpolation message: 1x112 char4. 在matlab的 toolboxmatlabelmatprivate文件夹上有一个“烟圈矩阵”发生函数smoke.m。运行指令smoke(3,0,double)

20、 ,将生成一个3阶伪特征根矩阵如下A = -0.5000 + 0.8660i 1.0000 0 0 -0.5000 - 0.8660i 1.0000 1.0000 0 1.0000 现在的问题是:在MATLAB当前目录为work情况下,如何利用函数句柄调用smoke.m函数,产生3阶伪特征根矩阵。请写出相应的程序或操作步骤。(提示:注意函数句柄创建的有效性;若想编写能完全自动执行的解题程序,注意使用cd, pwd, which, 及字符串的拆分、合并操作技术。)clear b=pwd which(smoke) b_d=b; b_d(end-4:end)=; str=b_d,toolboxmatlabelmatprivate; cd(str)SM=smoke; cd(b)A=SM(3,0,double) disp( ) pwd which(smoke)

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1