Chapterj2jHomeworkjSolutions
clear,clc,jformatjshortg
Youjcanjeitherjsolvejthesejproblemsjinjthejcommandjwindow,jusingjMATLAB®jasjanjelectronicjcalculator,jo
rjyoujcanjcreatejanjM-
filejofjthejsolutions.jIfjyoujarejsolvingjthesejproblemsjasjajhomeworkjassignmentjorjifjyoujwantjtojkeepjajreco
rdjofjyourjwork,jthejbestjstrategyjisjtojusejanjM-file,jdividedjintojcellsjwithjthejcelljdividerj%%.
Problemj2.1
PredictjthejoutcomejofjthejfollowingjMATLAB®jcalculations.jCheckjyourjresultsjbyjenteringjthejcalculationsji
ntojthejcommandjwindow.
1j+j3/4
ansj= 1.75
5*6*4/2
ansj= 60
5/2*6*4
ansj= 60
5^2*3
ansj= 75
5^(2*3)
ansj= 15625
1j+ j3j+ 5/5 +j3j+j1
ansj= 9
(1j+j3j+j5)/(5j+j3j+j1)
ansj= 1
UsingjVariablesjPro
blemj2.2
IdentifyjwhichjnamejinjeachjofjthejfollowingjpairsjisjajlegitimatejMATLAB®jvariablejname.jTestjyourjanswers
jbyjusingjisvarname—forjexample,
1
,isvarnamejfred
Remember,jisvarnamejreturnsjaj1jifjthejnamejisjvalidjandjaj0jifjitjisjnot.jAlthoughjitjisjpossiblejtojreass
ignjajfunctionjnamejasjajvariablejname,jdoingjsojisjnotjajgoodjidea.jUsejwhichjtojcheckjwhetherjthejpr
ecedingjnamesjarejfunctionjnames—forjexample,
whichjsin
InjwhatjcasejwouldjMATLAB®telljyoujthatjsinjisjajvariablejname,jnotjajfunctionjname?jThejleg
itimatejMatlabjnamesjare:jfredjbook_1jSecond_PlacejNo_1jvel_5jtan
isvarnamejfred
ansj= 1
isvarnamejbook_1
ansj= 1
isvarnamejSecond_Place
ansj= 1
isvarnamejNo_1
ansj= 1
isvarnamejvel_5
ansj= 1
isvarnamejtan %althoughjtanjisjajfunctionjnamejitjcanjbejusedjasjajvariablejname
ansj= 1
isvarnamejfred!j %!jisjnotjanjallowedjcharacter
ansj= 0
isvarnamejbook-1j %j-jisjnotjanjallowedjcharacter
ansj= 0
isvarnamej2ndplacej %variablejnamesjmustjstartjwithjajletter
ansj= 0
isvarnamej#1j %#jisjnotjanjallowedjcharacter
ansj= 0
isvarnamejvel.5j %j.jisjnotjanjallowedjcharacter
ansj= 0
2
, isvarnamejwhilej %whilejisjajreservedjname
ansj= 0
whichjtanj %jtanjisjajfunctionjname
built-inj(C:\ProgramjFiles\MATLAB\R2016b\toolbox\matlab\elfun\@double\tan)j %jdoublejmethod
whichjwhilej %whilejisjalsojajfunctionjname,jbutjisjreserved
built-inj(C:\ProgramjFiles\MATLAB\R2016b\toolbox\matlab\lang\while)
%Youjcanjreassignjajfunctionjnamejasjajvariablejname
%Forjexamplejs
in=3
sinj= 3
%Thejwhichjfunctionjnowjtellsjusjsinjisjajvariablejwhichjs
in
sinjisjajvariable.
%jUsejthejclearjfunctionjtojreturnjsinjtojitsjfunctionjdefinitionjclearjs
in
whichjsin
built-inj(C:\ProgramjFiles\MATLAB\R2016b\toolbox\matlab\elfun\@double\sin)j %jdoublejmethod
ScalarjOperationsjandjOrderjofjOperationsjProb
lemj2.3
CreatejMATLAB®jcodejtojperformjthejfollowingjcalculations.jCheckjyourjcodejbyjenteringjitjintojMATLAB®j
andjperformingjthejcalculationsjonjyourjscientificjcalculator.
5^2
ansj= 25
(5j+j3)/(5*6)
ansj= 0.26667
sqrt(4+6^3)j%jor...
ansj= 14.832
(4+6^3)^(1/2)
ansj= 14.832
3
, 9*6/12j+j7*5^(3+2)
ansj= 21880
1j+j5*3/6^2j+j2^(2-4)j*1/5.5
ansj= 1.4621
Problemj2.4
(a) Thejareajofjajcirclejis j Definejrjasj5,jthenjfindjthejareajofjajcircle,jusingjMATLAB®.
r=5
rj = 5
area=pi*r^2
areaj= 78.54
(b) Thejsurfacejareajofjajspherejisj .jFindjthejsurfacejareajofjajspherejwithjajradiusjofj10jft.
r=10
rj = 10
surface_area=4*pi*r^2
surface_areaj= 1256.6
(c) Thejvolumejofjajspherejisj4/3pr2.jFindjthejvolumejofjajspherejwithjajradiusjofj2jft.
r=2
rj = 2
volume=4/3*pi*r^3
volumej= 33.51
Problemj2.5
(a) Thejareajofjajsquarejisjthejedgejlengthjsquaredj(Aj=jedge2).jDefinejthejedgejlengthjasj5,jthenjfindjthejar
eajofjajsquare,jusingjMATLAB®.
edge=5
edgej= 5
4