s(i)=s(i+1) s(i+1)=a; end end,end s s =
Columns 1 through 8
23 31 35 43 45 57 65 132
Columns 9 through 14
345 356 1563 4123 121233 1223543
10. 两个整数a和b,如果a的因子和等于b,b的因子和也等于a,且a不等于b,则称a,b为一对亲密数。请寻找并输出2000以内的所有亲密数对。
g=[];
for a=1:2000 s1=0; s2=0;
for r=1:a-1
if mod(a,r)==0 s1=s1+r; end end
if a==s1 continue end
if find(g==a)~=0 continue end
for r=1:s1-1
if mod(s1,r)==0 s2=s2+r;
end end
if s2==a
s=[s1 s2]; g=[g;s]; end end g g =
284 220 1210 1184
11.用二分法求函数y=exp(x)-10的0点,要求误差小于0.001
x1=0; x2=10;
while x2-x1>0.001
if (exp(x1)-10)*(exp((x1+x2)/2)-10)>0 x1=(x1+x2)/2; else x2=(x1+x2)/2; end end
x=[x1 x2] x =
2.3022 2.3029
12.现有一组数如下: x y 1 12 2 27 3 34 4 48 5 67 6 79 7 89 8 109 但y的数据不太准确,已知其函数关系可能是y=12*x, y=12*x+1,y=12*x-1,y=12.5*x,请用matlab判断这四个函数,哪个与数据的契合度最
高?(提示:根据函数在各个点的值与原数据差异度来判断契合度)
x=1:8;
y(1,:)=12*x; y(2,:)=12*x+1; y(3,:)=12*x-1; y(4,:)=12.5*x;
y(5,:)=[12 27 34 48 67 79 89 109]; t=zeros(1,4); for i=1:4
for j=1:8
t(i)=t(i)+abs(y(i,j)-y(5,j)); end end
[x,i]=min(t) i =
4 即为y=12.5*x
13.已知rand函数能生成0到1之间的随机数。而古人用抛石头的方法,根据落在在一个圆内的石子数与在园外正方形内的石子数之比来求π,请用matlab软件模拟此过程,并求出π的值,然后,分析所求π的值的误差产生于什么地方。
s=rand(2,1000); pai=0;
for i=1:1000
if s(1,i).^2+s(2,i).^2<1 pai=pai+4/1000; end end pai pai =
3.1120
14.编写一个函数文件,函数的输入项为三维坐标的两个点,函数没有输出项,函数运行结果为画出这两个点所能确定的最小球
function qiu(x1,x2) o=(x1+x2)/2;
r=sqrt(sum((x1-x2).^2))/2; [X,Y,Z] = sphere(50); X=r*(X+o(1)); Y=r*(Y+o(2)); Z=r*(Z+o(3)); mesh(X,Y,Z) axis equal
qiu([1 2 3],[4 5 6])
14131211101110987468
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库matlab程序设计例题及答案(2)在线全文阅读。
相关推荐: