台湾国立大学郭彦甫Matlab教程笔记(6)user define function

这一部分是function的讲解

scripts vs. functions
1.scripts and functions are both .m files that contains matlab commands
2.functions are written when we need to perform routines
differences:
argument 是参数的意思
脚本没有输入参数,输出参数,对数据的操作是整个工作区。函数有输入参数,输出参数,对数据的操作是对局部的工作区。


function 写起来,另外一个 script 或者 function 呼叫它

content of matlab built-in functions
看 内嵌函数的内容 举例 :查看内建函数:平均数 mean()函数
输入命令:edit(which(‘mean.m’)) 查看一个 function 的内容

edit(which('mean.m'))


想要自己建立function ,只要模仿 matlab中的内建函数长什么样子就行。

内建函数


some observations 观察
1.keyword:function 关键字是function
2.function name matches the file name 函数名字和文件名字匹配
3.directory:matlab needs to find the function 路径
4.input and output varibles are optional 输入参数和输出参数可以没有
5.local varibles: dim and flag cannot be accessed 局部变量

user define functions用户自定义函数
范例程式1
1.write a function that calculate the displacement 位移 of free falling 自由落体for given initial displacement 初位移x0,initial velocity 初速度v0,and duration of falling t:下落时间

codes:代码:

function x=freebody(x0,v0,t)
% calculation fo free falling
% x0: initial  displacement in m
%v0: initial velocity in m/sec
%t: the elapsed time in sec
% x: the depth of falling in m
x=x0+v0.*t+1/2*9.8*t.*t;

新建 script 键入 上面的codes ,然后保存 save as 一般是名称跟function name 一致,为 freebody,如果不一致需要手动修改成一致。
如下图

然后就可以调用 freebody() 这个 function

上面的含义是:起始位置是0,初速度也是0,下落时间为10,得到下落的位移(位置)是490m
这个 调用自定义函数和调用内置函数一样
比如 调用cos()函数,计算cos(10)等于多少,呼叫,得到结果如下图

这里有一点:x=x0+v0.t+1/29.8*t.*t;这句话里面用的是点乘, 元素与元素之间相乘。【这里很重要,很多初学者都踩过的坑】
这点很重要哦。用例子来讲解 。还是用cos()函数
cos([1 2 3 4 5])计算的结果是五个值,就是分别计算。
现在在 freebody这个函数中 能不能一下算出2组位移呢?可以。因为用的是点乘,是对应相乘。
比如:
freebody([0,1],[0,4],[10,20])
得到的就是两组值:解释上式:初始位置分别是0和1,初始速度分别是0和4,下落时间分别是10和20s这样两组数据
得到两组位移分别是下图:

如果把点乘去掉,就算不出来。

范例程式2
functions with multiple inputs and outputs多输入、多输出函数
1.the accaleration of a particle and the force acting on it are as follows:粒子的加速度和受力如下式

分析: input 有五个 v2 v1 t2 t1 和m ,output 有两个 a 和 F
code:
function [a F]= acc(v2,v1,t2,t1,m)
a=(v2-v1)./(t2-t1);
F=m.*a;

function  [a F]= acc(v2,v1,t2,t1,m)
a=(v2-v1)./(t2-t1);
F=m.*a;

【注意】调用的时候需要有两个 output ,外面需要用中括号括起来。
尝试代码:acc(20,10,5,4,1)
结果

正确的做法:[a,F]=acc(20,10,5,4,1)
显示:

练习题:

解析:这个问题是写一个函数,这个函数没有input,而是从command window中输入 一个 华氏温度,然后需要 计算成摄氏温度。then显示 这个摄氏温度。还有要求就是:这个函数一直执行,直到输入的不是数字。
提示:C=(F-32)*(4/9).还有五个函数 input,isempty ,break,disp,num2str
老师的要求,自己搜索这些提示函数的用法
1.input 函数的使用查询

2.disp函数使用的查询

现在想要显示一句话,并且显示数字,换言之,需要在同一行上显示多个变量


第一步实现 输入和显示
代码1:这里不是最后的代码,最后的代码请往下看

function F2C
F=input('Fahrenheit temperature:');
C=(F-32)*(4/9);
x=['The Fahrenheit temperature is ',num2str(F)];
y=['The converted Celsius degree is ',num2str(C)];
disp(x);
disp(y);

结果显示:

第二步需要实现 一直在提示输入,直到输入的不是数字为止。根据老师提示isempty函数,加上他的演示,这里退出循环的条件是为空,按回车。
最终的代码:

function F2C
while 1F=input('Fahrenheit temperature:');if isempty(F)==1break;endC=(F-32)*(4/9);x=['The Fahrenheit temperature is ',num2str(F)];y=['The converted Celsius degree is ',num2str(C)];disp(x);disp(y);end

运行结果:

function default varibles默认变量
1.inputname :varible name of function input
2.mfilename: file name of currently running function
3.nargin :number of function input argument 函数输入变量数
4.nargout:number of function output arguments 函数输出变量数
5.varargin:varible length input argument list 输入变量的长度
6.varargout:varible length output argument list输出变量的长度
【注意】以上6个默认varible需要记住,作业的时候需要。
以下面这个function为例,说明不同的输入参数的个数。nargin表示输入参数的个数,你看下面pillar这个函数,有三个输入参数,如果你输入两个参数的话,函数内部自己假定第三个参数为1,参见下面if nargin ==2部分

function handles函数句柄
1.a way to create anonynous function同名
2.句柄是指针pointer

介绍函数句柄的基本使用

内容有:

【总结一下】
本次笔记的知识结构是:
1.内建函数function 的一些基本知识。包括function关键字,函数名,输入变量,输出变量。
2.两个用户自定义function的例程实现。一个:多输入单输出;另一个:多输入多输出。
3.函数的一些default varibles
4.函数句柄

台湾国立大学郭彦甫Matlab教程笔记(6)user define function相关推荐

  1. 台湾国立大学郭彦甫Matlab教程笔记(22) Cramer's method(Inverse matrix逆矩阵法)

    台湾国立大学郭彦甫Matlab教程笔记(22) Cramer's method(Inverse matrix) matrix left division左除:\ or mldivide() solvi ...

  2. 台湾国立大学郭彦甫Matlab教程笔记(21)linear equations(高斯消去法和追赶法)

    台湾国立大学郭彦甫Matlab教程笔记(21) today: linear equation 线性方程 linear system 线性系统 我们先看第一部分 linear equation 假定一个 ...

  3. 台湾国立大学郭彦甫Matlab教程笔记(20) root finding(numeric)

    台湾国立大学郭彦甫Matlab教程笔记(20) root finding(numeric) symbolic vs. numeric符号法和数值法的区别对比 symbolic 1)advantages ...

  4. 台湾国立大学郭彦甫Matlab教程笔记(17)numerical integration

    台湾国立大学郭彦甫Matlab教程笔记(17)numerical integration 数值积分 calculating the numerical value of a definite inte ...

  5. 台湾国立大学郭彦甫Matlab教程笔记(16) 数值微分 numerical differentiation

    台湾国立大学郭彦甫Matlab教程笔记(16) 数值微分 numeric differentiation 复习:diff()函数用来计算vector前后 entry的差异 数值微分继续 various ...

  6. 台湾国立大学郭彦甫Matlab教程笔记(15)polynomial integration 多项式积分

    台湾国立大学郭彦甫Matlab教程笔记(15) Polynomial integration多项式积分 一个多项式和它的积分如下 MATlAB中如何计算积分? polynomial integrati ...

  7. 台湾国立大学郭彦甫Matlab教程笔记(14)polynomial differentiation多项式微分

    台湾国立大学郭彦甫Matlab教程笔记(14) today: polynomial differentiation and integration多项式微分与积分 numerical differen ...

  8. 台湾国立大学郭彦甫Matlab教程笔记(12) advanced 2D plot 下

    台湾国立大学郭彦甫Matlab教程笔记(12) advanced 2D plot 下 上文记录的是关于统计的图标的绘制 下面我们来到另一个模块:颜色 fill()填充函数 功能:某一个封闭曲线,图上特 ...

  9. 台湾国立大学郭彦甫Matlab教程笔记(11) advanced 2D plots 上

    台湾国立大学郭彦甫Matlab教程笔记(11) today: 1.advanced 2D plots 2.color space色彩使用 3.3D plots 图形概览,做研究的时候需要选择图形 sp ...

  10. 台湾国立大学郭彦甫Matlab教程笔记(9) basic plotting

    台湾国立大学郭彦甫Matlab教程笔记(9) today: 1.basic plotting 2.graphical objects properties basics matlab has a po ...

最新文章

  1. spring cloud config动态刷新_SpringCloud-Config
  2. Qt学习笔记之MySQL数据库
  3. blog被封了文章全被删除了,郁闷
  4. linux 文件 加密传输,服务端和客户端之文件加密传输
  5. java中int转成String位数不足前面补零 java格式化2位数不足补零
  6. ICLR2020 | 如何判断两个神经网络学到的知识是否一致
  7. 【html笔记】html介绍和语法入门
  8. 系统封装 如何为原生PE集成软件
  9. 在Web上运行Linux
  10. 汇川技术js600变频器_汇川变频器MD200系列产品型号及功能介绍
  11. qt学习之路(三)之使用QT语言家
  12. linux下如何查看tlq服务,谁能跟我讲解一下bashrc?
  13. antd DatePicker 日期国际化错误 中英文混合存在(月份,星期英文,其他中文)
  14. vs code masm dll
  15. 【数字图像处理】MATLAB实现图像旋转
  16. mysql创建日历表,可以按日或按月增加数据
  17. iOS 增量代码覆盖率检测实践
  18. Office提示“由于本机的限制,该操作已被取消。请与系统管理员联系
  19. Java学习日记(31-40天,图)
  20. 清空hive表 姿势大全

热门文章

  1. Docker-容器化应用
  2. (2) Hive安装
  3. 修改服务器上tomcat的默认端口号
  4. android 图片占用内存的计算
  5. 某灵JAVA互联网架构师专题/分布式/高并发/微服务
  6. Oracle安装步骤(记录)
  7. 【React Native】深入理解Native与RN通信原理
  8. 棒棒的二维数据可视化分类模型
  9. 500以内的蓝牙降噪耳机哪款好?高性价比降噪蓝牙耳机分享
  10. vue 2 组件之间传值