MATLAB学习笔记之matlab程序流程控制

MATLAB程序流程控制

顺序结构程序

程序和程序设计

image-20221028154820171

image-20221028154825600

程序的三种基本结构

image-20221028154844598

脚本文件和函数文件

image-20221028154904294

文件的建立

image-20221028154929531

image-20221028154950444

顺序结构

image-20221028155004927

数据的输入

image-20221028155018117

数据的输出

image-20221028155033326

程序的暂停

image-20221028155048084

image-20221028155055798

a=input('a=');
b=input('b=');
c=a+0.618*(b-a);
s=abs(a-b);
disp(s)
disp(c)

image-20221028155134527

image-20221028155218225

用if语句实现选择结构

什么是选择结构

image-20221028155339637

单分支if语句

image-20221028155403546

image-20221028155409632

image-20221028155435909

image-20221028155441319

双分支if语句

image-20221028155457568

image-20221028155517067

x=input('请输入x的值:');
if rem(x,2)==1
    y=sqrt(x);
else
    y=x^(1/3);
end
y

image-20221028155554708

多分支if语句

image-20221028155621386

image-20221028155629755

c=input('请输入一个字符:','s');
if c>='A' && c<='Z'
    disp(lower(c))
elseif c>='a' && c<='z'
    disp(upper(c))
elseif c>='0' && c<='9'
    disp(str2double(c)^2)
else
    disp(c)
end

image-20221028155705461

用switch实现选择结构

语句格式

image-20221028155811913

image-20221028155821215

image-20221028155847791

image-20221028155857315

c=input('请输入一个单词:','s');
switch c(1) 
    case {'A','E','I','O','U','a','e','i','o','u'} 
        disp([c,'以元音字母开头']);
    otherwise 
        disp([c,'以辅音字母开头']);
end

image-20221028155929820

image-20221028155956145

g=input('请输入PM2.5值:');
switch fix(g) 
    case num2cell(0:34) 
        disp('空气质量优');
    case num2cell(35:74) 
        disp('空气质量良好');
    case num2cell(75:114) 
        disp('空气质量轻度污染');
    case num2cell(115:149) 
        disp('空气质量中度污染'); 
    case num2cell(150:249) 
        disp('空气质量重度污染');  
    otherwise 
        disp('空气质量严重污染');
end

image-20221028160137361

用for语句实现循环结构

什么是循环结构?

image-20221028160240017

for语句

image-20221028160309957

image-20221028160331647

image-20221028160345351

image-20221028160408053

y=0;
g=-1;
n=input('n=?'); 
for i=1:n
    g=-g;
    y=y+g/(2*i-1); 
end
pai=4*y

image-20221028160517573

image-20221028160531439

n=input('n=?');
x=1:2:(2*n-1);
y=(-1).^(2:n+1)./x;
pai=sum(y)*4

image-20221028160635582

image-20221028160655852

a=0;
b=1;
n=input('n=?');
h=(b-a)/n;
x=a:h:b;
f=sqrt(1-x.*x);
s=[];
for k=1:n
    s1=(f(k)+f(k+1))*h/2;
    s=[s,s1];
end
pai=4*sum(s)

image-20221028160804136

image-20221028160817796

s=0;
n=input('n=?');
for i=1:n
    x=rand(1);
    y=rand(1);
    if x*x+y*y<=1
        s=s+1;
    end
end
pai=s/n*4

image-20221028160856526

image-20221028160909115

image-20221028160940999

用while语句实现循环

while语句

image-20221028161109700

image-20221028161115795

image-20221028161131798

msum=0;
n=0;
x=input('Enter a number (end in 0):');
while x~=0
    msum=msum+x;
    n=n+1;
    x=input('Enter a number (end in 0):');
end
if n>0
    msum
    mean=msum/n
end

image-20221028161229010

image-20221028161307858

break语句和continue语句

image-20221028161330906

image-20221028161341660

for n=100:200
    if rem(n,21)~=0
        continue
    end
    n
    break
end

image-20221028161353987

循环的嵌套

image-20221028161436403

image-20221028161454002

m=input('m='); 
p=1:m;
p(1)=0;
for i=2:sqrt(m)
    for j=2*i:i:m
        p(j)=0;
    end
end
n=find(p~=0);
p(n)

image-20221028161529771

函数文件的定义与调用

函数文件的基本结构

image-20221028161647800

image-20221028161707304

image-20221028162117074

function [s,p]=fcircle(r)
s=pi*r*r;
p=2*pi*r;

函数调用

image-20221028175613599

image-20221028162214554

image-20221028175512176

匿名函数

image-20221028175630344

image-20221028175648814

image-20221028175703803

image-20221028175730983

image-20221028175739208

# f2.m
function f=f2(n)
f=0;
for k=1:n
    f=f+k*(k+1);
end
# mf.m
f1=@(n) n+10*log(n*n+5);
y1=f1(40)/(f1(30)+f1(20))
y2=f2(40)/(f2(30)+f2(20))

image-20221028175858400

image-20221028175918477

image-20221028175937723

函数的递归调用

函数的嵌套调用

image-20221028180116762

函数的递归调用

image-20221028180137378

image-20221028180149583

image-20221028180159859

# fact.m
function f=fact (n)
if n<=1
    f=1;
else
    f=fact (n-1)*n;    %递归调用求(n-1)!
end
# a.m
n=input('Please input n=');
s=fact (n);
disp(s)

image-20221028180408055

image-20221028180414301

image-20221028180456696

# ffib.m
function f=ffib(n)
if n>2
    f=ffib(n-1)+ffib(n-2);
else
    f=1;
end
# test.m
F=[];
for k=1:20
    F=[F,ffib(k)*ffib(k)];
end
sum(F)
ffib(20)*ffib(21)

image-20221028180700110

函数参数与变量的控制域

函数参数的可调性

image-20221028180800730

# temp.m
function fout=temp(a,b,c)
if nargin==1
    fout=a;
elseif nargin==2
    fout=a+b;
elseif nargin==3
    fout=(a*b*c)/2;
end

image-20221028181005383

全局变量与局部变量

image-20221028181028882

image-20221028181034966

# wad.m
function f=wad(x,y)
global ALPHA BETA
f=ALPHA*x+BETA*y;

image-20221028181245111

总结

顺序结构程序

image-20221028181453585

用if语句实现选择结构

image-20221028181502052

用switch实现选择结构

image-20221028181508412

用for语句实现循环结构

image-20221028181516463

用while语句实现循环

image-20221028181522221

函数文件的定义与调用

image-20221028181527494

函数的递归调用

image-20221028181532855

函数参数与变量的控制域

image-20221028181539013

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值