matlab note1

编程结构

if elseif else

a = 10;
if mod(a,2)~=0   %也可以用rem,也是取余数
    disp('a is a odd')
else
    disp('a is a even')
end

switch case

switch expression
case condition1
command
case condition2
command
otherwise
command
end

input_num = -1;
switch input_num
    case -1
        disp('negative 1')
    case 1
        disp('objective')
    otherwise
        disp('not a number')
end

while

while expression

statement

end

n = 1
while prod(1:n) < 1e100  ##科学计数法,1乘以10100次方,2e10就是2*10^10 prod(1 2 3 …… n)是求阶乘
	n = n+1
end

%%---------------------
sum =0;
n = 1;
while n < 1000
    n = n +1;
    sum = sum +n;
end
disp(sum)

break和while一起出现,如果出现了break为true的条件,直接跳出跳出整个while,到end的地方

for

for variable = start:increment:end

commands

end

for i = 1:10 
    for j = 1:2:10
        a(i,j) = 2^j+2^i;
    end
end
disp(a)

pre-allocating

clear A
tic                     %开始计时
A = zeros(2000);         %预宣告 pre-allocating
for ii = 1:2000
    for jj = 1:2000
        A(ii,jj)=ii+jj;
    end
end
toc                     %结束计时

some tips

clear all close all clc ; 不在命令窗显示执行结果

… 太长的时候换行 ctrl + c 中断

函数

disp(freeBody(0,0,10));

function x = freeBody(x0,v0,t)
x = x0+v0.*t+1/2*9.8*t.*t;
end
----------------------------------------------
function [a,Force]=acc(v2,v1,t2,t1,m)
    a = (v2-v1)./(t2-t1);
    Force = m.*a
end
----------------------------------------------
function x = ftc(c)
    c = input("输入摄氏度:");
    while isempty(c)~=1
        disp("对应的华氏度为");
        temp = c*9/5+32;
        disp(temp);
        c = input("输入摄氏度:");
    end
end

用==.*==的细节:如果输入的参数都是一个同样维度的矩阵,也就是多组数据,这样可以进行对应位置的数值计算,输出多组结果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值