Answers To Problems For MATLAB For Engineers, 6th Edition by Moore

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Contact me in order to access the whole complete document. Email: smtb98@gmail.

com
WhatsApp: https://wa.me/message/2H3BV2L5TTSUF1 Telegram: https://t.me/solutionmanual
complete document is available on https://unihelp.xyz/ *** contact me if site not loaded

MATLAB for Engineers, 6th Edition


ssm
Chapter 2 Homework Solutions
mtt
clear,clc, format shortg

You can either solve these problems in the command window, using MATLAB® as an electronic
calculator, or you can create a program using a script (M-file) or a live script (MLX-file). If you are
solving these problems as a homework assignment or if you want to keep a record of your work, the
bb99
best strategy is to use a program file, divided into sections with section dividers.

clear, clc

Getting Started
Problem 2.1
88@
Predict the outcome of the following MATLAB® calculations. Check your results by entering the
calculations into the command window.
@
1 + 3/4

ans = 1.75

5*6*4/2
ggm
ans = 60

5/2*6*4
maa

ans = 60

5^2*3

ans = 75

5^(2*3)
iill..cc

ans = 15625

1 + 3 + 5/5 + 3 + 1

ans = 9

(1 + 3 + 5)/(5 + 3 + 1)
oom

ans = 1

Using Variables
m
Problem 2.2
Identify which name in each of the following pairs is a legitimate MATLAB® variable name. Test
your answers by using isvarname—for example,

isvarname fred

The function isvarname returns a 1 if the name is valid and a 0 if it is not. Although it is possible
to reassign a function name as a variable name, doing so is not a good idea. Use which to check
whether the preceding names are function names—for example,

which sin

In what case would MATLAB® tell you that sin is a variable name, not a function name?

The legitimate Matlab names are: fred book_1 Second_Place No_1 vel_5 tan

isvarname fred

ans = logical
1

isvarname book_1

ans = logical
1

isvarname Second_Place

ans = logical
1

isvarname No_1

ans = logical
1

isvarname vel_5

ans = logical
1

isvarname tan %although tan is a function name it can be used as a variable


name

ans = logical
1

isvarname fred! %! is not an allowed character

ans = logical
0
isvarname book-1 % - is not an allowed character

ans = logical
0

isvarname 2ndplace % variable names must start with a letter

ans = logical
0

isvarname #1 % # is not an allowed character

ans = logical
0

isvarname vel.5 % . is not an allowed character

ans = logical
0

isvarname while % while is a reserved name

ans = logical
0

which tan % tan is a function name

built-in (C:\Program Files\MATLAB\R2022a\toolbox\matlab\elfun\@double\tan) % double method

which while % while is also a function name, but is reserved

built-in (C:\Program Files\MATLAB\R2022a\toolbox\matlab\lang\while)

You can reassign a function name as a variable name

For example:

sin = 3

sin = 3

The which function now tells us sin is a variable

which sin

sin is a variable.

Use the clear function to return sin to its function definition


clear sin
which sin

built-in (C:\Progr
ram Files\MAT
TLAB\R2022a\
\toolbox\matl
lab\elfun\@d
double\sin) % double me
ethod

Scalarr Operatio
ons and Order off Operatio
ons
Proble
em 2.3
Create MATLAB® cod de to perform
m the followin
ng calculation
ns. Check yo our code by e
entering it intto
MATLAB® ® and perforrming the calculations on your scientiffic calculator.

5^2

ans = 25

(5 + 3)/(5*6)

ans = 0.2666
67

sqrt(4
4 + 6^3) % or...

ans = 14.83
32

(4 + 6^3)^(1/2)
6

ans = 14.83
32

9+6/12
2 + 7*5^(3+
+2)

ans = 2188
84

1 + 5*3/6^2 + 2^
^(2-4) *1/5
5.5

ans = 1.462
21

Proble
em 2.4
(a) The area of a circle is . Defiine r as 5, the
en find the a rea of a circle, using MAT
TLAB®.

r = 5

r = 5

area = pi*r^2

area = 78.
.54

(b) The surface area of a sphere is . Find the su


urface area off a sphere with a radius o
of 10
ft.

r = 10
0

r = 10

surface_area = 4*pi*r^2
4

surface_area = 1256.6

(c) The volume of a sphere is . Find the volume


v of a ssphere with a radius of 2 ft.

r = 2

r = 2

volume
e = 4/3*pi*
*r^3

volume = 33.51
3

Proble
em 2.5
(a) The area of a squaare is the edg
ge length squ
uared ( ). Defin
ne the edge le
ength as 5, then
find the area
a of a squa
are, using MAATLAB®.

edge = 5

edge = 5

area = edge^2

area = 25

(b) The surface area of a cube is 6 times


s the edge len
ngth squared
d

( ). Find the surface


e area of a cube with edg
ge length 10.

edge = 10

edge = 10

surface_area = 6*edge^2
6

surface_area = 600

(c) The volume of a cube is the edge len


ngth cubed ( ). Find the volu
ume of a cub
be
with edge
e length 12.

edge = 12

edge = 12

volume
e = edge^3

volume = 1728
1

Proble
em 2.6

Consider the barbell shown


s in Figu
ure P2.6.
(a) Find the volume of the figure, if the radius of each sphere is 10 cm, the length of the bar
connecting them is 15 cm, and the diameter of the bar is 1 cm. Assume that the bar is a simple
cylinder.

r=10; %cm
length=15; %cm
d=1; % cm
% Find the volume of each sphere
volume_sphere=4/3*pi*r^3;
% Find the volume of the bar
volume_bar=pi*(d/2)^2*length;
% Combine the components to get the total volume
total_volume=2*volume_sphere +volume_bar

total_volume = 8389.4

b)Surface Area
(b) Find the surface area of the figure.

Find the surface area of each sphere

sa_sphere=4*pi*r^2;
% Find the surface area of the bar
sa_bar=pi*d*length;
% Combine the components to get the total surface area
total_sa=2*sa_sphere + sa_bar

total_sa = 2560.4

Problem 2.7
The ideal gas law was introduced in Example 2.1. It describes the relationship between pressure (P),
temperature (T), volume (V), and the number of moles of gas (n).

PV = nRT

The additional symbol, R, represents the ideal gas constant. The ideal gas law is a good
approximation of the behavior of gases when the pressure is low and the temperature is high. (What
constitutes low pressure and high temperature varies with different gases.) In 1873, Johannes
Diderik van der Waals, (see Figure P2.7) proposed a modified version of the ideal gas law that better
models the behavior of real gases over a wider range of temperature and pressure.
In this equation the ad
dditional varia
ables a and b represent vvalues chara
acteristic of in
ndividual gasses.

Use both the ideal gass law and va


an der Waals’ equation to calculate the
e temperaturre of water va
apor
(steam), given
g the following data.

P=220; % Pressuure in bars


s
n=2; % Moles
V=1; % Volume in
n liters
a=5.536; % const
tant in L^2
2 bar/mol^2
2
b=0.03049; % con
nstant in L/mol
L
R=0.08
8314472; % ideal gas constant L bar/(K mo
ole)

Find the temperature


e using the ideal gas law

T_ideal=P*V/(n*R
R) % kelvi
ins

T_ideal = 1323

Find the temperature


t using Van de
er Waal's equation

T_VW=(P + n^2*a/
/V^2)*(V - n*b)/(n*R)
)

T_VW = 1367
7.4
Proble
em 2.8

(a) The volume of a cy


ylinder is . Defin
ne r as 3 and
d h as the arrray

h = [1
1, 5, 12]

Find th
he volume off the cylinderrs (see Figure
e P2.8a).

r=3;
h=[1,5,12];
volume
e = pi*r^2.*h

volume = 1×3
28.274 141.37 339.29
3

Note thatt we need to use the .* op


perator becau
use h is an a
array

(b) The area


a of a trian
ngle is 1/2 the he base of th e triangle, tim
e length of th mes the height of the trian
ngle.
Define the base as the array

b = [2, 4, 6]

and the height


h h as 12
2, and find th
he area of the
e triangles

b=[ 2, 4, 6];
h=12;
area=1
1/2*b.*h

area = 1×3
12 24 36

Although you don't ha ave to use thee .* operator for both mul tiplications a
and the ./ for the division iit
won't hurrt if you do an
nd is good prractice.

area=1
1./2.*b.*h

area = 1×3
12 24 36
(c) The volume of anyy right prism is the area of
o the base off the prism, tiimes the verttical dimension of
the prism
m. The base of
o the prism can
c be any sh hape—for exxample, a circcle, a rectang gle, or a trian
ngle.
Find the volume
v of the
e prisms creaated from thee triangles off part (b). Asssume that the
e vertical
dimension of these prrisms is 6

h=6;
volume
e=h.*area

volume = 1×3
72 144 216

Proble
em 2.9
The respo onse of circu
uits containing
g resistors, in
nductors andd capacitors d
depends upo on the relativve
values of the resistors s and the wayy they are coonnected. Ann important in
ntermediate qquantity used d in
describing the response of such circuits is s. Depending
D onn the values o
of R, L, and C, the valuess of s
will be eitther both real values, a pa
air of compleex values, or a duplicated
d value.

The equa
ation that iden
ntifies the res
sponse of a particular
p serries circuit (F
Figure P2.9) is
(a) Determine the values of s for a resistance of 800 Ω.

R=800 %ohms

R = 800

L=100e-3 %H

L = 0.1

C=1e-6 %F

C = 1e-06

s(1)= -R/(2*L)+sqrt((R/(2*L))^2 - 1/(L*C))

s = -1550.5

s(2)= -R/(2*L)-sqrt((R/(2*L))^2 - 1/(L*C))

s = 1×2
-1550.5 -6449.5

(b) Create a vector of values for R ranging from 100 to 1000 Ω and evaluate s. Refine your values of
R until you find the approximate size of resistor that yields a pure real value of s. Describe the effect
on s as R increases in value.

Hint:

1 μF = 1e-6 F

1 mH = 1e-3 H

R=100:100:1000; % array of R values


s_plus = -R./(2*L)+sqrt((R./(2*L)).^2 - 1/(L*C))

s_plus = 1×10 complex


-500 + 3122.5i -1000 + 3000i ⋯

s_minus = -R./(2*L)-sqrt((R./(2*L)).^2 - 1/(L*C))

s_minus = 1×10 complex


-500 - 3122.5i -1000 - 3000i ⋯

results = [R',s_plus',s_minus']

table_values = 10×3 complex


100 + 0i -500 - 3122.5i ⋯
200 + 0i -1000 - 3000i
300 + 0i -1500 - 2783.9i
400 + 0i -2000 - 2449.5i
500 + 0i -2500 - 1936.5i
600 + 0i -3000 - 1000
0i
700 + 0i -2000 + 0
0i
800 + 0i -1550.5
- + 0
0i
900 + 0i -1298.4
- + 0
0i
1000 + 0i -1127 + 0
0i

% Repe
eat for R between
b 600
0 and 700
R=600:10:700;
s_plus = -R./(2*
*L)+sqrt((R
R./(2*L)).^
^2 - 1/(L*C
C))

s_plus = 1×11 comp


plex
-3000 + 1000i -3050 + 6i ⋯
835.16

s_minus = -R./(2
2*L)-sqrt((
(R./(2*L)).
.^2 - 1/(L*
*C))

s_minus = 1×11 com


mplex
-3000 - 1000i -3050 - 6i ⋯
835.16

results = [R',s_
_plus',s_mi
inus']

table_
_values = 11×
×3 complex
600 + 0i -3000 - 0i ⋯
1000
610 + 0i -3050 - 835.16
6i
620 + 0i -3100 - 624.5
5i
630 + 0i -3150 - 278.39
9i
640 + 0i -2710.1
- + 0
0i
650 + 0i -2500 + 0
0i
660 + 0i -2356.6
- + 0
0i
670 + 0i -2244.3
- + 0
0i
680 + 0i -2151 + 0
0i
690 + 0i -2070.7
- + 0
0i

Proble
em 2.10
The equa
ation that iden
ntifies the res
sponse parameter, s, of tthe parallel ccircuit shown in Figure P2
2.10

is
(a) Determine th ance of 200 Ω.
he values of s for a resista

R=200;
C=1e-6
6;
L=0.64
4

L = 0.64

s_plus=-1/(2*R*C
C) + sqrt((
(1/(2*R*C))
)^2 - 1/(L*
*C))

s_plus = -33
34.94

s_minus=-1/(2*R*
*C) - sqrt(
((1/(2*R*C)
))^2 - 1/(L
L*C))

s_minus = -4
4665.1

(b) Create a vec ctor of values


s for R rangin
ng from 100 tto 1000 Ω an nd evaluate ss. Refine you ur
values of R until you find
f the size of
o resistor that yields a pu
ure real value of s. Descrribe the effecct on
s as R de
ecreases.

R=100:100:1000;
s_plus=-1./(2*R*
*C) + sqrt(
((1./(2*R*C
C)).^2 - 1/
/(L*C));
s_minus=-1./(2*R
R*C) - sqrt
t((1./(2*R*
*C)).^2 - 1
1/(L*C));
results = [R',s_
_plus',s_mi
inus']

results = 10×3 com


mplex
100 + 0i -158.77
- + 0i ⋯
0
200 + 0i -334.94
- + 0
0i
300 + 0i -564.27
- + 0
0i
400 + 0i -1250 + 0
0i
500 + 0i -1000 - 750
0i
600 + 0i -833.33
- - 931.69
9i
700 + 0i -714.29
- - 1025.8
8i
800 + 0i -625 - 1082.5
5i
900 + 0i -555.56
- - 1119.8
8i
1000 + 0i -500 - 1145.6
6i

Refine the R values

R=400:10:500;
s_plus=-1./(2*R*
*C) + sqrt(
((1./(2*R*C
C)).^2 - 1/
/(L*C));
s_minus=-1./(2*R
R*C) - sqrt
t((1./(2*R*
*C)).^2 - 1
1/(L*C));
results = [R',s_
_plus',s_mi
inus']

results = 11×3 com


mplex
400 + 0i -1250 + 0i ⋯
0
410 + 0i -1219.5
- - 274.39
9i
420 + 0i -1190.5
- - 381.14
4i
430 + 0i -1162.8
- - 458.71
1i
440 + 0i -1136.4
- - 520.75
5i
450 + 0i -1111.1
- - 572.65
5i
460 + 0i -1087 - 617.27
7i
470 + 0i -1063.8
- - 656.33
3i
480 + 0i -1041.7
- - 690.96
6i
490 + 0i -1020.4
- - 721.99
9i

Proble
em 2.11
Burning one
o gallon of gasoline in your
y car prod
duces 19.4 p ounds of CO
O2. Calculate e the amount of
CO2 emittted during a year for the following vehicles, assum
ming they all travel 12,000 miles per yyear.
The reported fuel-effic
ciency numbeers were extracted from tthe U.S. Deppartment of EEnergy website,
www.fueleconomy.gov, and reflect the combined city and h highway estim
mates.

Create an
n array of mp
pg values

mpg=[1
142, 36, 52
2, 35, 41,1
113];

Calculate
e the emissions

Mass_C
CO2=(12000./mpg*19.4)
)'

Mass_CO2 = 6×1
1639.4
6466.7
4476.9
6651.4
5678
2060.2

Notice tha
at the result was
w transpos
sed so that itt is easier to read.

Proble
em 2.12
(a) Creatte an evenly spaced vector of values from 1 to 20 in incrementts of 1.

a=1:20
0

a = 1×20
1 2 3 4 5 6 7 8 9 10 11 12
2
13 ⋯

(b) Create a vec


ctor of values
s from zero to
o in incre
ements of .

b=0:pi/10:2*pi

b = 1×21
0 0.31416 0.
.62832 0.94248 1.2566
1.5708 ⋯

(c) Creeate a vector containing 15 values, ev


venly spaced between 4 a
and 20. (Hintt: Use the
e command. If you can’t remember
linspace r the syntax, typ
pe help linspace.)

c=linspace(4,20,15)

c = 1×15
4 5.1429 6.2857
6 7.4286 8.5714
9.7143 ⋯

(d) Create a vec


ctor containin
ng 10 values,, spaced loga
arithmically b
between 10 a
and 1000. (H
Hint:
Use the logspace
l command.)

d=logspace(1,3,4
4)

d = 1×4
10 46.416 215.44
2 1000

Proble
em 2.13
(a) Creatte a table of conversions from feet to meters. Starrt the feet column at 0, inccrement it byy 1,
and end iti at 10 feet. (Look
( up the conversion factor
f in a te
extbook or onnline.)

feet=0
0:1:10;
meters=feet./3.2
28;
[feet',meters']

ans = 11×2
0 0
1 0.30488
2 0.60976
3 0.91463
4 1.2195
5 1.5244
6 1.8293
7 2.1341
8 2.439
9 2.7439

(b) Creeate a table of


o conversion ns from radia
ans to degree
es. Start the rradians colummn at 0 and
incremen
nt by ra
adians, up to radians. (L Look up the conversion fa
factor in a texxtbook or online.)

radians=0:0.1*pi
i:pi;
degree
es=radians*
*180/pi;
[radians',degree
es'] % Or we could also
a give t
this array a name

ans = 11×2
0 0
0.31416 18
0.62832 36
0.94248 54
1.2566 72
1.5708 90
1.885 108
2.1991 126
2.5133 144
2.8274 162

conversion_table=[radians',degrees']

conversion_table = 11×2
0 0
0.31416 18
0.62832 36
0.94248 54
1.2566 72
1.5708 90
1.885 108
2.1991 126
2.5133 144
2.8274 162

(c) Create a table of conversions from mi/h to ft/s. Start the mi/h column at 0 and end it at 100
mi/h. Print 15 values in your table. (Look up the conversion factor in a textbook or online.)

mph=linspace(0,100,15);
ft_per_sec=mph*5280/3600;
vel_conversion=[mph',ft_per_sec']

vel_conversion = 15×2
0 0
7.1429 10.476
14.286 20.952
21.429 31.429
28.571 41.905
35.714 52.381
42.857 62.857
50 73.333
57.143 83.81
64.286 94.286

(d) The acidity of solutions is generally measured in terms of pH. The pH of a solution is defined
as –log10 of the concentration of hydronium ions. Create a table of conversions from concentration
of hydronium ion to pH, spaced logarithmically from .001 to .1 mol/liter with 10 values. Assuming that
you have named the concentration of hydronium ions H_conc, the syntax for calculating the negative
of the logarithm of the concentration (and thus the pH) is

pH = –log10(H_conc)
%d
H_conc=logspace(-3,-1,10);
pH=-lo
og10(H_conc
c);
pH_tab
ble=[H_conc
c',pH']

pH_table = 10×2
0.001 3
0.0016681 2.7778
0.0027826 2.5556
0.0046416 2.3333
0.0077426 2.1111
0.012915 1.8889
0.021544 1.6667
0.035938 1.4444
0.059948 1.2222
0.1 1

Proble
em 2.14
The gene
eral equation for the distance that a fre
eely falling bo
ody has traveled (neglecting air frictio
on) is

sume that g =9.8


Ass . Gen e of time verssus distance traveled for values of tim
nerate a table me

from 0 to 100 seconds


s. Choose a suitable incre
ement for yo or. (Hint: Be careful to use the
our time vecto
correct op
perators; is an array op
peration!)

g=9.8;
t=0:10
0:100;
d=1/2*g*t.^2;
results=[t',d']

table_
_values = 11×
×2
0 0
10 490
20 1960
30 4410
40 7840
50 12250
60 17640
70 24010
80 31360
90 39690

Proble
em 2.15
In direct current
c applic
cations, electtrical power is
i calculated using Joule’’s law as

P = VI
where P is pow
wer in watts

V is the potential
p diffe
erence, meas
sured in voltss

I is the ellectrical curre ed in ampere s


ent, measure

Jou
ule’s law can
n be combine
ed with Ohm’s
s law

V = IR

to give
g

where R is resis
stance meas
sured in ohms
s.

The resista nductor of uniform cross ssection (a wirre or rod for e


ance of a con example) is

where

ρ is the ele
ectrical resistiivity measure
ed in ohm-me
eters

l is the leng
gth of the wirre

A is the cro
oss-sectional area of the wire
w

Thiis results in the


t equation for power

Ele
ectrical resisttivity is a matterial property
y that has be
een tabulated
d for many m
materials. Forr
example

rho = [1.59e-8; 1.68e-8; 2.44e-8;


2 2.
.82e-8; 1.0
0e-7]; % oh
hm-meters

Calculate
e the power th
hat is dissipa
ated through a wire with t he following dimensions for each of th
he
materials listed.

diameter 0.001 m
length 2.00 m

Assume the wire carries a current of 120 amps.

d=0.001; % meters
area = pi*d^2/4; % meters^2
length = 2; % meters

I=120; %amps
P=I.^2.*rho.*length./area

P = 5×1
583.04
616.04
894.73
1034.1
3666.9

Problem 2.16
Repeat the previous problem for 10 wire lengths, from 1 m to 1 km. Use logarithmic spacing.

Realizing that 1km is 1000 meters, define a length array

length=logspace(1,3,10) % meters

length = 1×10
10 16.681 27.826 46.416 77.426
129.15 ⋯

P_silver=I.^2.*rho(1).*length./area

P_silver = 1×10
2915.2 4862.9 8111.7 13531 22571
37651 ⋯

P_copper=I.^2.*rho(2).*length./area

P_copper = 1×10
3080.2 5138.1 8570.9 14297 23849
39783 ⋯

P_gold = I.^2.*rho(3).*length./area

P_gold = 1×10
4473.7 7462.5 12448 20765 34638
57779 ⋯

P_al = I.^2.*rho(4).*length./area

P_al = 1×10
5170.4 8624.7 14387 23999 40032
66778 ⋯

P_Fe = I.^2.*rho
o(5).*lengt
th./area

P_Fe = 1×10
18335 30584 51017 85102 1.4196e+05
2.368e+05 ⋯

In chapte
er 4 we'll learn
n how to do these
t calcula
ations in a sin
ngle step usiing the meshg
grid function
n.

Proble
em 2.17
Newton’s
s law of unive
ersal gravitatiion tells us th
hat the force exerted by o
one particle o
on another is

where the
e universal gravitational constant
c G is
s found experrimentally to be

G = 6.673
3× N m2/kg2
m

Thee mass of ea
ach particle is
s m1and m2,, respectivelyy, and r is thee distance be
etween the twwo
particles. Use Newtonn’s law of universal gravita
ation to find tthe force exe
erted by the e
earth on the
moon, as ssuming that

the mass of
o the earth is
s approximate
ely 6 × kkg,

the mass of
o the moon is
s approximattely 7.4 × kg, and

the earth and the moon


n are an averrage of 3.9 × m apart.

G=6.67
73e-11; % N m^2/kg^
^2
m_earth=6e24; % kg
m_moon=7.4e22; % kg
r=3.9e
e8; % meter
rs
F=G*m_
_earth*m_mo
oon/r^2

F = 1.9479e+20

Note thatt the operatorrs *, / and ^ were


w used beecause all of the values a
are scalars. Dot operatorrs
could alsoo have been used and wo ould give the
e same resultt.

Proble
em 2.18
We knoww that the eartth and the moon are not always
a the s ame distance apart. Base ed on the
equation in the previo
ous problem, find the force
e the moon eexerts on the
e earth for 100 distances
between 3.8 × m and 4.0 × m. Be careeful when youu do the divission to use th
he correct
operator.
r=linspace(3.8e8
8,4.0e8,10)
); % meters
s
F=G*m_
_earth*m_mo
oon./r.^2; % Notice the .^ and
d ./ operat
tors
[r',F']

ans = 10×2
3.8e+08 2.0518e+20
3.8222e+08 2.028e+20
3.8444e+08 2.0046e+20
3.8667e+08 1.9817e+20
3.8889e+08 1.9591e+20
3.9111e+08 1.9369e+20
3.9333e+08 1.9151e+20
3.9556e+08 1.8936e+20
3.9778e+08 1.8725e+20
4e+08 1.8518e+20

Proble
em 2.19
Recall fro
om Problem 2.7
2 that the id
deal gas law is:

PV =nRT
T

and an der Waals modification


d that the Va n of the ideal gas law is

Using the data from Problem


m 2.7, find the value of te
emperature (T
T), for

(a) 10 values off pressure fro


om 0 bar to 400
4 bar for vo
olume of 1 L

a)
P=linspace(0,400
0,10); % pr
ressure
n=2; % moles
V=1; % volume
a=5.536; % const
tant
b=0.03049; % con
nstant
R=0.08
8314472; % ideal gas constant

Find the temperature


t using the ide
eal gas law

T_ideal=P*V/(n*R
R)

T_ideal = 1×10
0 267.27 534.54
5 801.81 1069.1
1336.4 ⋯

Find the temperature


t using Van de
er Waal's equation
T_VW=(P+n^2*a/V^2)*(V-n*b)/(n*R)

T_VW = 1×10
125.04 376.02 626.99 877.97 1128.9
1379.9 ⋯

It would be easier to compare the calculated values if they are listed in a table

[T_ideal',T_VW']

ans = 10×2
0 125.04
267.27 376.02
534.54 626.99
801.81 877.97
1069.1 1128.9
1336.4 1379.9
1603.6 1630.9
1870.9 1881.9
2138.2 2132.8
2405.4 2383.8

(b) 10 values of volume from 0.1 L to 10 L for a pressure of 220


bar
V=linspace(0.1,10,10);
P=220;
% Find the temperature using the ideal gas law
T_ideal=P*V/(n*R)

T_ideal = 1×10
132.3 1587.6 3042.9 4498.2 5953.5
7408.8 ⋯

% Find the temperature using Van der Waal's equation


T_VW=(P+n^2*a./V.^2).*(V-n*b)/(n*R)

T_VW = 1×10
571.23 1612.2 3018.6 4456 5902
7351.6 ⋯

% It would be easier to compare the calculated values if they are listed in


% a table
[T_ideal',T_VW']

ans = 10×2
132.3 571.23
1587.6 1612.2
3042.9 3018.6
4498.2 4456
5953.5 5902
7408.8 7351.6
8864.1 8803.1
10319 10256
11775 11709
13230 13163

c
You might interpret this probem to mean that the 10 values of pressure

and ten values of volume should be used in the same calculation

V=linspace(0.1,10,10);
P=linspace(0,400,10);
% Find the temperature using the ideal gas law
T_ideal=P.*V/(n*R)

T_ideal = 1×10
0 320.73 1229.4 2726.2 4810.9
7483.6 ⋯

% Find the temperature using Van der Waal's equation


T_VW=(P+n^2*a./V.^2).*(V-n*b)/(n*R)

T_VW = 1×10
519.61 409.76 1253.2 2715.7 4774.9
7425.6 ⋯

% It would be easier to compare the calculated values if they are listed in


% a table
[T_ideal',T_VW']

ans = 10×2
0 519.61
320.73 409.76
1229.4 1253.2
2726.2 2715.7
4810.9 4774.9
7483.6 7425.6
10744 10666
14593 14496
19030 18914
24054 23921

Number Display
Problem 2.20
Create a array a equal to [−1/3, 0, 1/3, 2/3], and use each of the built-in format options to display the
results:

format short (which is the default)

format long
format bank

format short e

format long e

format short eng

format long eng

format short g (shortg)

format long g (longg)

format +

format rat

a=[-1/3,0,1/3,2/3] % displays as format shortg automatically since shortg was


specified at the beginning of this file

a = 1×4
-0.33333 0 0.33333 0.66667

format long
a

a = 1×4
-0.333333333333333 0 0.333333333333333 ⋯

format bank
a

a = 1×4
-0.33 0 0.33 0.67

format short e
a

a = 1×4
-3.3333e-01 0 3.3333e-01 6.6667e-01

format long e
a

a = 1×4
-3.333333333333333e-01 0
3.333333333333333e-01 ⋯

format short eng


a
a = 1×4
-333.3333e-003 0.0000e+000 333.3333e-003 666.6667e-003

format long eng


a

a = 1×4
-333.333333333333e-003 0.00000000000000e+000 333.333333333333e-003

format short
a

a = 1×4
-0.3333 0 0.3333 0.6667

format long g
a

a = 1×4
-0.333333333333333 0
0.333333333333333 ⋯

format +
a

a = 1×4
- ++

format rat
a

a = -1/3 0 1/3 2/3

format shortg % This is my favorite


a

a = 1×4
-0.33333 0 0.33333 0.66667

Saving Your Work in Files


Problem 2.21
Create an array called D_to_R composed of two columns, one representing degrees, and the other
representing the corresponding value in radians. Any value set will do for this exercise.

• Save the array to a file called degrees.dat.

• Once the file is saved, clear your workspace then load the data from the file back into MATLAB.

D=0:10:180;
R=D*pi/180;
D_to_R=[D',R']

D_to_R = 19×2
0 0
10 0.17453
20 0.34907
30 0.5236
40 0.69813
50 0.87266
60 1.0472
70 1.2217
80 1.3963
90 1.5708

save degrees.dat -ascii D_to_R

Check your current directory to confirm that the file was saved

clear
load degrees.dat
degrees

degrees = 19×2
0 0
10 0.17453
20 0.34907
30 0.5236
40 0.69813
50 0.87266
60 1.0472
70 1.2217
80 1.3963
90 1.5708

You might also like