Matlab编程与工程应用第二版习题解答全.docx
《Matlab编程与工程应用第二版习题解答全.docx》由会员分享,可在线阅读,更多相关《Matlab编程与工程应用第二版习题解答全.docx(37页珍藏版)》请在冰豆网上搜索。
![Matlab编程与工程应用第二版习题解答全.docx](https://file1.bdocx.com/fileroot1/2022-12/1/32d470b5-48dc-416b-9210-9021e801f5f7/32d470b5-48dc-416b-9210-9021e801f5f71.gif)
Matlab编程与工程应用第二版习题解答全
Matlab编程与工程应用〔第二版习题解答
第一章:
MATLAB简介
1.
>>myage=25;
>>myage=myage-1;
>>myage=myage+2;
2.
>>myage=25;
>>myage=myage-1;
>>myage=myage+2;
3.
>>namelengthmax
ans=
63
4.
>>formatbank
>>1/3
ans=
0.33
5.
>>formatrat
>>5/6+2/7
ans=
47/42
6.
25194.333399
7.
>>pounds=100;
>>kilos=2.2*pounds
kilos=
220.0000
8.
>>R1=20;R2=30;R3=40;
>>RT=\1
RT=
9.2308
9.
>>ftemp=90;
>>ctemp=*5/9
ctemp=
32.2222
10.略
11.
>>sind<90>
ans=
1
12.
>>sita=2*pi/3;
>>r=2;
>>x=r*cos
x=
-1.0000
>>y=r*sin
y=
1.7321
13.
>>t=100;V=50;
>>wcf=35.7+0.6*t-35.7+0.16*V+0.43*t*0.16*V
wcf=
412
14.
Fix<3.5>=3;Floor<3.5>=3;
Fix<3.4>=3;Fix<-3.4>=-3;
Fix<3.2>=3;Floor<3.2>=3;
Fix<-3.2>=-3;Floor<-3.2>=-4;
Fix<-3.2>=-3;Ceil<-3.2>=-3;
15.
Sqrt<19>
3^1.2
Tan
16.
Intmin<‘int32’>;intmax<‘int32’>;intmin<‘int64’>;intmax<‘int64’>;
17.
Realmin<‘double’>;Realmax<‘double’>;
18.
>>DblNum=33.8;
>>int32Num=int32
int32Num=
34
19.
>>A1=rand;
A2=20*rand;
A3=30*rand+20;
A4=round;
A5=round;/
A6=round+50;
>>A4=randi<10>;
>>A5=randi<11>;
>>A6=randi<50>+50;
20.略
21.
Double〔‘A’;所以大写在前,小写在后;
22.
>>CharNum='xyz';
>>CharNum=char
CharNum=
vwx
23.
>>vec=[3:
1:
6]
vec=
3456
>>vec2=[1.0000:
0.5000:
3.0000]
vec2=
1.00001.50002.00002.50003.0000
>>vec3=[5:
-1:
2]
vec3=
5432
24.
>>vec1=linspace<4,8,3>
vec1=
468
>>vec2=linspace<-3,-15,5>
vec2=
-3-6-9-12-15
>>vec3=linspace<9,5,3>
vec3=
975
25.
>>vec=[1:
1:
10]
vec=
12345678910
>>vec=linspace<1,10,10>
vec=
12345678910
>>vec2=[2:
5:
12]
vec2=
2712
>>vec=linspace<2,12,3>
vec=
2712
26.
>>myend=randi<4>+8;
>>vec1=[1:
3:
myend]
vec1=
14710
27.
>>vec=[-1:
0.2:
1]'
vec=
-1.0000
-0.8000
-0.6000
-0.4000
-0.2000
0
0.2000
0.4000
0.6000
0.8000
1.0000
28.
>>vec=[0:
1:
15];
>>n=[1:
2:
numel-1];
>>vec2=vec
vec2=
02468101214
29.
function[ab]=PI_CONT
%UNTITLEDSummaryofthisfunctiongoeshere
%Detailedexplanationgoeshere
n=1:
1:
fix/2>;
a=x;
n=1+fix/2>:
1:
numel;
b=x;
end
rem,2>
30.
rand<2,3>
10*rand<2,3>
5+round<15*rand<2,3>>
randint<2,3,[515]>
31.
rows=randi<5>;
cols=randi<5>;
y=zeros;
32.
>>mat=[78910
121086];
>>A1=mat<1,3>
>>A2=mat<2,:
>
>>A3=mat<:
1:
2>
33.
>>mymat=[234;567]
>>mymat1=fliplr
>>mymat2=flipud
>>mymat3=rot90
34.
>>mymatzero=zeros<4,2>
>>mymatzero<2,:
>=[3,6]
35.
>>x=linspace<-pi,pi,20>;
>>y=sin;
36.
>>randmat=randint<3,5,[-55]>
>>sign
37.
>>randmat=randint<4,6,[-55]>;
>>randmat2=abs
38.
>>randmat=rand<3,5>
>>randmat<3,:
>=[]
39.
>>vec=1:
1:
1000;
>>vec
>>vec>
>>[ab]=size;
>>vec
40.
同上
41.
>>mat=zeros<3,5>;
>>mat<:
:
1>=zeros<3,5>;
>>mat<:
:
2>=zeros<3,5>;
42.
>>myc=clock
>>today=myc<1:
3>
>>now=myc<4:
6>
>>now=fix
第二章MATLAB程序设计概述
1.
ri=2;%Radiusinternal
ro=4;%Radiusouter
V=4*pi/3*%calculatevolume
2.
%CalculateAtomicWeightH2O2
Weight_O=15.9994;
Weight_H=1.0079;
AtomicWeight=Weight_O*2+Weight_H*2%CalculateH2O2AtomicWeight
3.
fprintf<'LengthofThecharacterstringis:
%d\n',…
length
\n','s'>>>;
4.
NewNumber=input<'pleaseinputaNumber:
\n'>;
fprintf<'Numberwith2decimalis:
%0.2f\n',NewNumber>;
5.
>>vec=input<'Enteramatrix:
'>
Enteramatrix:
[1:
1:
10;1:
1:
10]
6.
>>fprintf<'OUTPUT:
%f\n',12345.6789>
OUTPUT:
12345.678900
>>fprintf<'OUTPUT:
%10.4f\n',12345.6789>
OUTPUT:
12345.6789
>>fprintf<'OUTPUT:
%10.2f\n',12345.6789>
OUTPUT:
12345.68
>>fprintf<'OUTPUT:
%6.4f\n',12345.6789>
OUTPUT:
12345.6789
>>fprintf<'OUTPUT:
%2.4f\n',12345.6789>
OUTPUT:
12345.6789
7.
>>fprintf<'OUTPUT:
%d\n',12345>
OUTPUT:
12345
>>fprintf<'OUTPUT:
%5d\n',12345>
OUTPUT:
12345
>>fprintf<'OUTPUT:
%8d\n',12345>
OUTPUT:
12345
>>fprintf<'OUTPUT:
%3d\n',12345>
OUTPUT:
12345
8.
>>x=12.34;
>>y=4.56;
>>fprintf<'xis%.3f\n',x>
xis12.340
>>fprintf<'xis%0.0f\n',x>
xis12
>>fprintf<'yis%0.1f\n',y>
yis4.6
>>fprintf<'yis%0.1f!
\n',y>
yis4.6!
9.
%CalculateAreaofthesquareness
fprintf<'TheAreaofTheSquarenessis%.2f',…
>…
*>>;
10.
NewName=input<'Whatisyourname?
','s'>;
fprintf<'Wow,yournameis%s!
\n',NewName>;
11.
NewString=input<'Enteryourstring:
','s'>;
fprintf<'YourStringwas:
''%s''\n',NewString>;
12.
WaterSpeed=input<'Enterthefolwinm^3/s:
'>;
WaterSpeedft=0.028\WaterSpeed;
fprintf<'Aflowrateof%.3fmeterspersec\n',WaterSpeed>;
fprintf<'isequivalentto%.3ffeetpersec\n',WaterSpeedft>;
13.
IncomeY=input<'Inputyourincomeeveryyear?
'>;
FoodCostY=[IncomeY*8/100IncomeY*10/100];
FoodCostM=12\FoodCostY;
fprintf<'FoodCostineveryyearis%.2f-%.2f\n',FoodCostY<1>,FoodCostY<2>>;
fprintf<'FoodCostineveryMonthis%.2f-%.2f\n',FoodCostM<1>,FoodCostM<2>>;
14.
Weight=input<'PleaseinputtheWeightoftheplan\n'>
Area=input<'Pleaseinputtheareaoftheplanwing\n'>
fprintf<'ChargeofPlanWinis%.2f\n',Weight/Area>
15.
x=10;
y=10;
plot;
16.
clf
x=-2:
0.1:
2;
y=exp;
plot;
xlabel<'X'>;
ylabel<'Y'>;
legend<'Exp'>;
Title<'ExpExample'>;
17.
clf
x=1:
5:
100;
y=sqrt;
plot;
holdon;
bar;
18.
clf
y=randint<1,100,[0100]>;
x=1:
1:
length;
plot;
holdon
plot;
figure<2>;
plot;
figure<3>;
bar
19.
clf
x=linspace<0,pi,10>;
y=sin;
plot;
figure<2>;
x=linspace<0,pi,100>;
y=sin;
plot;
20.
loadTimeTemp.dat
x=TimeTemp<:
1>';
y=TimeTemp<:
2>';
plot;
Xlabel<'Height'>;
Ylabel<'Temperature'>;
Title<'Height-Temperature'>;
legend<'TEMP'>
21.
>>randint<3,6,[50100]>;
>>saveRandInt.batans-ascii;
>>randint<2,6,[50100]>
>>saveRandInt.batrandint-ascii–append
>>loadRandInt.bat
>>RandInt
22.
>>loadtesttan.bat
>>tan
23.
>>loadhightemp.dat
>>hightemp<:
1>=hightemp<:
1>.+1900
>>savey2ktemp.dathightemp–ascii
24.
function[fn]=fn
%Calculateyasafunctionofx
fn=x^3-4*x^2+sin;
end
25.
function[mwh]=mwh_to_gj
%conversfromMWhtoGJ
mwh=3.6*x;
end
26.
function[output_args]=miletometer
%Convermiletometer
Mile=input<'mileperhoursnumber:
'>;
meter=Mile*5280*0.3048/3600;
output_args=meter;
fprintf<'Meterofthemileis:
%.2f',meter>;
end
27.
function[Tn]=Interest
%UNTITLED4Summaryofthisfunctiongoeshere
Tn=p*<1+i>*n;
end
28.
略
29.
function[V]=SpeedConv
%UNTITLED4Summaryofthisfunctiongoeshere
V=1.016*sqrt
end
30.function[THR]=Cal_rate
%UNTITLED4Summaryofthisfunctiongoeshere
THR=<220-A>*0.6;
end
31.
function[nJ]=STL_calc
%UNTITLED4Summaryofthisfunctiongoeshere
nJ=sqrt<2*pi*n>*>^n;
end
32.
function[output_args]=costn
%UNTITLED5Summaryofthisfunctiongoeshere
output_args=5*n^2-44*n+11;
end
n=input<'Enterthenumberofunits:
'>;
Cn=costn;
fprintf<'Thecostfor100unitswillbe$%.2f\n',Cn>;
33.
略〔仿造32题
34.
function[V]=oblong
%UNTITLED8Summaryofthisfunctiongoeshere
V=1/12*sqrt<2>*length^3;
end
fprintf<'Thevolumeofoblongis:
%.2f\n',oblong
\n'>>>
35.
function[y]=pickone
%pickonereturnsarandomelementfromvectorx
n=length;
y=x>;
end
36.
function[y]=vecout
%UNTITLED11Summaryofthisfunctiongoeshere
y=x:
1:
x+5;
end
37.
b=input<'Enterthefirstside:
'>;
c=input<'Enterthesecondside:
'>
afa=input<'Entertheanglebetweenthem:
'>
y=sqrt>>;
fprintf<'Thethirdsideis%.3f\n',y>;
38.
略
39.
loadfloatnums.dat
floatnums=round;
floatnums=floatnums';
saveintnums.datfloatnums-ascii;
40.
loadcostssales.dat
n=length1>>;
fprintf<'Therewere%dquartersinthefile',n>;
n=1:
1:
n;
costs=costssales<:
1>;
sales=costssales<:
1>;
plot;
holdon
plot;
legend<'COSTS','SALES'>;
xlabel<'COST'>;
ylabel<'SALE'>;
title<'COST-SALE'>;
costssales=costssales'
costssales=flipud;
savenewfile.datcostssales-ascii;
第三章选择语句
2.if
\n','s'>~='x'>
disp<'sorrythecharnotx'>;
end
3.functiony=Test3
if
y=1;
else
y=x+1;
end;
4.disp<'Theprogramwillcalculatethevolumeofapyramid'>;
a=input<'Enterthelengthofthebase:
'>;
c=input<'Isthatiorc?
','s'>;
if
a=a/2.54;
end;
b=input<'Enterthewidthofthebase:
','s'>;
c=input<'Isthatiorc?
'>;
if
b=b/2.54;
end;
d=input<'Enterthelengthofthebase:
','s'>;
c=input<'Isthatiorc?
'>;
if
d=d/2.54;
end;
fprintf<'TheVolumeofthePyramidis%.3fcubicinches.',a*b*d/3>;
5.
if
','s'>==