###注 此代码在matlab中执行%% chapter 6%%1 分段函数(函数文件形式)
clc,clear
x =input('请输入一个x的值:');%输入一个 x 的值
%进行条件判断 分段函数
if x <0
y =-1;
elseif x ==0
y =0;else
y =1;
end
x,y
%%2 求出[100,200]之间第一个能被21整除的整数
clc,clear
for n =100:200%对[100,200]之间的数进行for循环
%求出[100,200]之间第一个能被21整除的整数
if rem(n,21)~=0%函数 rem 为取余 若余数不等于0则 continue 等于0 就 end
continue
end
break
end
n
%%3
clear,clc,
total=0;
k=0;%通过while循环计算出第一个 10*k^2-4*k+2+total大于20000的值
while total<=20000
k=k+1
total=10*k^2-4*k+2+total;
end
disp('the number of the term is:')
disp(k)
disp('the sum is:')
disp(total)%%4 编程生成一个6阶矩阵,使其主对角线上元素皆为1,与主对角线相邻元素皆为2,其余元素皆为0
clc,clear
A=zeros(6,6);%生成一个 6*6 零矩阵
for i=1:6for j=1:6% 此 if 将对角线元素变为 1if i==j
A(i,j)=1;
end
% 此 if 与主对角线相邻元素变为2ifabs(i-j)==1
A(i,j)=2;
end
end
end
A
%%5 求出1-100的和
clear,clc,sum=0;
k=1%此while循环求出1-100的和
while k<=100sum=sum+k;
k=k+1;
end
k,sum%%6
x = zeros(1,10);%创建一个 1*10 的零向量
for n =1:10
x(n)= sin(n*pi/10);%对向量中每一个赋值 求出 1到10/pi 的值
end
x
%%7
clc,clear,sum=0%利用嵌套循环求出1-5的阶乘和
for i =1:5%利用for 构造 1-5 配合下面的for循环求出阶乘和
pdr =1for k =1:k %求阶乘
pdr = pdr*k
end
sum=sum+pdr
end
sum%%8
clear,clc
data=[39456;716-15];for n=data %此时将data的各列依次赋给n
x=n(1)-n(2)%上-下 对应元素 赋值x
end
%%9 分段函数的代码表达
clear,clc,
x =input('x:')if x>0
y = x*sin(x)else
y = x^2+exp(x)
end
%%10 同样 分段函数的代码表达
clear,clc,
x =input('x:')if x<=0
y =(x+sqrt(pi))/exp(2);else
y = log(x+sqrt(1+x^2))/2
end
x,y
%%11 switch 条件语句的运用
clear,clc,
angle=input('input angle in degree:');
switch angle
case 45
disp('Northwest')
case 135
disp('Southeast')
case 225
disp('Southwest')
case 315
disp('Northwesr')
otherwise
disp('Direction Unknown')
end
%%12
clear,clc,
t=[0:100];
x=exp(-t).*sin(t);
response =input('Type min, max, or sum.','s')
response=lower(response);
switch response
case 'min'
minimum =min(x)
case 'max'
maximum =max(x)
case 'sum'
total =sum(x)
otherwise
disp('you have extered a proper choice.')
end
%%13if 循环
clear,clc,
score =input('please input your score:');%%通过if循环来对分数进行评级
if score>100
disp('please input your score again.')
elseif 90<= score & score<=100
disp('A')
elseif 80<=score & score<90
disp('B')
elseif 70<=score & score<80
disp('C')
elseif 60<=score & score<70
disp('D')else
disp('E')
end
%%14 swith 循环
clear,clc,
score =input('please input your score:');%%通过switch循环来对分数进行评级
switch score
case score>100
disp('please input your score again.')
case 90<= score & score<=100
disp('A')
case 80<=score & score<90
disp('B')
case 70<=score & score<80
disp('C')
case 60<=score & score<70
disp('D')
otherwise
disp('E')
end
%%15 每年存 10000 ,利率 6% 积累$1000000要需要多长时间。
clear,clc,sum=0;
year =0;%通过while循环构造
whilesum<=1000000%%没有存到 10000000 不出来
sum=sum*1.06;%%利率6%sum=sum+10000;%新一年的存款
year = year +1;%下一年
end
year
%%16%%(1) It(1)=50;
clc;clear;
It(1)=50;%初始库存为50辆手推车
S =[50556070707580809055];%构造销售额list
p(1)=50;% 第1周所生产的手推车数量50
k =1;%%目前初始状态第一周
while(It(k)>0& k<10)%% 跳出循环条件: 十周后 或者 库存小于0
It(k+1)= p(k)+ It(k)- S(k);
k = k+1;
p(k)= S(k-1)
end
plot(1:k,It(1:k))%%画出存量图
%%(2) It(1)=30;
clc;clear;
It(1)=30;%初始库存为30辆手推车
S =[50556070707580809055];%构造销售额list
p(1)=50;% 第1周所生产的手推车数量50
k =1;%%目前初始状态第一周
while(It(k)>0& k<10)%% 跳出循环条件: 十周后 或者 库存小于0
It(k+1)= p(k)+ It(k)- S(k);
k = k +1;
p(k)= S(k-1)
end
plot(1:k,It(1:k))%%画出存量图