用if编写的函数文件程序清单第三次试验.docx
- 文档编号:8361867
- 上传时间:2023-01-30
- 格式:DOCX
- 页数:11
- 大小:15.75KB
用if编写的函数文件程序清单第三次试验.docx
《用if编写的函数文件程序清单第三次试验.docx》由会员分享,可在线阅读,更多相关《用if编写的函数文件程序清单第三次试验.docx(11页珍藏版)》请在冰豆网上搜索。
用if编写的函数文件程序清单第三次试验
用if编写的函数文件程序清单:
functiony=encode(x)
%encode:
对输入的数值[-1,1]之间进行二进制编码
%调用格式:
y=encode(x)
%x:
模拟输入
%y:
二进制编码输出
%首先检查输入数值的范围是否位于【-1,1】之间
ifx<-1|x>1
error('x的范围必须在【-1,】之间')
end
%满足条件则进行二进制编码
ifx>-1&x<-0.5
y='00';
elseifx>-0.5&x<0
y='01';
elseifx>0&x<0.5
y='10';
elsey='11';
end
end
end
用switch编写的一个函数文件程序清单:
functiony=encode_switch(x)
%encode_switch:
对输入的数值[-1,1]之间进行二进制编码
%调用格式:
y=encode_switch(x)
%x:
模拟输入
%y:
二进制编码输出
switchfloor(2*x)
case-2
y='00';
case-1
y='01';
case0
y='10';
case1
y='11';
otherwise
error('x的范围必须在【-1,】之间');
end
编程计算fibnacci序列,f
(1)=1,f
(2)=2,f(i+2)=f(i)+f(i+1),i≥1
(1)用for循环计算前序列的前50个值
%用for循环计算前序列的前50个值
fori=1:
50
f(i+2)=f(i)+f(i+1);f(i)
end
(2)
%用while循环计算序列中所有样值小于5000的部分
f
(1)=1,f
(2)=1;f(3)=f
(2)+f
(1);
i=1;
whilef(i)<=5000
f(i+2)=f(i)+f(i+1);
i=i+1;f(i-1)
end
Togetstarted,selectMATLABHelporDemosfromtheHelpmenu.
Theelementtype"name"mustbeterminatedbythematchingend-tag"".
Couldnotparsethefile:
c:
\matlab7\toolbox\ccslink\ccslink\info.xml
>>diaryexp3
>>E=[{1},{1,2,3,4,5},{rand(2,2)},{'babcdefgh><$@8*_+|SaL{}"?
'}]
E=
[1][1][2][3][4][5][2x2double][1x25char]
>>celldisp(E)
E{1}=
1
E{2}=
1
E{3}=
2
E{4}=
3
E{5}=
4
E{6}=
5
E{7}=
0.95010.6068
0.23110.4860
E{8}=
babcdefgh><$@8*_+|SaL{}"?
>>student_1=struct('number','20080101','name','张三','sex','女','age',18)
student_1=
number:
'20080101'
name:
'张三'
sex:
'女'
age:
18
>>student_2.number=20080102
student_2=
number:
20080102
>>student_2.name='王五'
student_2=
number:
20080102
name:
'王五'
>>student_2.sex='女'
student_2=
number:
20080102
name:
'王五'
sex:
'女'
>>student_2.age=18
student_2=
number:
20080102
name:
'王五'
sex:
'女'
age:
18
>>student_3=struct('number','20080103','name','张四','sex','男','age',19)
student_3=
number:
'20080103'
name:
'张四'
sex:
'男'
age:
19
>>student_4=struct('number','20080104','name','张六','sex','男','age',19)
student_4=
number:
'20080104'
name:
'张六'
sex:
'男'
age:
19
>>student_1.name
ans=
张三
>>student_3.sex
ans=
男
>>student_4.age
ans=
19
>>edit
?
?
?
Inputargument"x"isundefined.
Errorin==>encodeat7
ifx<-1|x>1
>>y=encode(-0.7)
y=
00
>>y=encode(-0.2)
y=
01
>>x=0.2;y=encode(x)
y=
10
>>x=0.8;y=encode(x)
y=
11
>>edit
>>y=encode_switch(-0.7)
y=
00
>>y=encode_switch(-0.2)
y=
01
>>x=0.2;y=encode_switch(x)
y=
10
>>x=0.8;y=encode_switch(x)
y=
11
>>f=
Columns1through7
11235813
Columns8through14
21345589144233377
Columns15through21
610987159725844181676510946
ans=
1
ans=
1
ans=
2
ans=
3
ans=
5
ans=
8
ans=
13
ans=
21
ans=
34
ans=
55
ans=
89
ans=
144
ans=
233
ans=
377
ans=
610
ans=
987
ans=
1597
ans=
2584
ans=
4181
>>edit
ans=
1
ans=
1
ans=
2
ans=
3
ans=
5
ans=
8
ans=
13
ans=
21
ans=
34
ans=
55
ans=
89
ans=
144
ans=
233
ans=
377
ans=
610
ans=
987
ans=
1597
ans=
2584
ans=
4181
ans=
6765
ans=
10946
ans=
17711
ans=
28657
ans=
46368
ans=
75025
ans=
121393
ans=
196418
ans=
317811
ans=
514229
ans=
832040
ans=
1346269
ans=
2178309
ans=
3524578
ans=
5702887
ans=
9227465
ans=
14930352
ans=
24157817
ans=
39088169
ans=
63245986
ans=
102334155
ans=
165580141
ans=
267914296
ans=
433494437
ans=
701408733
ans=
1.1349e+009
ans=
1.8363e+009
ans=
2.9712e+009
ans=
4.8075e+009
ans=
7.7787e+009
ans=
1.2586e+010
>>diaryoff
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- if 编写 函数 文件 程序 清单 第三次 试验