matlab总练习题完整版.docx
《matlab总练习题完整版.docx》由会员分享,可在线阅读,更多相关《matlab总练习题完整版.docx(25页珍藏版)》请在冰豆网上搜索。
![matlab总练习题完整版.docx](https://file1.bdocx.com/fileroot1/2022-12/1/3e4c6341-abc5-48b8-8e50-027e682518e0/3e4c6341-abc5-48b8-8e50-027e682518e01.gif)
matlab总练习题完整版
>>vpa('pi',20)
ans=
3.14932385
>>vpa('exp
(1)',20)
ans=
2.70452354
>>x=linspace(-pi,pi,21);
>>y=sin(x)
y=
Columns1through6
-0.0000-0.3090-0.5878-0.8090-0.9511-1.0000
Columns7through12
-0.9511-0.8090-0.5878-0.309000.3090
Columns13through18
0.58780.80900.95111.00000.95110.8090
Columns19through21
0.58780.30900.0000
å-
>>k=1:
1000;
>>kk=1./k;
>>kkk=kk./k;
>>res=sum(kkk)-(pi^2)/6
res=
-9.9950e-04
%承接上题
>>sum(kk)-log(1000)
ans=
0.5777
>>power(1+eps,1/eps)
ans=
2.7183
>>a=rand(2,3)
a=
0.81470.12700.6324
0.90580.91340.0975
>>x=a(1,:
)
x=
0.81470.12700.6324
>>y=a(2,:
)
y=
0.90580.91340.0975
>>norm(x)
ans=
1.0391
>>norm(y)
ans=
1.2900
>>acos(dot(x,y)/norm(x)/norm(y))
ans=
0.8189
>>rand(3,3)
ans=
0.27850.96490.9572
0.54690.15760.4854
0.95750.97060.8003
>>det(ans)
ans=
0.2937
线性无关
a=
0.39220.7060
0.65550.0318
0.17120.2769
>>x=a(1,:
)
x=
0.39220.7060
>>y=a(2,:
)
y=
0.65550.0318
>>z=a(3,:
)
z=
0.17120.2769
>>alpha=x-z
alpha=
0.22100.4291
>>beta=y-z
beta=
0.4843-0.2451
>>alpha=[alpha0]
alpha=
0.22100.42910
>>beta=[beta0]
beta=
0.4843-0.24510
>>cross(alpha,beta)
ans=
00-0.2620
面积0.2620
>>a=11:
19;
>>b=a;
>>fork=1:
8
b=[b;a+10*k];
end
>>rank(b)
ans=
2
>>a=vander(1:
9);
>>b=fliplr(a)
b=
Columns1through5
11111
124816
1392781
141664256
1525125625
16362161296
17493432401
18645124096
19817296561
Columns6through9
1111
3264128256
24372921876561
102440961638465536
31251562578125390625
7776466562799361679616
168071176498235435764801
32768262144209715216777216
59049531441478296943046721
>>det(b)
ans=
5.0566e+15
方式一>>f=@(x,y)exp(x+y)+sin((x^2)+(y^2))
f=
@(x,y)exp(x+y)+sin((x^2)+(y^2))
>>f(1,2)
ans=
19.1266
方式二
functionf=myfunfun(x,y)
f=exp(x+y)+sin((x^2)+(y^2));
>>myfunfun(1,2)
ans=
19.1266
>>
Char1.4142426773176679737995343276415727
>>a=ans;
>>sqrt2char(3-2)=a(3)
sqrt2char=
4142426773176679737995343276415727
>>forx=1:
100
b(x)=str2num(sqrt2char(x))
end
>>sum(b)/100
ans=
4.8100
f=@(x)(x^3)*sin(x)+(x^2)/3+x*cos(x)
f=
@(x)(x^3)*sin(x)+(x^2)/3+x*cos(x)
>>ezplot(f,-2,1)
>>x0=fzero(f,-1)
x0=
-0.7889
另一根为0,是显然的
functiony=difun(x)
ifx<-pi
y=-x-pi;
elseifx>-pi&xy=sin(x);
else
y=(x-pi)/2;
end
end
>>y=[]
y=
[]
>>forx=-6:
0.05:
6
y=[ydifun(x)];
end
>>plot(x,y)
>>plot([-6:
0.05:
6],y)
>>pi/4
ans=
0.7854%pi/4的理想值
矩形公式:
functiony=rectangle(n)
x=0:
1/n:
1;
a=1./(1+x.*x);
y=sum(a)*(1/n);
end
>>rectangle(1000)
ans=
0.7861
>>rectangle(10000)
ans=
0.7855
>>rectangle(100000)
ans=
0.7854
梯形公式:
functiony=trapezoid(n)
x=0:
1/n:
1;
a=1./(1+x.*x);
begin=a
(1);
endd=a(n+1);
a
(1)=0;
a(n)=0;
y=sum(a)*(1/n)+begin*(1/n)*0.5+endd*(1/n)*0.5;
end
trapezoid(1000)
ans=
0.7854
>>trapezoid(100)
ans=
0.7853
Simpson公式
functiony=simpson(n)
x=0:
1/n:
1;
a=thefun(x);
begin=a
(1);
endd=a(n+1);
medium=[];
forx=1:
n
medium=[medium(a(x)+a(x+1))*0.5];
end
a
(1)=0;
a(n)=0;
y=begin*(1/n)*(1/6)+endd*(1/n)*(1/6)+sum(a)*(1/n)*(1/3)+sum(medium)*(1/n)*(1/6)*4;
end
functione=thefun(r)
e=1./(1+r.*r);
end
>>simpson(100)
ans=
0.7854
>>simpson(10)
ans=0.7832
>>A=[621-1;2410;114-1;-10-13];
>>b=[615-5]';
>>x=A\b
x=
0.7906
-0.3613
0.8639
-1.1152
>>[diag(1:
4)eye(4)]
ans=
10001000
02000100
00300010
00040001
functionyh=yhsj(n)
yh=1;
disp
(1);
fork=2:
n
yh=[yh,0]+[0,yh];
disp(yh)
end
end
%n=11运行
>>x=sym('x');
>>f=sqrt(1+((4/9)*x^(1/2))^2)
f=
((16*x)/81+1)^(1/2)
>>a=sym('a');
>>b=sym('b');
>>int(f,a,b)
ans=
(16*b+81)^(3/2)/216-(16*a+81)^(3/2)/216
>>t=sym('t');
>>a=sym('a');
>>x=a*(t-sin(t))
x=
a*(t-sin(t))
>>y=a*(1-cos(t))
y=
-a*(cos(t)-1)
>>dx=diff(x)
dx=
-a*(cos(t)-1)
>>dy=diff(y)
dy=
a*sin(t)
>>int(sqrt(dx^2+dy^2),0,2*pi)
ans=
8*(a^2)^(1/2)
>>p=polyfit([-pi-pi/20pi/2pi],[0-1010],5)
p=
Columns1through5
-0.03490.00000.3440-0.00000
Column6
-0.0000
>>plot([-pi:
pi/100:
pi],polyval(p,-pi:
pi/100:
pi));
>>holdon
>>plot([-pi:
pi/100:
pi],sin(-pi:
pi/100:
pi));
>>
functiona=num2p(n)
strr=num2str(n);
index=size(strr);
indexx=index
(2);
a=[];
fork=indexx-1:
-1:
0
a=[arem(fix(n/(10^k)),10)];
end
end
>>num2p(95489298494)
ans=
Columns1through8
95489298
Columns9through11
494
functionppi=ttry(n)
pointx=-1+2*rand(1,n);
pointy=-1+2*rand(1,n);
index=pointx.*pointx+pointy.*pointy;
indexx=index<=1;
ppi=4*sum(indexx)/n;
end
>>ttry(100)
ans=
3.1200
>>ttry(10000)
ans=
3.1376
functiony=collatz(n)
ifn==1
y=1
return;
else
ifrem(n,2)==0
n=n/2;
else
n=3*n+1;
end
n
y=collatz(n);
end
>>collatz(12)
n=
6
n=
3
n=
10
n=
5
n=
16
n=
8
n=
4
n=
2
n=
1
y=
1
>>f=@(x)1./(1+x.^2)
f=
@(x)1./(1+x.^2)
>>y=f(x);
>>x=0:
0.1:
1;
>>y=f(x);
>>p=polyfit(x,y,5)
p=
Columns1through5
-0.23720.35290.5071-1.13430.0115
Column6
0.9999
>>intp=polyint(p)
intp=
Columns1through5
-0.03950.07060.1268-0.37810.0058
Columns6through7
0.99990
>>res1=polyval(intp,1)-polyval(intp,0)
res1=
0.7854%这是插值拟合解
>>atan
(1)
ans=
0.7854%actan
(1)公式解
fid=fopen('C:
\陈民权的文档2\matlab作业\pi_1m.txt','r')
fid=
3
>>b=fscanf(fid,'%s');
>>fclose(fid)
ans=
0
>>b(1:
55)=[];
编辑“givemerun.m”文件:
y=1;
n=zeros(1,10)
flag=0;
forx=1:
1500000
ify==1000001
break
else
ifb(x)~='['&flag==0&isempty(str2num(b(x)))~=1
c=b(x);
d=str2double(c);
n(d+1)=n(d+1)+1;
y=y+1;
elseifb(x)=='['
flag=1;
elseifb(x)==']'
flag=0;
end
end
end
n
>>givemerun%运行“givemerun”
n=
0000000000
n=
n=
Columns1through3
9995999758100026
Columns4through6
100229100230100359
Columns7through9
995489980099985
Column10
100106%分别为0,1,2,…,8,9在前一百万位出现的次
>>sum(n)
ans=
1000000
>>cc=num2str(n)
cc=
9995999758100026100229100230100359995489980099985100106>>fopen('res.txt','w')
ans=
6
>>fprintf(6,cc)
ans=
78
functionf=veryfun(n)
ifn==6174
return
else
index
(1)=(n-rem(n,1000))/1000;
index
(2)=fix(n/100)-index
(1)*10;
index(3)=rem(fix(n/10),10);
index(4)=rem(n,10);
index2=sort(index,'ascend');
index3=sort(index,'descend');
a=[1000100101];
max=index3*(a');
min=index2*(a');
max-min
veryfun(max-min);
end
end
>>veryfun(9864)
ans=
5175
ans=
5994
ans=
5355
ans=
1998
ans=
8082
ans=
8532
ans=
6174
functionm=tem(index,t)
m=index
(1).*exp(index
(2).*t)
end
>>[index,r,j,c,m,e]=nlinfit(t,m,@tem,[00])
index=
78.7137-0.1047
%在这里省略中间的迭代过程和表格变量输入过程
>>m=tem(index,[0:
0.1:
25]);
>>plot([0:
0.1:
25],m)
functionson=sonfind(A)
index=sum(A)/2;
fork=1:
5
b=nchoosek([1:
10],k);
sizee=size(b);
kk=sizee
(1);
forx=1:
kk
c=b(x,:
);
A_son=[];
fory=c
A_son=[A_sonA(y)];
end
ifsum(A_son)==index
son=A_son;
break
return
end
end
end
end
%集合元素有互异性,但不影响验证!
>>sonfind(11:
20)
>>sonfind([105510601010101010])
ans=
5560
>>sonfind([10101010901010101010])
ans=
90
%进一步验证(这里元素互异)
>>sonfind([21222324251112131465])
ans=
2122232425
>>sort([21222324251112131465])
ans=
11121314212223242565
>>sonfind(ans)
ans=
1112131465
201612