MATLAB-7-程序流程控制

3.1顺序结构

1.文件建立

(1)建立脚本文件F1.m,在F1的编辑器中输入

A=[1 2 3;4 5 6];

B=[1 2;3 4;5 6];

C=A*B

在命令行窗口输入

>> F1

C =

    22    28

    49    64

(2)建立函数文件F2.m,在F2的编辑器中输入

function C=F2(A,B)

C=A*B;

在命令行窗口输入

>> A=[1 2 3;4 5 6];

>>B=[1 2;3 4;5 6];

>> C=F2(A,B)

C =

    22    28

    49    64

2.顺序结构

(1)数据输入

>> A=input('please input value:')

please input value:100

A =

   100

(2)数据输出

>> s='hello,world';

>> disp(s)

hello,world

>> a=[1 2 3;4 5 6];

>> disp(a)

     1     2     3

     4     5     6

(3)程序暂停

a.pause(延迟秒数)

b.ctrl-C 强行终止

(4)例子:求a,b两个坐标点的距离及其黄金分割点c

>> a=input('a=');

a=1+1i;

>> b=input('b=');

b=4.5+4.5i;

>> c=a+0.618*(b-a);

>> s=abs(a-b);

>> disp(s);

    4.9497

 

>> disp(c);

   3.1630 + 3.1630i

3.2 if选择结构

1.双分支例子

>> x=input('x=');

x=6

>> if rem(x,2)//默认rem(x,2)==1

y=sqrt(x);

else

y=power(x,1/3);

end//表示结束

>> y

y =

   1.8171

2.多分支例子

>> c=input('c=','s');

c=d

>> if c>='A'&&c<='Z'

disp(lower(c));

elseif c>='a'&&c<='z'

disp(upper(c));

else

disp(c);

end

D

3.4 for循环

(1)利用无穷级数展开式求pi的近似值

y=0;

g=-1;

n=input('n=');

n=5

>> for i=1:n

g=-g;

y=y+g*1/(2*i-1);

end

>> pi=4*y

pi =

3.3397

 

 

Ps:另一种向量求和的方法

>>  n=input('n=');

n=5

>> x=1:2:(2*n-1);

>> y=(-1).^(2:n+1)./x;

>> pi=sum(y)*4

pi =

 3.3397

3.5 while循环

1.多次输入,0截止

>> msum=0;

n=0;

x=input('enter a number(end in 0):');

enter a number(end in 0):10

>> while x~=0

msum=msum+x;

n=n+1;

 x=input('enter a number(end in 0):');

end

enter a number(end in 0):5

enter a number(end in 0):0

>> if n>0

msum

mean=msum/n

end

msum =

 

      15      

mean =

 

      15/2    

2.break和continue语句

求(100,200)第一个能被21整除的整数

>> for n=100:200

if rem(n,21)~=0

continue

end

n

break

end

n =

 

     105      

3.多重循环结构

用筛选法求自然数范围内的全部素数

>> m=input('m=');

m=90

>> p=1:m;

>> p(1)=0;//表示1不是素数

>> for i=2:sqrt(m)

for j=2*i:i:m//内循环划去i的倍数,但不包括i

p(j)=0;

end

end

>> n=find(p~=0);

>> n

n =

 Columns 1 through 6

 

       2              3              5              7             11             13      

 

  Columns 7 through 12

 

      17             19             23             29             31             37      

 

  Columns 13 through 18

 

      41             43             47             53             59             61      

 

  Columns 19 through 24

 

      67             71             73             79             83             89     

3,6 函数文件的定义与调用

1.求半径为r的圆的面积和周长

Fcircle.m文件

function [s,p] = fcircle( r )

s=pi*r*r;

p=2*pi*r;

命令行窗口

>> fcircle(5)

ans =

 

8875/113  

>> [s,p]=fcircle(10)

s =

 

   13823/44   

p =

 

    7100/113  

2.匿名函数

>> f=@(x,y) x^2+y^2

f =

    @(x,y)x^2+y^2

>> f(3,4)

ans =

 

      25      

>> h=@sin

h =

    @sin

>> h(pi/2)

ans =

 

       1      

3.7函数的递归调用

1.求n!

在fact.m函数文件

function f = fact(n)

if n<=1

    f=1;

else

    f=fact(n-1)*n;

 

end

 

在a,m脚本文件

n=input('n=');

s=fact(n);

disp(s)

在命令行窗口

>> a

n=3

       6  

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值