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')
![在这里插入图片描述](https://img-blog.csdnimg.cn/db497eb015d14c129ce5e09e11664f38.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Im-6YaS,size_16,color_FFFFFF,t_70,g_se,x_16)
函数式文件
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