matlab——结构化程式与自定义函数

matlab ——结构化程式与自定义函数
script writing
打开new script:
某一句变注解:前面加%
一大段变注解:选中后右键"comment" ;
取消注解 :右键"uncomment"
划区块: %%
执行区块: run section
执行全部 :run
自动缩排 :选中后右键"smart indent"

Structured programming
relational (logical) operators: < <= > >= == ~=(not equal to) &&(and) ||(or)
structure programming:
if elseif else end
switch
while
for
pre-allocating space to variables 预宣告可以提升运行速度
tips for script writing
(1)clear all : to remove previous variables
(2)close all : to close all figures
(3)use semicolon ; at the end of commands to inhibit unwanted output 语句末尾使用分号不输出
(4)use ellipsis… to make scripts more readable:
A=[1 2 3 4 5 6 ;…
6 5 4 3 2 1 ]; 语句过长用省略号换行
(5)press Ctrl+C to terminate the script before conclusion 及时终止程序运行
scripts vs function
script 需要打开、编程,function直接呼叫调用即可
function示例:

function x = freebody(x0,v0,t)
%calculation of free falling
%x0 : initial displacement in m
%v0 : initial velocity in m/sec
%t : the escaped time in see
%x : the depth of falling in m
x= x0 + v0.*t+1/2*9.8*t.*t;
%注意是点乘而非叉乘,适用于矩阵

User-defined function
function default variables:
inputname : variable name of function input 输入变量名
mfilename : file name of currently running function 输出变量名
nargin : number of function input arguments 输入参数个数
nargout : number of function output arguments 输出参数个数
varargin : variable length input argument list 输入参数的变量长度
varargout : variable length output argument list 输出参数的变量长度

function handle :类似于指针

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值