当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

聊城大学:《MATLAB语言及应用 MATLAB Language and Application》课程教学资源(课件讲稿,下)第2章 MATLAB应用基础

资源类别:文库,文档格式:PDF,文档页数:49,文件大小:1.4MB,团购合买
点击下载完整版文档(PDF)

2.3数学函数 函数使用说明: ■(1)三角函数以弧度为单位计算。 ■(2)abs函数可以求实数的绝对值、复数的模、 字符串的ASCII码值。 (多功能) ■(3)用于取整的函数有fix、f1oor、ceil、 round,要注意它们的区别。 常用的基本数学函数如课本表2.7所示 2015-2-4 1

2015-2-4 1 函数使用说明: (1) 三角函数以弧度为单位计算。 (2) abs函数可以求实数的绝对值、复数的模、 字符串的ASCII码值。 (多功能) (3) 用于取整的函数有fix、floor、ceil、 round,要注意它们的区别。 常用的基本数学函数如课本表2.7所示 2.3 数学函数

PS:数组的信息获取 ◆1.数组的尺寸 函数名 函数描述 size 矩阵或数组各方向的长度 length 矩阵或数组各方向中的最长长度 ndims 矩阵或数组的维数 numel 矩阵或数组的元素个数 ◆2.数组的检测函数 口以“s”开头,函数返回的结果为逻辑型,如果检测符 合条件则返回1,不符合条件就返回0。 isempty,isscalar,isvector,issparse,isnumeric,isreal, isfloat,isinteger,ischar,islogical,iscell,isstruct,isa. 2015-2-4 2

2015-2-4 2 PS :数组的信息获取  1. 数组的尺寸  2. 数组的检测函数  以“is”开头,函数返回的结果为逻辑型,如果检测符 合条件则返回1,不符合条件就返回0。  isempty, isscalar, isvector, issparse, isnumeric, isreal, isfloat, isinteger, ischar, islogical, iscell, isstruct, isa. ndims 矩阵或数组的维数 length 矩阵或数组各方向中的最长长度 numel 矩阵或数组的元素个数 size 矩阵或数组各方向的长度 函数名 函数描述

Logical Functions ◆Function Purpose ischar (a) Returns a 1 if a is a character array and a 0 otherwise. isempty (a) Returns a 1 if a is an empty array and a 0 otherwise. isinf (a) Returns a 1 if the value of a is infinite (Inf) and a 0 otherwise. isnan(a) Returns a 1 if the value of a is NaN (not a number)and a 0 otherwise. isnumeric (a) Returns a l if the a is a numeric array and a 0 otherwise. 2015-2-4 3

2015-2-4 3 Logical Functions  Returns a l if the a is a numeric array and a 0 otherwise. isnumeric (a) Returns a 1 if the value of a is NaN (not a number) and a 0 otherwise. isnan (a) Returns a 1 if the value of a is infinite (Inf) and a 0 otherwise. isinf (a) Returns a 1 if a is an empty array and a 0 otherwise. isempty (a) Returns a 1 if a is a character array and a 0 otherwise. ischar (a) Function Purpose

2.4M文件 ◆用MATLAB语言编写的程序,称为M文件。 ◆M文件可以根据调用方式的不同分为两类: 脚本文件(Script File)(也称命令文件) ■函数文件(Function File)。 2015-2-4 4

2015-2-4 4 2.4 M 文件 用MATLAB语言编写的程序,称为M文件。 M文件可以根据调用方式的不同分为两类: 脚本文件(Script File)(也称命令文件) 函数文件(Function File)

2.4.1 脚本文件(Script File) ◆1.建立新的M文件, ■启动MATLAB文本编辑器,即可编写。 ◆2.打开已有的M文件,有3种方法 (1)菜单操作。从MATLAB主窗口的File菜单中选择Open命令, 则屏幕出现Open对话框,在Open对话框中选中所需打开的M文 件。在文档窗口可以对打开的文件进行编辑修改,编辑完成后, 将M文件存盘。 (2)命令操作。在MATLAB命令窗口输入命令:edit文件名, 则打开指定的M文件。 ■ (3)命令按钮操作。单击MATLAB.主窗口工具栏上的Open File 命令按钮,再从弹出的对话框中选择所需打开的M文件。 2015-2-4 5

2015-2-4 5 2.4.1  脚本文件(Script File) 1.建立新的M文件,  启动MATLAB文本编辑器,即可编写。 2.打开已有的M文件,有3种方法      (1) 菜单操作。从MATLAB主窗口的File菜单中选择Open命令, 则屏幕出现Open对话框,在Open对话框中选中所需打开的M文 件。在文档窗口可以对打开的M文件进行编辑修改,编辑完成后, 将M文件存盘。      (2) 命令操作。在MATLAB命令窗口输入命令:edit 文件名, 则打开指定的M文件。      (3) 命令按钮操作。单击MATLAB主窗口工具栏上的Open File 命令按钮,再从弹出的对话框中选择所需打开的M文件

例2.1建立一个命令文件将变量a,b的值互换, 然后运行该命令文件。 ◆ 程序1: ◆首先建立命令文件并以文件名exch.m存盘: ■clear; ■a=1:10; ■b=[11,12,13,14;15,16,17,18]; ■c=a;a=b;b=c; ■a b ◆然后在MATLAB的命令窗口中输入exch,将会 执行该命令文件。 2015-2 6

2015-2-4 6   程序1: 首先建立命令文件并以文件名exch.m存盘: clear; a=1:10; b=[11,12,13,14;15,16,17,18]; c=a;a=b;b=c; a b 然后在MATLAB的命令窗口中输入exch,将会 执行该命令文件。 例2.1 建立一个命令文件将变量a, b的值互换, 然后运行该命令文件

例如编写M文件计算theta的几个三角函数得 数组theta,再创建一系列的极坐标图形。 ◆打开M文件编辑器,输入命令语句如下: %An M-file script to produce %Comment lines注释行 ◆ %"flower petal"plots ◆theta=-pit0.01:pi; Computations,theta can be rho(1,:)=2*sin(5*theta).^2;assigned outside the script file rho(2,:)cos(10*theta).^3;%(in command window) rho(3,:)=sin(theta).^2; rho(4,:)5*cos(3.5*theta).^3; ◆fork=1:4 ◆ polar(theta,rho(k,:) Graphics output pause %Entering these commands in an M-file called petals.m. end %Typing petals at the MATLAB command line executes the statements %After the script displays a plot,press Return to move to the next plot. 2015-2-4 7

2015-2-4 7 例如编写M文件计算theta 的几个三角函数得 数组theta,再创建一系列的极坐标图形。  打开M文件编辑器,输入命令语句如下:  % An M-file script to produce       % Comment lines注释行  % "flower petal" plots   theta = -pi:0.01:pi;                % Computations,theta can be   rho(1,:) = 2*sin(5*theta).^2; % assigned outside the script file  rho(2,:) = cos(10*theta).^3; %(in command window)  rho(3,:) = sin(theta).^2;  rho(4,:) = 5*cos(3.5*theta).^3;  for k = 1:4   polar(theta,rho(k,:))           % Graphics output   pause                  %Entering these commands in an M-file called  petals.m.  end         %Typing petals at the MATLAB command line  executes the statements  %After the script displays a plot, press Return to move to the  next plot

◆运行后的结果如图2.1所示,单击键盘上的任意键 得到图2.2,再单击任意键,可以得到图2.3,再 单击任意键可以得到图2.4,共计四个图形。 ◆记住: 口命令文件的名称不能和变量名相同,而且必须以字母 开头,命令文件产生的变量都保存在工作空间中,命 令文件可以调用工作空间中任意变量。 2015-2-4 8

2015-2-4 8 运行后的结果如图2.1所示,单击键盘上的任意键 得到图2.2,再单击任意键,可以得到图2.3,再 单击任意键可以得到图2.4,共计四个图形。 记住:  命令文件的名称不能和变量名相同,而且必须以字母 开头,命令文件产生的变量都保存在工作空间中,命 令文件可以调用工作空间中任意变量

90 2 120 60 1.5 150 30 180 210 330 240 300 270 图2.1曲线‘2*sin(5*theta).2'的极坐标图形 2015-2-4 9

2015-2-4 9 图2.1 曲线‘2*sin(5*theta).^2’的极坐标图形

90 1 120 60 150 30 180 0 210 330 240 300 270 图2.2曲线‘cos(10*theta).3'的极坐标图形 2015-2-4 10

2015-2-4 10 图2.2 曲线‘cos(10*theta).^3’的极坐标图形 0.2 0.4 0.6 0.8 1 30 210 60 240 90 270 120 300 150 330 180 0

点击下载完整版文档(PDF)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共49页,可试读17页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有