ChapterV2VHomeworkVSolutions
clear,clc,VformatVshortg
YouVcanVeitherVsolveVtheseVproblemsVinVtheVcommandVwindow,VusingVMATLAB®VasVanVelectronicV
calculator,VorVyouVcanVcreateVanVM-
fileVofVtheVsolutions.VIfVyouVareVsolvingVtheseVproblemsVasVaVhomeworkVassignmentVorVifVyouVwant
VtoVkeepVaVrecordVofVyourVwork,VtheVbestVstrategyVisVtoVuseVanVM-
file,VdividedVintoVcellsVwithVtheVcellVdividerV%%.
ProblemV2.1
PredictVtheVoutcomeVofVtheVfollowingVMATLAB®Vcalculations.VCheckVyourVresultsVbyVenteringVtheVc
alculationsVintoVtheVcommandVwindow.
1V+V3/4
ansV= 1.75
5*6*4/2
ansV= 60
5/2*6*4
ansV= 60
5^2*3
ansV= 75
5^(2*3)
ansV= 15625
1V+V3V+ 5/5 +V3V+V1
ansV= 9
(1V+V3V+V5)/(5V+V3V+V1)
ansV= 1
UsingVVariablesVPr
oblemV2.2
IdentifyVwhichVnameVinVeachVofVtheVfollowingVpairsVisVaVlegitimateVMATLAB®VvariableVname.VTestV
yourVanswersVbyVusingVisvarname—forVexample,
1
,isvarnameVfred
Remember,VisvarnameVreturnsVaV1VifVtheVnameVisVvalidVandVaV0VifVitVisVnot.VAlthoughVitVisVp
ossibleVtoVreassignVaVfunctionVnameVasVaVvariableVname,VdoingVsoVisVnotVaVgoodVidea.VUseV
whichVtoVcheckVwhetherVtheVprecedingVnamesVareVfunctionVnames—forVexample,
whichVsin
InVwhatVcaseVwouldVMATLAB®tellVyouVthatVsinVisVaVvariableVname,VnotVaVfunctionVna
me?VTheVlegitimateVMatlabVnamesVare:VfredVbook_1VSecond_PlaceVNo_1Vvel_5Vtan
isvarnameVfred
ansV= 1
isvarnameVbook_1
ansV= 1
isvarnameVSecond_Place
ansV= 1
isvarnameVNo_1
ansV= 1
isvarnameVvel_5
ansV= 1
isvarnameVtan %althoughVtanVisVaVfunctionVnameVitVcanVbeVusedVasVaVvariableVname
ansV= 1
isvarnameVfred!V %!VisVnotVanVallowedVcharacter
ansV= 0
isvarnameVbook-1V %V-VisVnotVanVallowedVcharacter
ansV= 0
isvarnameV2ndplaceV %variableVnamesVmustVstartVwithVaVletter
ansV= 0
isvarnameV#1V %#VisVnotVanVallowedVcharacter
ansV= 0
isvarnameVvel.5V %V.VisVnotVanVallowedVcharacter
ansV= 0
2
, isvarnameVwhileV %whileVisVaVreservedVname
ansV= 0
whichVtanV %VtanVisVaVfunctionVname
built-inV(C:\ProgramVFiles\MATLAB\R2016b\toolbox\matlab\elfun\@double\tan)V %VdoubleVmethod
whichVwhileV %whileVisValsoVaVfunctionVname,VbutVisVreserved
built-inV(C:\ProgramVFiles\MATLAB\R2016b\toolbox\matlab\lang\while)
%YouVcanVreassignVaVfunctionVnameVasVaVvariableVname
%ForVexampleVs
in=3
sinV= 3
%TheVwhichVfunctionVnowVtellsVusVsinVisVaVvariableVwhichV
sin
sinVisVaVvariable.
%VUseVtheVclearVfunctionVtoVreturnVsinVtoVitsVfunctionVdefinitionVclearV
sin
whichVsin
built-inV(C:\ProgramVFiles\MATLAB\R2016b\toolbox\matlab\elfun\@double\sin)V %VdoubleVmethod
ScalarVOperationsVandVOrderVofVOperationsVP
roblemV2.3
CreateVMATLAB®VcodeVtoVperformVtheVfollowingVcalculations.VCheckVyourVcodeVbyVenteringVitVintoV
MATLAB®VandVperformingVtheVcalculationsVonVyourVscientificVcalculator.
5^2
ansV= 25
(5V+V3)/(5*6)
ansV= 0.26667
sqrt(4+6^3)V%Vor...
ansV= 14.832
(4+6^3)^(1/2)
ansV= 14.832
3
, 9*6/12V+V7*5^(3+2)
ansV= 21880
1V+V5*3/6^2V+V2^(2-4)V*1/5.5
ansV= 1.4621
ProblemV2.4
(a) TheVareaVofVaVcircleVis V DefineVrVasV5,VthenVfindVtheVareaVofVaVcircle,VusingVMATLAB®.
r=5
rV= 5
area=pi*r^2
areaV= 78.54
(b) TheVsurfaceVareaVofVaVsphereVisV .VFindVtheVsurfaceVareaVofVaVsphereVwithVaVradiusVofV10Vft.
r=10
rV= 10
surface_area=4*pi*r^2
surface_areaV= 1256.6
(c) TheVvolumeVofVaVsphereVisV4/3pr2.VFindVtheVvolumeVofVaVsphereVwithVaVradiusVofV2Vft.
r=2
rV= 2
volume=4/3*pi*r^3
volumeV= 33.51
ProblemV2.5
(a) TheVareaVofVaVsquareVisVtheVedgeVlengthVsquaredV(AV=Vedge2).VDefineVtheVedgeVlengthVasV5,V
thenVfindVtheVareaVofVaVsquare,VusingVMATLAB®.
edge=5
edgeV= 5
4