• Home
  • Textbooks
  • MATLAB Programming for Engineers
  • Branching Statements and Program Design

MATLAB Programming for Engineers

Stephen J.Chapman

Chapter 3

Branching Statements and Program Design - all with Video Answers

Educators


Chapter Questions

01:01

Problem 1

The tangent function is defined as $\tan \theta=\sin \theta / \cos \theta$. This expression can be evaluated to solve for the tangent as long as the magnitude of $\cos$ $\theta$ is not too near to 0 . (If $\cos \theta$ is 0 , evaluating the equation for $\tan \theta$ will produce the non-numerical value Inf.) Assume that $\theta$ is given in degrees, and write the MATLAB statements to evaluate $\tan \theta$ as long as the magnitude of $\cos \theta$ is greater than or equal to $10^{-20}$. If the magnitude of $\cos \theta$ is less than $10^{-20}$, write out an error message instead.

Steven Clarke
Steven Clarke
Numerade Educator
03:16

Problem 2

The following statements are intended to alert a user to dangerously high oral thermometer readings (values are in degrees Fahrenheit). Are they correct or incorrect? If they are incorrect, explain why and correct them.
If temp $<97.5$
disp ("Temperature below normal') ;
elseif temp > $97.5$
disp ('Temperature normal') ;
elseif temp > $99.5$
disp ('Temperature slight1y high");
elseif temp > $103.0$
disp ('Temperature dangerously high')
end

Kris Bright
Kris Bright
Numerade Educator
02:05

Problem 3

The cost of sending a package by an express delivery service is $\$ 10.00$ for the first two pounds, and $\$ 3.75$ for cach pound or fraction thereof over two pounds. If the package weighs more than 70 pounds, a $\$ 10.00$ excess weight surcharge is added to the cost. No package over 100 pounds will be accepted. Write a program that accepts the weight of a package in pounds and computes the cost of mailing the package. Be sure to handle the case of overweight packages.

James Kiss
James Kiss
Numerade Educator
10:44

Problem 4

In Example 3.3, we wrote a program to evaluate the function $f(x, y)$ for any two user-specified values $x$ and $y$. where the function $f(x, y)$ was defined as follows.
$$
f(x, y)=\left\{\begin{array}{llll}
x+y & x \geq 0 & \text { and } & y \geq 0 \\
x+y^{2} & x \geq 0 & \text { and } & y<0 \\
x^{2}+y & x<0 & \text { and } & y \geq 0 \\
x^{2}+y^{2} & x<0 & \text { and } & y<0
\end{array}\right.
$$
The problem was solved by using a single if construct with 4 code blocks to calculate $f(x, y)$ for all possible combinations of $x$ and $y$. Rewrite program funxy to use nested if constructs, where the outer construct evaluates the value of $x$ and the inner constructs evaluate the value of $y$.

Chris Trentman
Chris Trentman
Numerade Educator
01:10

Problem 5

Write a MATLAB program to evaluate the function
$$
y(x)=\ln \frac{1}{1-x}
$$
for any user-specified value of $x$, where $x$ is a number $<1$ (note that in is the natural logarithm, the logarithm to the base e). Use an if structure to verify that the value passed to the program is legal. If the value of $x$ is legal, calculate $y(x)$. If not, write a suitable error message and quit.

Gregory Higby
Gregory Higby
Numerade Educator
06:54

Problem 6

Write a program that allows a user to enter a string containing a day of the week ("Sunday," "Monday," "Tuesday," etc.) and uses a switch construct to convert the day to its corresponding number, where Sunday is considered the first day of the week and Saturday is considered the last day of the week. Print out the resulting day number. Also, be sure to handle the case of an illegal day name! (Note: Be sure to use the 's' option on function input so that the input is treated as a string.)

Harriet O'Brien
Harriet O'Brien
Numerade Educator
View

Problem 7

The Ideal Gas Law was defined in Example 3.7. Assume that the volume of 1 mole of this gas is $10 \mathrm{~L}$, and plot the pressure of the gas as a function of temperature as the temperature is changed from 250 to 400 kelvins. What sort of plot (linear, semilogx, etc.) is most appropriate for this data?

Carson Merrill
Carson Merrill
Numerade Educator
08:15

Problem 8

The gain $G$ of a certain microwave dish antenna can be expressed as a function of angle by the equation
$$
G(\theta)=|\operatorname{sinc} 4 \theta| \quad \text { for }-\frac{\pi}{2} \leq \theta \leq \frac{\pi}{2}
$$
where $\theta$ is measured in radians from the boresite of the dish, and $\operatorname{sinc} x=$ $\sin x / x$. Plot this gain function on a polar plot, with the title "Antenna Gain vs $\theta^{*}$ in boldface.

Mandeep Mangat
Mandeep Mangat
Numerade Educator
01:22

Problem 9

When a ray of light passes from a region with an index of refraction $n_{1}$ into a region with a different index of refraction $n_{2}$, the light ray is bent (see Figure 3.16). The angle at which the light is bent is given by Snell's law.
$$
n_{1} \sin \theta_{1}=n_{2} \sin \theta_{2}
$$
where $\theta_{1}$ is the angle of incidence of the light in the first region, and $\theta_{2}$ is the angle of incidence of the light in the second region. Using Snell's law, it is possible to predict the angle of incidence of a light ray in Region 2 if the angle of incidence $\theta_{1}$ in Region $I$ and the indices of refraction $n_{1}$ and $n_{2}$ are known. The equation to perform this calculation is
$$
\theta_{2}=\sin ^{-1}\left(\frac{n_{1}}{n_{2}} \sin \theta_{1}\right)
$$
Write a program to calculate the angle of incidence (in degrees) of a light ray in Region 2 given the angle of incidence $\theta_{1}$ in Region $I$ and the indices of refraction $n_{1}$ and $n_{2}$. (Note: If $n_{1}>n_{2}$, then for some angles $\theta_{1}$. Equation $3.8$ will have no real solution because the absolute value of the quantity $\left(\frac{n_{1}}{n_{2}} \sin \theta_{1}\right)$ will be greater than 1.0. When this occurs, all light is reflected back into Region 1, and no light passes into Region 2 at all. Your program must be able to recognize and properly handle this condition.)
The program should also create a plot showing the incident ray, the boundary between the two regions, and the refracted ray on the other side of the boundary.

Test your program by running it for the following two cases: $(a) n_{1}=$ 1. $0, n_{2}=1.7$, and $\theta_{1}=45^{\circ},(b) n_{1}=1.7, n_{2}=1.0$, and $\theta_{1}=45^{\circ}$.

Abhishek Kumar
Abhishek Kumar
Numerade Educator
00:42

Problem 10

Assume that the complex function $f(t)$ is defined by the equation
$$
f(t)=(0.5-0.25 i) t-1.0
$$
Plot the amplitude and phase of function $f$ for $0 \leq t \leq 4$.

Adnan Gill
Adnan Gill
Numerade Educator
02:18

Problem 11

High-Pass Filter. Figure $3.17$ shows a simple high-pass filter consisting of a resistor and a capacitor. The ratio of the output voltage $V_{0}$ to the input voltage $V_{i}$ is given by the equation
$$
\frac{V_{o}}{V_{i}}=\frac{j 2 \pi f R C}{1+\rho \pi f R C}
$$
Assume that $R=16 \mathrm{k} \Omega$ and $C=1 \mu \mathrm{F}$. Calculate and plot the amplitude and phase response of this filter as a function of frequercy.

Ajay Singhal
Ajay Singhal
Numerade Educator
02:44

Problem 12

The Spiral of Archimedes. The spiral of Archimedes is a curve described in polar coordinates by the equation
$$
r=k \theta
$$
where $r$ is the distance of a point from the origin, and $\theta$ is the angle of that point in radians with respect to the origin. Plot the spiral of Archimedes for $0 \leq \theta \leq 6 \pi$ when $k=0.5$. Be sure to label your plot properly.

Faizanullah Kazmi
Faizanullah Kazmi
Numerade Educator
04:04

Problem 13

Output Power from a Motor. The output power produced by a rotating motor is given by the equation
$$
P=\tau_{\text {IND }} \omega_{m}
$$
where $\tau_{\text {IND }}$ is the induced torque on the shaft in newton-meters, $\omega_{m}$ is the rotational speed of the shaft in radians per second, and $P$ is in watts. Assume that the rotational speed of a particular motor shaft is given by the equation
$$
\omega_{m}=188.5\left(1-e^{-02 t}\right) \mathrm{rad} / \mathrm{s}
$$
and the induced torque on the shaft is given by
$$
T_{\mathrm{IND}}=10 e^{-0.2 t} \mathrm{~N} \cdot \mathrm{m}
$$
Plot the torque, speed, and power supplied by this shaft versus time for $0 \leq t \leq 10 \mathrm{~s}$. Be sure to label your plot properly with the symbols $\tau_{I N D}$ and $\omega_{n}$ where appropriate. Create two plots, one with the power displayed on a linear scale, and one with the output power displayed on a logarithmic scale. Time should always be displayed on a linear scale.

Narayan Hari
Narayan Hari
Numerade Educator
01:43

Problem 14

Plotting Orbits When a satellite orbits the Earth, the satellite's orbit will form an ellipse with the Earth located at one of the focal points of the ellipse. The satellite's orbit can be expressed in polar coordinates as
$$
r=\frac{p}{1-\varepsilon \cos \theta}
$$
where $r$ and $\theta$ are the distance and angle of the satellite from the center of the Earth, $p$ is a parameter specifying the size of the orbit, and $\varepsilon$ is a parameter representing the eccentricity of the orbit. A circular orbit has an eccentricity $\varepsilon$ of 0 . An elliptical orbit has an eccentricity of $0 \leq \varepsilon \leq 1$. If $\varepsilon>1$, the satellite follows a hyperbolic path and escapes from the Earth's gravitational field.

Consider a satellite with a size parameter $p=1000 \mathrm{~km}$. Plot the orbit of this satellite if $(a) e=0 ;(b) \varepsilon=0.25 ;(c) \varepsilon=0.5$. How close does each orbit come to the Earth? How far away does each orbit get from the Earth? Compare the three plots you created. Can you determine what the parameter $p$ means from looking at the plots?

Lauren Shelton
Lauren Shelton
Numerade Educator