当前位置: 首页 > news >正文

MATLAB 2--结构化程式与自定义函数

目录

  • 一、结构化程式
    • 1、逻辑运算符
    • 2、流程控制语句以及示例
      • ①if语句
      • ②switch语句
      • ③while语句
      • ④for语句
      • ⑤break语句
    • 3、为变量预分配空间(pre-allocating space to variables)
      • ①NOT pre-allocating
      • ②Pre-allocating
  • 二、函数(Funtions)
    • 1、查看内置函数
    • 2、撰写函数
      • ①自由落体
      • ②bubblesort算法
      • ③判断闰年
      • ④华氏温度到摄氏温度的转换

一、结构化程式

1、逻辑运算符

运算符作用
<小于
<=小于或等于
大于
>=大于或等于
==等于
~=不等于
&&

2、流程控制语句以及示例

流程控制语句作用
if若if语句为真,则执行子句
switch根据switch语句内容判断执行哪个子句
while重复执行子句直到while中的条件为假
for执行子句固定次数

①if语句

if condition1
     statement1;
elseif condition2
     statement2;
else
     statement3;
end

if语句示例1

if rem(a,2)==0
    disp('a is even');
else
    disp('a is odd');
end

运行结果: a is odd

②switch语句

switch expression
case value1
     statement1;
case value2
     statement2;
……
otherwise
     statement;
end

switch语句示例:2

switch input_num
case -1
	disp('negative 1');
case 0
	disp('zero');
case 1
	disp('positive 1');
otherwise
	disp('other value');
end

运行结果:other value

③while语句

while expression
     statement;
end
n = 1;
while prod(1:n) < 1e100    //prod函数用于求数组元素的乘积
	n = n + 1;
end

运行结果: n=70

④for语句

for variable=start:increment:end
     commands;
end
for n=1:10
	a(n)=2^n;
end
disp(a)
      2           4           8          16          32          64         128         256         512        1024

★上述所有循环和条件语句都要在末尾以end闭合

⑤break语句

x = 2; k = 0; error = inf;
error_threshold = 1e-32;
while error > error_threshold
    if k > 100
    	break
    end
    x = x - sin(x)/cos(x);
    error = abs(x - pi);
    k = k + 1;
end

3、为变量预分配空间(pre-allocating space to variables)

①NOT pre-allocating

tic
for ii = 1:2000
    for jj = 1:2000
        A(ii,jj) = ii + jj;
    end
end
toc

程序结果:时间已过 3.754662 秒。

②Pre-allocating

tic
A = zeros(2000, 2000);		// 预先为变量分配内存空间
for ii = 1:size(A,1)
    for jj = 1:size(A,2)
        A(ii,jj) = ii + jj;
    end
end
toc

程序结果:时间已过 0.038823 秒。

程序一比程序二所用的时间更长.这是因为: 对于程序一,没有预先为变量A分配内存,因此每当A的形状发生改变时,都需要重新为A分配内存地址,这花费了更多的时间。

二、函数(Funtions)

1、查看内置函数

edit(which('mean.m'))

在matlab中看到mean的源代码如下:
在这里插入图片描述

2、撰写函数

①自由落体

在这里插入图片描述

function x = freebody(x0,v0,t)
% calculation of 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;

使用方式:

freebody(0, 0, 2)			
freebody(0, 0, [0 1 2 3])	
freebody(0, 0, [0 1; 2 3])	

得到 19.6000
得到 [0 4.9000 19.6000 44.1000]
得到 [0 4.9000; 19.6000 44.1000]

②bubblesort算法

图1.bubblesort算法代码运行
在这里插入图片描述运行结果
在这里插入图片描述

③判断闰年

判断闰年程序
在这里插入图片描述

运行程序
在这里插入图片描述运行程序结果
在这里插入图片描述

④华氏温度到摄氏温度的转换

function F2C()
while 1
    F_degree = input('tempreature in Fahrenheit: ', 's');
    F_degree = str2num(F_degree);
    if isempty(F_degree)
        return
    end
    C_degree = (F_degree-32)*5/9;
    disp(['tempreature in Celsius: ' num2str(C_degree)])
end

这里是引用


  1. 命令行输入a=5 ↩︎

  2. 命令行输入:input_num=5 ↩︎

相关文章:

  • Go学习笔记 -- 并发原理
  • 【MindSpore产品】【数据处理功能】加入数据增强之后,报出卷积输入类型不同的问题
  • 基于Nexus搭建docker镜像源仓库
  • Estimating High-Dimensional Directed Acyclic Graphs with the PC-Algorithm
  • Linux文件查找find
  • Vue--》Vue中实现数据代理
  • 深度学习入门(十) 模型选择、过拟合和欠拟合
  • RK3399驱动开发 | 12 - AP6255 SDIO WiFi 调试(基于linux4.4.194内核)
  • 牛客网-《刷C语言百题》第二期
  • 测试开发需要掌握哪些技能?
  • 巴什博弈——范围拿物品问题
  • 【Mybatisplus】初识Mybatisplus+SpringBoot整合
  • 【编程碎笔】-Java中关于next(),nextInt(),nextLine()的深度解剖
  • 2023年荆州市高新技术企业申报条件以及奖励补贴政策(附申报时间)汇总!
  • macOS Ventura 正式版你确定不更新,好用到爆的功能你不想尝试一下?
  • 云存储架构框架设计 | 最佳实践
  • 阿里巴巴面试题- - -多线程并发篇(三十)
  • 计算机网络【UDP与TCP协议(三次握手、四次挥手)】
  • Linux进程控制
  • Unity 分享 功能 用Unity Native Share Plugin 实现链接、图片、视频等文件的分享+ 安卓 Ios 都可以,代码图文详解