目录
%% top-down design
%% calculate the area of a disc
%% [1] get the input: radius
%% [1.1]
%% [1.2]
%% [2]Caculate the result: the area
%% [2.1]
%% [2.2]
%% [3] Display the output
%% [3.1]
%% [3.2]
文件的建立
脚本文件可以直接在命令窗口运行,函数文件应以函数调用格式使用:
3.1 顺序结构程序
(1) 数据的输入: A = input('请输入A的值: ');
(2) 数据的输出: disp(输出项);
(3) 程序的延迟: pause(延迟秒数); 不加秒数为暂停运行,按任意键可以继续。
CTRL+C:阻止程序的运行
3.2 用if语句实现选择结构
单分支if语句
- 当条件结果为标量时,非零表示条件成立,零表示条件不成立
- 当条件结果为矩阵时,如果矩阵为非空,且不包含零元素,则条件成立,否则不成立
例如: [1,2;0,4]表示条件时,条件不成立。
双分支if语句
3.3 用switch语句实现选择结构
- switch表达式应该是一个其值可以列举的表达式
- case结果表为switch表达式的取值,当取值有多个时,用单位数据表示
3.4 用for语句实现循环结构
说明:
- for语句针对每一个元素执行一次循环
for k = [1,2,3,5] k end k=4
- 退出循环之后,循环变量的值就是向量中最后的元素值
- 当向量为空时,循环体一次也不执行。
例:计算圆周率
利用无穷级数展开计算Π的近似值
n = input('n= '); x = 1:2:(2*n-1); y = (-1).^(2:n+1)./x; pai = sum(y)*4; disp(['pai= ',num2str(pai)])
利用蒙特卡洛法求Π的近似值
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; disp(['pai= ',num2str(pai)])
按照matlab的定义,for语句的循环变量也可以时一个列向量
s=0; a=0; for A = [1,2,3,4] s = s+1; end for S = [1;2;3;4] a = a+1; end disp(a); disp(s);
3.5 用while语句实现循环结构
- while语句经常用于循环次数不确定的语句
- break语句:结束循环
- continue语句 :跳出本次循环
例题:
clear; clc; 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)
3.6 函数文件的定义与调用
函数文件基本结构
function outputargument = functionname(inputargument)
当有多个形参时,形参之间用逗号分割,组成形参表。当输出形参多于一个时,应用方括号括起来,构成一个输出矩阵
- 函数文件名通常由函数名再加上扩展名.m组成,函数文件名与函数扩展名可以不同
- return语句表示结束函数的执行。
- end:表示结束
区别在于第二个可以输入矩阵,因为每一个元素对应.^
注释的元素
- % calcarea returns the area of a circle
- % Format: calcarea(rad)
- % The input argument rad be a vector of radii
- % The output argument area is a vector of area(s) of circle(s)
- % Modify records
- % name,2023-02-04 9:20pm
- % Modified the function to enable vectorized calculation
函数调用
匿名函数
>> f = @(x,y) x^2+y^2; >> f(3,4) ans = 25
h = @sin h(pi/2) ans = 1
3.7 函数的递归调用
函数的递归调用:
自己调用自己 例:
3.8 函数参数与变量的作用域
函数参数可调性
function four = test(a,b,c) if nargin == 1 fout = a; elseif nargin == 2 fout = a+b; elseif nargin == 3 fout = (a*b*c)/2; end
当用户输入的参数为1,2,3;则分别执行不同的命令,使命令执行更加简洁。
全局变量与局部变量
例:
全局变量全程可以修改,包括在命令行窗口中。
3.9输出函数与输入函数
输出函数
% top-down design
% calculate the area of a disc
% [1] get the input: radius
radius = 5;
% [2]Caculate the result: the area
area = pi * radius ^ 2;
% [3]Display the output
% disp(['the area of the disc is ' num2str(area)]);
%%输出the area of the disc is N (N表示78的char形态)
fprintf( 'the area of the disc is %5.3f \n ' ,area); % f - float,\n - newline
5表示数据整体的长度,不足由空格补齐,3表示保留的小数点位数,
若为+5.3表示向右对齐,-5.3表示向左对齐
%%输出the area of the disc is 78.539816
输入函数
>>r = input('the radius of the picture A :')
the radius of the picture A : 5
r = 5
>>leter = input('enter a char : ','s') %%转换为字符型第二项必须为's'
>>leter = input('enter a char : ') %%不输入s,则由input自行确认
enter a char :'apple'
leter = 'apple'
>>R = 16
>>rad = input('enter a word: ')
enter a word: R
rad = 16
3.10输出类型
关于%d,%f,%c,%s
>fprintf('The value is ".d,for sure! in',4^3)
The value is 64,for sure!
>> %format specifier
>> %place holder
>> fprintf( ' The value is %d,for sure! ',4^3)
The value is 64,for sure! >> fprintf( ' The value is %d,for sure! \n',4^3)
The value is 64, for sure!
>> fprintf( ' The value is %f,for sure! \n',4^3)
The value is 64.000000,for sure!
>> fprintf( ' The value is %c,for sure! \n ',4^3)
The value is @,for sure!
>> fprintf( ' The value is %s,for sure! \n' ,4^3)
The value is @,for sure!
> fprintf( ' The value is %s,for sure! \n ',[64 65])
The value is @A,for sure!
>fprintf( ' The value is %c,for sure! \n',[64 65])
The value is @,for sure!
The value is A, for sure!