1 / 32

王国利

Matlab 计算与仿真技术 第九讲 : Matlab 编程 -III http://human-robot.sysu.edu.cn/course. 王国利. http://human-robot.sysu.edu.cn. 信息科学与技术学院. 中山大学. 第九讲提纲. 第八讲回顾 Matlab 编程 -III - 变量查询 - 匿名函数 - 参数交互输入 - Matlab 程序调试 - 语言结构与调试函数. 第八讲回顾. 函数的定义及申明 - 使用方式 : 调用 / 嵌套 - 协作方式 : 主函数 / 子函数 / 嵌套函数

duaa
Download Presentation

王国利

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Matlab计算与仿真技术 第九讲: Matlab编程-III http://human-robot.sysu.edu.cn/course 王国利 http://human-robot.sysu.edu.cn 信息科学与技术学院 中山大学

  2. 第九讲提纲 • 第八讲回顾 • Matlab编程-III - 变量查询 - 匿名函数 - 参数交互输入 - Matlab程序调试 - 语言结构与调试函数

  3. 第八讲回顾 • 函数的定义及申明 - 使用方式: 调用/嵌套 - 协作方式: 主函数/子函数/嵌套函数 - 内部变量: 生存周期控制 • 控制语句 - if /if-else/if-elseif-…-else - switch-case - for/while

  4. 第八讲回顾 (续) if 语句一般形式 if logical_expression block of statements elseif logical_expression block of statements … … else block of statements end

  5. 第八讲回顾 (续) switch语句 一般形式 switch expression case value1, block of statements case value2, block of statements ... otherwise, block of statements end

  6. 第八讲回顾 (续) - 循环结构 根据条件重复执行程序指令或模块 for 语句 %计数循环 一般形式 for index = expression block of statements end

  7. 第八讲回顾 (续) while语句 一般形式 while expression block of statements end

  8. Matlab编程 (续) while语句实例: newton法计算 while abs(rold-r) > delta rold = r; r = 0.5*(rold + x/rold); end

  9. Matlab编程 (续) 循环中断控制: while语句实例 x = rand(1,10); k = 1; while k<=10 if x(k)>0.8 break end k = k + 1; end

  10. Matlab编程 (续) 函数返回控制: while语句实例 x = rand(1,10); k = 1; while k<=10 if x(k)>0.8 return end k = k + 1; end

  11. Matlab编程 (续) 综合练习: 利用级数展开近似计算 sin - 提示 sin的级数展开为 - m-函数实例 1: function s=powersin(x); 2: % POWERSIN. Power series for sin(x). 3: % POWERSIN(x) tries to compute… sin(x) from a power series.

  12. Matlab编程 (续) 4: s=0; 5: t=x; 6: n=1: 7: while abs(t) > eps 8: s=s+t; 9: t=-x.^2/((n+1)*(n+2)).*t; 10: n=n+2; 11: end

  13. Matlab编程 (续) • 综合练习七: 生命周期曲线 - 提示: 生理-23天; 情感-28天; 智力-33天 function biorythm(mybirthday) t0=datenum(mybirthday); t1=fix(now); t=(t1-28):1:(t1+28); t=t'; y=100*[sin(2*pi*(t-t0)/23) … sin(2*pi*(t-t0)/28) … sin(2*pi*(t-t0)/33)];

  14. Matlab编程 (续) plot(t,y(:,1),t,y(:,2),'--',t,y(:,3),'.-',[t1 t1],[-100 100],'k:'); datetick('x','dd/mm','keeplimits'); axis tight; title(mybirthday); legend('Physical','Emotional','Intellectual'); xlabel('time[dd/mm]'); ylabel('biorythm index [%]')

  15. Matlab编程 (续) - 关于函数变量与参数传递 输入(输出)变量查询函数: nargin(nargout) 功能: 返回函数的输入(输出)个数 应用: 根据变量个数, 选取模块执行 function c = testarg1(a, b) if (nargin == 1) c = a .^ 2; elseif (nargin == 2) c = a * b; end

  16. Matlab编程 (续) - 变量的传递 输入(输出)变量查询函数: varargin(varargout) 功能: 返回由函数的输入(输出)变量的元胞数组 元胞:Matlab的数据类型, 由{ }标识 { } 中可以是算术/逻辑/字符/结构数据类型, 例如 >> A(1,1) = {[1 4 3; 0 5 8; 7 2 9]}; >> A(1,2) = {'Anne Smith‘}; >> A(2,1) = {3+7i}; >> A(2,2) = {-pi:pi/10:pi};

  17. Matlab编程 (续) - 变量的传递(续): 替代列表 注意: 元胞数组的引用: { } function testvar(varargin) for k = 1:length(varargin) x(k) = varargin{k}(1); % Cell array indexing y(k) = varargin{k}(2); end xmin = min(0,min(x)); ymin = min(0,min(y)); axis([xmin fix(max(x))+3 ymin fix(max(y))+3]) plot(x,y)

  18. Matlab编程 (续) - 函数句柄 定义: 指向函数的Matlab值, 基本格式 fhandle=@functionname fhandle可以作为变量传递 功能: 用于函数的创建和访问(运算) >> sqr = @(x) x.^2 % 匿名函数 >> trigFun = {@sin, @cos, @tan} >> plot(trigFun{2}(-pi:0.01:pi))

  19. Matlab编程 (续) 函数句柄的引用格式 fhandle(arg1, arg2, ..., argn) 实例: fhandle指向函数的曲线绘制 function x = plotFHandle(fhandle, data) plot(data, fhandle(data)) 引用: 绘制sin函数的曲线 >> plotFHandle(@sin,-pi:0.01:pi)

  20. Matlab编程 (续) 匿名函数 创建匿名函数 fhandle = @(arglist) expr 创建实例 >> sumxy = @(x, y) (x + y) 应用操作 >> sumxy(5, 7)

  21. Matlab编程 (续) 匿名函数元胞组实例 >> A = {@(x)x.^2, @(y)y+10, (x,y)x.^2+y+10} A = [@(x)x.^2] [@(y)y+10] [@(x,y)x.^2+y+10] >> A{1}(4) + A{2}(7) ans = 33 >> A{3}(4, 7) ans = 33

  22. Matlab编程 (续) 参数化的匿名函数 >>a = 1.3; b = .2; c = 30; >> parabola = @(x) a*x.^2 + b*x + c; >> fplot(parabola, [-25 25]) % fplot(@(x) a*x.^2 + b*x + c, [-25 25]) >> a = -3.9; b = 52; c = 0; >> fplot(parabola, [-25 25]) 提示: fplot 绘制句柄指向函数的图形

  23. Matlab编程 (续) 多重匿名函数的使用 问题: 计算 提示: quad 是 matlab 缺省的积分计算函数 回忆: >> lookfor quad >> g = @(c) (quad(@(x) (x.^2 + c*x + 1),… 0, 1)); >> g(2)

  24. Matlab编程 (续) 用户参数交互输入 参数的作用: 使程序更加通用灵活 参数输入的主要模式 -input 函数输入参数 - keyboard函数 (键盘模式) - menu 函数 (菜单模式)

  25. Matlab编程 (续) 键盘输入模式 基本格式: v= input(‘ 显示的提示信息 ’); 功能实现: 向用户显示提示信息 将用户的输入赋给v 使用实例 freq=input(‘frequence is’) 结果 frequence is (等待键盘输入, 回车确认输入完成 )

  26. Matlab编程 (续) 键盘控制模式 基本格式: keyboard; 功能实现: 临时终止程序执行 将控制权交给命令窗口 利用回车结束键盘控制 使用实例 查询/修改函数工作空间的变量 建立新的函数空间的变量

  27. Matlab编程 (续) 菜单输入模式 基本格式: item_no=menu(‘title’,’item1’,…’itemn’); 功能实现: 显示菜单 用户选择按钮 返回菜单序号 使用实例 s=menu(‘signal’,’red’,’green’,yellow’) 可结合选择控制语句使用

  28. Matlab编程 (续) 程序设计技术 循环向量化计算模式 目的: 向量计算替代循环模式 实例: tic tic for t=0:0.01:100 t=0.0:0.01:100 i=i+1; y(i)=sin(t) y=sin(t) end toc toc 耗时7.58 耗时0.01

  29. Matlab编程 (续) 程序设计技术(续) 阵列预分配空间 目的: 避免循环过程空间分配的耗时 实例: y(k)=0.75y(k-1)-0.125y(k-2)+2u(k) clear all,tic T=0.001;t=[0:T:16] u=sin(2*pi*t); % y=zeros(size(u)) y(1)=2*u(1); y(2)=0.75*y(1)+2*u(2); for k=3:fix(16/T)+1 y(k)= 0.75*y(k-1)-... 0.125*y(k-2)+2*u(k) end toc 耗时0.562(%)/ 0.0160

  30. Matlab编程 (续) 程序设计技术(续) 内存使用/管理 目的: 提高内存使用效率 主要的命令 >> clear % 清楚变量 >> pack % 将变量寄宿在磁盘 >> quit % 退出Matlab环境 >> save % 将变量数据存入文件 >> load % 将文件数据加载进入工作空间

  31. Matlab编程 (续) Matlab程序调试 主要工具: m编辑器内嵌debugger功能 程序错误 语法或格式错误: 显式且容易定位 功能或计算错误: 算法错误 纠错技巧 显示中间变量的内容 利用Keyboard语句,查看函数工作空间的变量内容 设置断点或单步执行方式

  32. 结束语 第十讲预告: Matlab绘图-I (2008年04月30日)

More Related