matlab_2

matlab1

语法:

if,elseif,else
matlab内部介绍:if,elseif,else

if expression
    statements
elseif expression
    statements
else
    statements
end
if any(A > limit)
    disp('There is at least one value above the limit.')
else
    disp('All values are below the limit.')
end

for
for

for index = values
   statements
end
for v = [1 5 8 17]
   disp(v)
end

switch,case,otherwise
switch,case,otherwise

switch switch_expression
   case case_expression
      statements
   case case_expression
      statements
    ...
   otherwise
      statements
end
n = input('Enter a number: ');

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

whilewhile

while expression
    statements
end
n = 10;
f = n;
while n > 1
    n = n-1;
    f = f*n;
end
disp(['n! = ' num2str(f)])

break
break

limit = 0.8;
s = 0;

while 1
    tmp = rand;
    if tmp > limit
        break
    end
    s = s + tmp;
end

coutinue
continue

for n = 1:50
    if mod(n,7)
        continue
    end
    disp(['Divisible by 7: ' num2str(n)])
end

运算符

在这里插入图片描述

特殊符号

•clear all to remove previous variables 
•close all to close all figures
Use ellipsis ... to make scripts more readable
Press Ctrl+Cto terminate the script before conclusion

定义

定义一个的作用是什么?

在这里插入图片描述

function x = freebody(x0,v0,t) 
x = x0 + v0.*t + 1/2*9.8*t.*t;

重点这个function一定要建立在一个新的脚本中,并且保存,保存的文件名为function name!!!
我们后面调用freebody时就是这个作用,类似于其他编程中的函数作用,这个地方必须用.*,原因时矩阵相乘的不同

看 内嵌函数的内容 举例 :查看内建函数:平均数 mean()函数

输入命令:edit(which(‘mean.m’)) 查看一个 function 的内容

这里有一点:x=x0+v0.t+1/29.8*t.*t;这句话里面用的是点乘, 元素与元素之间相乘。
这点很重要哦。

发布了50 篇原创文章 · 获赞 75 · 访问量 6702

猜你喜欢

转载自blog.csdn.net/weixin_45822638/article/details/103341257
今日推荐