MATLAB笔记——程序设计

m文件

命令式文件

>> %logotu.m
>> load logo
>> surf(L, R), colormap(M)
>> n=size(L, 1)

n =

    43

>> axis off
>> view(-37.5, 30)
>> title('Lift is too short to spend writing DO loops')

在这里插入图片描述

函数式文件

demo_5_2_fibfun.m文件

function f = demo_5_2_fibfun( n )
% FIBFUN For calculating Fibonacci numbers.
% Incidengtally, the name Fibonacci comes
%from Filius Bonassi, or"son of Bonassus"
% 文件名与函数名要一一对应。
if(n>2)
    f = demo_5_2_fibfun(n-1) + demo_5_2_fibfun(n-2);
else
    f = 1;
end

调用结果:

>> demo_5_2_fibfun(17)

ans =

        1597

调用help获取函数说明

>> help demo_5_2_fibfun
  FIBFUN For calculating Fibonacci numbers.
  Incidengtally, the name Fibonacci comes
 from Filius Bonassi, or"son of Bonassus"
  文件名与函数名要一一对应。

查看

>> lookfor fib
demo_5_2_fibfun                - FIBFUN For calculating Fibonacci numbers.
fibodemo                       - Used by SINGLEMATH demo.

判断某一年是否为闰年。

function isleapyear(year)
sign=0;
if rem(year,4)==0
   sign=sign+1;
end
if rem(year,100)==0
       sign=sign-1;
end
if rem(year,400)==0
       sign=sign+1;
 end
 if sign==1
      fprintf('%4d year is a leap year.\n',year)
  else 
      fprintf('%4d year is not a leap year.\n',year)
end

调用

>> isleapyear(2021)
2021 year is not a leap yea

控制语句

for循环

demo_5_4_Vandermonde.m文件中的代码

function a = demo_5_4_Vandermonde( t )
% t=[-1 0 1 3 5]
n=max(size(t))
for j=1:n
    for i=1:n
        a(i,j)=t(i)^(n-j);
    end
end

调用

>> b=demo_5_4_Vandermonde(t)

n =

     5


b =

     1     1     1     1     1
    16     8     4     2     1
    81    27     9     3     1
   256    64    16     4     1
   625   125    25     5     1

while循环

demo_5_4_expm.m文件

function [ e ] = demo_5_4_expm( a )
% a=[2 3;3 4]
% expm(a)=1+a+a.^2/2! 
%                  + a.^3/3! + ...
e=zeros(size(a));
f=eye(size(a));
k=1;

while norm(e+f-e,1)>0
    e=e+f;
    f=a*f/k;
    k=k+1;
end

调用

>> a = [2 3;3 4]

a =

     2     3
     3     4

>> demo_5_4_expm(a)

ans =

  162.7871  224.6754
  224.6754  312.5707

if语句

代码

>> % Script file: triarea.m
% This program is to calculate the area of a triangular
A=input('请输入三角形的三条边(数组形式):');
    if A(1)+A(2)>A(3) & A(1)+A(3)>A(2) & A(2)+A(3)>A(1)
       p=(A(1)+A(2)+A(3))/2;
       s=sqrt(p*(p-A(1))*(p-A(2))*(p-A(3)));
       disp(s);
    else
       disp('不能构成一个三角形。')
    end

运行结果

请输入三角形的三条边(数组形式):[3 4 5]
    6

switch语句

代码

>>  num=input('请输入一个数:');
switch num
  case -1
    disp('I am a teacher.');
  case 0
    disp('I am a student.');
  case 1
    disp('You are a teacher.');
  otherwise
    disp('You are a student.');
end

运行

请输入一个数:2
You are a student.

鸡兔同笼问题:
鸡兔同笼,头共36,脚共100。求鸡、兔各多少?

>> i=1;
while 1
    if rem(100-i*2,4)==0&(i+(100-i*2)/4)==36
        break
    end
    i=i+1; 
end
a1=i
a2=(100-2*i)/4

a1 =

    22


a2 =

    14

函数变量及变量作用域

通过varargin参数,用户可以输入任意多个学生的数学、英语、语文的成绩,然后求各科的平均值。

function [ mathAvg,engAvg,chnAvg ] = test532( varargin )
len = length(varargin);
mathAvg=0;engAvg=0;chnAvg=0;
for i=1:len
    mathAvg = mathAvg+varargin{i}(1);
    engAvg = engAvg+varargin{i}(2);
    chnAvg = chnAvg+varargin{i}(3);
end
mathAvg = mathAvg / len
engAvg = engAvg / len
chnAvg = chnAvg / len
end

调用

>> test532([1 2 3],[2, 3, 4])

mathAvg =

    1.5000


engAvg =

    2.5000


chnAvg =

    3.5000


ans =

    1.5000

子函数与局部函数

demo_5_11.m

function c = demo_5_11( a, b )
c = test1( a, b )*test2( a, b )
end
 
function c = test1( a, b )
c=a+b
end
 
function c = test2( a, b )
c=a-b
end

调用

>> demo_5_11(1, 2)

c =

     3


c =

    -1


c =

    -3


ans =

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艾醒(AiXing-w)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值