Matlab如何定义函数?Matlab定义函数的方法
时间:2023-07-16 23:11:39
Matlab是一款著名的商业数学软件,它的功能十分强大,我们常常会使用它解决一些数学难题。但是刚开始使用它的时候经常会遇到一些难题,比如Matlab如何定义函数,如果你不知道的话,赶紧看看小编整理的Matlab定义函数的方法吧!
方法/步骤:
1、首先建立M文件或直接点击(File/New/Function)建立函数文件,其中函数文件的格式是:
function [输出变量]= 函数名称(输入变量)
% 注释
% 函数体
2、如下所示,是编写的一个求1到n之和的求和函数eg_sum,按照上述格式,编写代码如下并保存文件,注意文件命名时不能以数字开头:
function [s]= eg_sum( n )
% calculate the sum of 1..n
s = 0;
for i = 1:n
s = s + i;
3、回到控制台Command Window(主界面),注意要使用之前保存的函数文件名来调用函数,如下所示:
>> clear
>> a = eg_sum(10)
运行结果:a = 55
许多网友说函数名一定要与函数文件名一致,其实不是这样子的,只要知道调用函数时是通过函数文件名调用的这点即可!比如下图所示,即使文件名仍然是eg_sum,但我可以修改函数名为:my_sum调用时用文件名调用就好,如下所示:
>> b = eg_sum(10)
运行结果:b = 55
注意事项:
在调用函数时,请确保已将Matlab当前目录切换到函数文件所在处,不然会报错:Undefined function ‘eg_sum’ for input arguments of type ‘double’。
标签:
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
WPS2016多行合一文字怎么制作
2023-11-12 11:17:38
![](https://img.aspxhome.com/file/2023/9/a163569_0s.jpg)
excel工作表标签颜色怎么添加或删除?
2022-09-16 03:47:24
![](https://img.aspxhome.com/file/2023/5/41035_0s.jpg)
MuMu模拟器一直提示关闭hyper-v怎么解决?
2022-06-27 02:05:10
![](https://img.aspxhome.com/file/2023/7/a327774_0s.png)
Win10远程桌面连接不上怎么解决?
2023-11-20 13:49:29
![](https://img.aspxhome.com/file/2023/4/a310734_0s.png)
Win10系统怎么设置小键盘控制鼠标?Win10系统设置小键盘控制鼠标技巧
2023-07-26 10:12:53
![](https://img.aspxhome.com/file/2023/28/a240629_0s.jpg)
Win10安装更新后无法还原解决方法?
2023-11-24 00:12:36
![](https://img.aspxhome.com/file/2023/3/50813_0s.png)
excel2007斜线表头如何制作
2022-01-31 07:09:09
如何利用Word 2007制作倒写的“福”字
2022-12-01 21:05:07
![](https://img.aspxhome.com/file/2023/7/30037_0s.jpg)
mac系统的电脑怎么切换城市天气? mac切换其他城市的天气的技巧
2022-07-20 05:24:30
![](https://img.aspxhome.com/file/2023/9/a214429_0s.jpg)
Excel2007如何将列名显示方便计数使用?
2023-10-05 20:57:50
![](https://img.aspxhome.com/file/2023/7/42107_0s.jpg)
win10关机的快捷键是什么?win10快速关机的四种方法
2022-06-28 06:31:41
![](https://img.aspxhome.com/file/2023/28/a242746_0s.jpg)
win10显示器颜色怎么调_win10电脑屏幕颜色调节方法
2023-03-23 00:55:14
![](https://img.aspxhome.com/file/2023/5/a316698_0s.jpg)
怎么取消Excel绿色三角?
2023-06-17 10:07:55
![](https://img.aspxhome.com/file/2023/0/37910_0s.jpg)
wps怎么画房屋图标? wsp房屋的画法
2022-09-15 14:25:05
![](https://img.aspxhome.com/file/2023/3/a165793_0s.png)
游戏专用XP系统GHOST XP SP3极速优化版
2023-06-06 23:29:22
![](https://img.aspxhome.com/file/2023/0/53330_0s.jpg)
怎么在wps表格中制作拟合直线图文教程
2023-04-22 20:43:00
Wps中进行论文页眉的设置方法
2023-06-23 01:00:18
macOS Catalina 分拆iTunes、SideCar、Find My
2023-05-07 18:53:35
![](https://img.aspxhome.com/file/2023/3/a212653_0s.jpeg)
滚动滚动条有平滑滚动效果
2023-08-04 04:49:02
win10语音助手听不见我说话怎么办?
2022-06-16 04:07:12
![](https://img.aspxhome.com/file/2023/1/a279321_0s.jpg)