码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    if语句示例1:

    if rem(a,2)==0
        disp('a is even');
    else
        disp('a is odd');
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5

    运行结果: a is odd

    ②switch语句

    switch expression
    case value1
         statement1;
    case value2
         statement2;
    ……
    otherwise
         statement;
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    switch语句示例:2

    switch input_num
    case -1
    	disp('negative 1');
    case 0
    	disp('zero');
    case 1
    	disp('positive 1');
    otherwise
    	disp('other value');
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    运行结果:other value

    ③while语句

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

    运行结果: n=70

    ④for语句

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

    ★上述所有循环和条件语句都要在末尾以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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    程序结果:时间已过 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

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

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

    二、函数(Funtions)

    1、查看内置函数

    edit(which('mean.m'))
    
    • 1

    在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;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    使用方式:

    freebody(0, 0, 2)			
    freebody(0, 0, [0 1 2 3])	
    freebody(0, 0, [0 1; 2 3])	
    
    • 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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    这里是引用


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

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

  • 相关阅读:
    汇编语言(2)基础知识
    7.3 调用函数
    #816 Div2E. Long Way Home 斜率优化dp
    windows自动切换深色模式(夜晚模式)
    Flink实时计算中台Kubernates功能改造点
    理解在Unity中使用多个相机
    通关算法题之 ⌈二叉树⌋ 上
    深度学习服务器怎么选?哪个更划算?
    Java创建自定义注解所需要使用的几个元注解
    【探索Linux】—— 强大的命令行工具 P.15(进程间通信 —— system V共享内存)
  • 原文地址:https://blog.csdn.net/m0_58585940/article/details/127588716
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号