• Home
  • Textbooks
  • Applied Numerical Methods With MATLAB: for Engineers & Scientists
  • MATLAB Fundamentals

Applied Numerical Methods With MATLAB: for Engineers & Scientists

Steven Chapra

Chapter 2

MATLAB Fundamentals - all with Video Answers

Educators


Chapter Questions

Problem 1

Use the linspace function to create vectors identical to the following created with colon notation:
(a) $\mathrm{t}=4: 6: 35$
(b) $x=-4: 2$

Check back soon!

Problem 2

Use colon notation to create vectors identical to the following created with the linspace function:
(a) $\mathrm{v}=1$ inspace $(-2,1.5,8)$
(b) $r=1$ inspace $(8,4.5,8)$

Check back soon!

Problem 3

The command linspace ( $\mathrm{a}, \mathrm{b}, \mathrm{n}$ ) generates a row vector of n equally spaced points between a and b . Use colon notation to write an alternative one-line command to generate the same vector. Test your formulation for $\mathrm{a}=-3$, $\mathrm{b}=5, \mathrm{n}=6$.

Check back soon!

Problem 4

The following matrix is entered in MATLAB:
$\gg A=\left[\begin{array}{ll}3 & 2 \\ 1 ; 0: 0.5: 1 ; 1 \text { inspace }(6,8,3)\end{array}\right]$
(a) Write out the resulting matrix.
(b) Use colon notation to write a single-line MATLAB command to multiply the second row by the third column and assign the result to the variable $c$.

Check back soon!

Problem 5

The following equation can be used to compute values of $y$ as a function of $x$ :

$$
y=b e^{-a x} \sin (b x)\left(0.012 x^4-0.15 x^3+0.075 x^2+2.5 x\right)
$$

where $a$ and $b$ are parameters. Write the equation for implementation with MATLAB, where $a=2, b=5$, and $x$ is a vector holding values from 0 to $\pi / 2$ in increments of $\Delta x=\pi / 40$. Employ the minimum number of periods (i.e., dot notation) so that your formulation yields a vector for $y$. In addition, compute the vector $z=y^2$ where each element holds the square of each element of $y$. Combine $x, y$, and $z$ into a matrix $w$, where each column holds one of the variables, and display $w$ using the short g format. In addition, generate a labeled plot of $y$ and $z$ versus $x$. Include a legend on the plot (use help to understand how to do this). For $y$, use a 1.5 -point, dashdotted red line with 14 -point, rededged, white-faced pentagram-shaped markers. For $z$, use a standard-sized (i.e., default) solid blue line with standardsized, blue-edged, green-faced square markers.

Check back soon!

Problem 6

A simple electric circuit consisting of a resistor, a capacitor, and an inductor is depicted in Fig. P2.6. The charge on the capacitor $q(t)$ as a function of time can be computed as

$$
q(t)=q_0 e^{-R t /(2 L)} \cos \left[\sqrt{\frac{1}{L C}-\left(\frac{R}{2 L}\right)^2 t}\right]
$$
where $t=$ time, $q_0=$ the initial charge, $R=$ the resistance, $L=$ inductance, and $C=$ capacitance. Use MATLAB to generate a plot of this function from $t=0$ to 0.8 , given that $q_0=10, R=60, L=9$, and $C=0.00005$.

Check back soon!

Problem 7

The standard normal probability density function is a bell-shaped curve that can be represented as

$$
f(z)=\frac{1}{\sqrt{2 \pi}} e^{-z^2 / 2}
$$

Use MATLAB to generate a plot of this function from $z=-5$ to 5 . Label the ordinate as frequency and the abscissa as $z$.

Check back soon!

Problem 8

If a force $F(\mathrm{~N})$ is applied to compress a spring, its displacement $x(\mathrm{~m})$ can often be modeled by Hooke's law:

$$
F=k x
$$

where $k=$ the spring constant ( $\mathrm{N} / \mathrm{m}$ ). The potential energy stored in the spring $U(\mathrm{~J})$ can then be computed as

$$
U=\frac{1}{2} k x^2
$$

Five springs are tested and the following data compiled:
$$
\begin{array}{cccccc}
\hline \boldsymbol{F}, \mathbf{N} & 14 & 18 & 8 & 9 & 13 \\
\boldsymbol{x}, \mathbf{m} & 0.013 & 0.020 & 0.009 & 0.010 & 0.012 \\
\hline
\end{array}
$$
Use MATLAB to store $F$ and $x$ as vectors and then compute vectors of the spring constants and the potential energies. Use the max function to determine the maximum potential energy.

Check back soon!

Problem 9

The density of freshwater can be computed as a function of temperature with the following cubic equation:

$$
\begin{aligned}
\rho= & 5.5289 \times 10^{-8} T_C^3-8.5016 \times 10^{-6} T_C^2 \\
& +6.5622 \times 10^{-5} T_C+0.99987
\end{aligned}
$$
where $\rho=$ density $\left(\mathrm{g} / \mathrm{cm}^3\right)$ and $T_C=$ temperature $\left({ }^{\circ} \mathrm{C}\right)$. Use MATLAB to generate a vector of temperatures ranging from $32^{\circ} \mathrm{F}$ to $93.2^{\circ} \mathrm{F}$ using increments of $3.6^{\circ} \mathrm{F}$. Convert this vector to degrees Celsius and then compute a vector of densities based on the cubic formula. Create a plot of $\rho$ versus $T_C$. Recall that $T_C=5 / 9\left(T_F-32\right)$.

Check back soon!

Problem 10

Manning's equation can be used to compute the velocity of water in a rectangular open channel:

$$
U=\frac{\sqrt{S}}{n}\left(\frac{B H}{B+2 H}\right)^{2 / 3}
$$

where $U=$ velocity $(\mathrm{m} / \mathrm{s}), S=$ channel slope, $n=$ roughness coefficient, $B=$ width (m), and $H=$ depth (m). The following data are available for five channels:
$$
\begin{array}{cccc}
\hline \boldsymbol{n} & \boldsymbol{S} & \boldsymbol{B} & \boldsymbol{H} \\
\hline 0.035 & 0.0001 & 10 & 2 \\
0.020 & 0.0002 & 8 & 1 \\
0.015 & 0.0010 & 20 & 1.5 \\
0.030 & 0.0007 & 24 & 3 \\
0.022 & 0.0003 & 15 & 2.5 \\
\hline
\end{array}
$$
Store these values in a matrix where each row represents one of the channels and each column represents one of the parameters. Write a single-line MATLAB statement to compute a column vector containing the velocities based on the values in the parameter matrix.

Check back soon!

Problem 11

2.11 It is general practice in engineering and science that equations be plotted as lines and discrete data as symbols. Here are some data for concentration (c) versus time $(t)$ for the photodegradation of aqueous bromine:
$$
\begin{array}{lrrrrrr}
\hline \boldsymbol{t}, \boldsymbol{m i n} & 10 & 20 & 30 & 40 & 50 & 60 \\
\boldsymbol{c}, \mathbf{p p m} & 3.4 & 2.6 & 1.6 & 1.3 & 1.0 & 0.5 \\
\hline
\end{array}
$$
These data can be described by the following function:

$$
c=4.84 e^{-0.034 t}
$$

Use MATLAB to create a plot displaying both the data (using diamond-shaped, filled-red symbols) and the function (using a green, dashed line). Plot the function for $t=0$ to 70 min .

Check back soon!

Problem 12

The semilogy function operates in an identical fashion to the plot function except that a logarithmic (base-10) scale is used for the $y$ axis. Use this function to plot the data and function as described in Prob. 2.11. Explain the results.

Check back soon!

Problem 13

Here are some wind tunnel data for force (F) versus
velocity (v):

$$
\begin{array}{lcccccccc}
\hline \boldsymbol{v}, \mathbf{m} / \mathbf{s} & 10 & 20 & 30 & 40 & 50 & 60 & 70 & 80 \\
\boldsymbol{F}, \mathbf{N} & 25 & 70 & 380 & 550 & 610 & 1220 & 830 & 1450 \\
\hline
\end{array}
$$
These data can be described by the following function:

$$
F=0.2741 v^{1.9842}
$$

Use MATLAB to create a plot displaying both the data (using circular magenta symbols) and the function (using a black dash-dotted line). Plot the function for $v=0$ to $100 \mathrm{~m} / \mathrm{s}$ and label the plot's axes.

Check back soon!

Problem 14

The loglog function operates in an identical fashion to the plot function except that logarithmic scales are used for both the $x$ and $y$ axes. Use this function to plot the data and function as described in Prob. 2.13. Explain the results.

Check back soon!

Problem 15

The Maclaurin series expansion for the cosine is

$$
\cos x=1-\frac{x^2}{2!}+\frac{x^4}{4!}-\frac{x^6}{6!}+\frac{x^8}{8!}-\cdots
$$

Use MATLAB to create a plot of the sine (solid line) along with a plot of the series expansion (black dashed line) up to and including the term $x^8 / 8$ !. Use the built-in function factorial in computing the series expansion. Make the range of the abscissa from $x=0$ to $3 \pi / 2$.

Check back soon!

Problem 16

You contact the jumpers used to generate the data in Table 2.1 and measure their frontal areas. The resulting values, which are ordered in the same sequence as the corresponding values in Table 2.1, are
$$
\begin{array}{llllllll}
\hline \boldsymbol{A}, \mathbf{m}^{\mathbf{2}} & 0.455 & 0.402 & 0.452 & 0.486 & 0.531 & 0.475 & 0.487 \\
\hline
\end{array}
$$
(a) If the air density is $\rho=1.223 \mathrm{~kg} / \mathrm{m}^3$, use MATLAB to compute values of the dimensionless drag coefficient $C_{D^*}$
(b) Determine the average, minimum and maximum of the resulting values.
(c) Develop a stacked plot of $A$ versus $m$ (upper) and $C_D$ versus $m$ (lower). Include descriptive axis labels and titles on the plots.

Check back soon!

Problem 17

The following parametric equations generate a conical helix.

$$
\begin{aligned}
& x=t \cos (6 t) \\
& y=t \sin (6 t) \\
& z=t
\end{aligned}
$$
Compute values of $x, y$, and $z$ for $t=0$ to $6 \pi$ with $\Delta t=\pi / 64$. Use subplot to generate a two-dimensional line plot (red solid line) of $(x, y)$ in the top pane and a threedimensional line plot (cyan solid line) of $(x, y, z)$ in the bottom pane. Label the axes for both plots.

Check back soon!

Problem 18

Exactly what will be displayed after the following MATLAB commands are typed?

$$
\text { (a) } \begin{aligned}
& \Rightarrow x=5 ; \\
& >x \\
> & \Rightarrow 3 ; \\
& =8-x
\end{aligned}
$$

$$
\text { (b) } \begin{aligned}
& >q=4: 2: 12 ; \\
& >r=\left[\begin{array}{llll}
7 & 8 & 4 ; & 3 \\
> & -5
\end{array}\right] ; \\
& >\operatorname{sum}(q) \text { * } r(2,3)
\end{aligned}
$$

Check back soon!

Problem 19

The trajectory of an object can be modeled as

$$
y=\left(\tan \theta_0\right) x-\frac{g}{2 v_0^2 \cos ^2 \theta_0} x^2+y_0
$$

where $y=$ height (m), $\theta_0=$ initial angle (radians), $x=$ horizontal distance ( m ), $g=$ gravitational acceleration $\left(=9.81 \mathrm{~m} / \mathrm{s}^2\right), v_0=$ initial velocity $(\mathrm{m} / \mathrm{s})$, and $y_0=$ initial height. Use MATLAB to find the trajectories for $y_0=0$ and $v_0=28 \mathrm{~m} / \mathrm{s}$ for initial angles ranging from 15 to $75^{\circ}$ in increments of $15^{\circ}$. Employ a range of horizontal distances from $x=0$ to 80 m in increments of 5 m . The results should be assembled in an array where the first dimension (rows) corresponds to the distances, and the second dimension (columns) corresponds to the different initial angles. Use this matrix to generate a single plot of the heights versus horizontal distances for each of the initial angles. Employ a legend to distinguish among the different cases, and scale the plot so that the minimum height is zero using the axis command.

Check back soon!

Problem 20

The temperature dependence of chemical reactions can be computed with the Arrhenius equation:

$$
k=A e^{-E /\left(R T_a\right)}
$$

where $k=$ reaction rate $\left(\mathrm{s}^{-1}\right), A=$ the preexponential (or frequency) factor, $E=$ activation energy ( $\mathrm{J} / \mathrm{mol}), R=$ gas constant $[8.314 \mathrm{~J} /($ mole $\cdot \mathrm{K})]$, and $T_a=$ absolute temperature (K). A compound has $E=1 \times 10^5 \mathrm{~J} / \mathrm{mol}$ and $A=7 \times 10^{16}$. Use MATLAB to generate values of reaction rates for temperatures ranging from 253 to 325 K . Use subplot to generate a side-by-side graph of (a) $k$ versus $T_a$ (green line) and (b) $\log _{10} k$ (red line) versus $1 / T_a$. Employ the semilogy function to create (b). Include axis labels and titles for both subplots. Interpret your results.

Check back soon!

Problem 21

Figure P2.21 $a$ shows a uniform beam subject to a linearly increasing distributed load. As depicted in Fig. P2.21b, deflection $y(\mathrm{~m})$ can be computed with

$$
y=\frac{w_0}{120 E I L}\left(-x^5+2 L^2 x^3-L^4 x\right)
$$

where $E=$ the modulus of elasticity and $I=$ the moment of inertia $\left(\mathrm{m}^4\right)$. Employ this equation and calculus to generate MATLAB plots of the following quantities versus distance along the beam:
(a) displacement (y),
(b) slope $[\theta(x)=d y / d x]$,
(c) moment $\left[M(x)=E I d^2 y / d x^2\right]$,
(d) shear $\left[V(x)=E I d^3 y / d x^3\right]$, and
(e) loading $\left[w(x)=-E^2 d^4 y / d x^4\right]$.

Use the following parameters for your computation: $L=600 \mathrm{~cm}, \quad E=50,000 \mathrm{kN} / \mathrm{cm}^2, \quad I=30,000 \mathrm{~cm}^4$, $w_0=2.5 \mathrm{kN} / \mathrm{cm}$, and $\Delta x=10 \mathrm{~cm}$. Employ the subplot function to display all the plots vertically on the same page in the order (a) to (e). Include labels and use consistent MKS units when developing the plots.

Check back soon!

Problem 22

The butterfly curve is given by the following parametric equations:

$$
\begin{aligned}
& x=\sin (t)\left(e^{\cos t}-2 \cos 4 t-\sin ^5 \frac{t}{12}\right) \\
& y=\cos (t)\left(e^{\cos t}-2 \cos 4 t-\sin ^5 \frac{t}{12}\right)
\end{aligned}
$$

Generate values of $x$ and $y$ for values of $t$ from 0 to 100 with $\Delta t=1 / 16$. Construct plots of (a) $x$ and $y$ versus $t$ and (b) $y$ versus $x$. Use subplot to stack these plots vertically and make the plot in (b) square. Include titles and axis labels on both plots and a legend for (a). For (a), employ a dotted line for $y$ in order to distinguish it from $x$.

Check back soon!

Problem 23

The butterfly curve from Prob. 2.22 can also be represented in polar coordinates as

$$
r=e^{\sin \theta}-2 \cos (4 \theta)-\sin ^5\left(\frac{2 \theta-\pi}{24}\right)
$$

Generate values of $r$ for values of $\theta$ from 0 to $8 \pi$ with $\Delta \theta=\pi / 32$. Use the MATLAB function polar to generate the polar plot of the butterfly curve with a dashed red line. Employ the MATLAB Help to understand how to generate the plot.

Check back soon!