## Krista Donaldson, Krista Donaldson

## Chapter 3

## Functions and Files - all with Video Answers

## Educators

Chapter Questions

Suppose that $y=-3+i x$. For $x=0,1$, and 2 , use MATLAB to compute the following expressions. Hand check the answers.

a. $|y|$

b. $\sqrt{y}$

c. $(-5-7 i) y$

d. $\frac{1}{6-3 i}$

Julie Silva

Numerade Educator

Let $x=-5-8 i$ and $y=10-5 i$. Use MATLAB to compute the following expressions. Hand check the answers.

a. The magnitude and angle of $x y$.

b. The magnitude and angle of $\frac{1}{y}$.

Teresa Liang

Numerade Educator

Use MATLAB to find the angles corresponding to the following coordinates. Hand check the answers.

a. $(x, y)=(5.8)$

b. $(x, y)=(-5,8)$

c. $(x, y)=(5,-8)$

d. $(x, y)=(-5,-8)$

Cindy Rodgers

Numerade Educator

For several values of $x$, use MATLAB to confirm that $\sinh x=$ $\left(e^x-e^{-x}\right) / 2$.

Bobby Barnes

University of North Texas

For several values of $x$, use MATLAB to confirm that $\sinh ^{-1} x=$ $\ln \left(x+\sqrt{x^2+1}\right),-\infty<x<\infty$.

Arpit Giri

Numerade Educator

The capacitance of two parallel conductors of length $L$ and radius $r$, separated by a distance $d$ in air, is given by

$$ C=\frac{\pi \epsilon L}{\ln \left(\frac{d-r}{r}\right)} $$ where $\epsilon$ is the permittivity of air $\left(\epsilon=8.854 \times 10^{-12} \mathrm{~F} / \mathrm{m}\right)$. Write a script file that accepts user input for $d, L$, and $r$, and computes and displays $C$. Test the file with the values: $L=1 \mathrm{~m}, r=0.001 \mathrm{~m}$, and $d=0.004 \mathrm{~m}$.

Sheh Lit Chang

University of Washington

When a belt is wrapped around a cylinder, the relation between the belt forces on each side of the cylinder is $$ F_1=F_2 e^{\mu \beta} $$ where $\beta$ is the angle of wrap of the belt and $\mu$ is the friction coefficient. Write a script file that first prompts a user to specify $\beta, \mu$, and $F_2$ and then computes the force $F_1$. Test your program with the values $\beta=130^{\circ}$. $\mu=0.3$, and $F_2=100 \mathrm{~N}$. (Hint: Be careful with $\beta!$ )

Carson Merrill

Numerade Educator

The MATLAB trigonometric functions expect their argument to be in radians. Write a function called sind that accepts an angle $x$ in degrees and computes $\sin x$. Test your function.

Foster Wisusik

Numerade Educator

Write a function that accepts temperature in degrees F and computes the corresponding value in degrees $C$. The relation between the two is $$ T^{\circ} \mathrm{C}=\frac{5}{9}\left(T^{\circ} \mathrm{F}-32\right) $$ Be sure to test your function.

Check back soon!

An object thrown vertically with a speed $v_0$ reaches a height $h$ at time $t$, where $$ h=v_0 t-\frac{1}{2} g t^2 $$

Write and test a function that computes the time $t$ required to reach a specified height $h$, for a given value of $t_0$. The function's inputs should be $h, v_0$, and $g$. Test your function for the case where $h=100 \mathrm{~m}$, $v_0=50 \mathrm{~m} / \mathrm{s}$, and $g=9.81 \mathrm{~m} / \mathrm{s}^2$. Interpret both answers.

Amrita Bhasin

Numerade Educator

A water tank consists of a cylindrical part of radius $r$ and height $h$, and a hemispherical top. The tank is to be constructed to hold $500 \mathrm{~m}^3$ when filled. The surface area of the cylindrical part is $2 \pi r h$, and its volume is $\pi r^2 h$. The surface area of the hemispherical top is given by $2 \pi r^2$, and its volume is given by $2 \pi r^3 / 3$. The cost to construct the cylindrical part of the tank is $\$ 300$ per square meter of surface area; the hemispherical part costs $\$ 400$ per square meter. Use the fminbnd function to compute the radius that results in the least cost. Compute the corresponding height $h$.

Pawan Yadav

Numerade Educator

A fence around a field is shaped as shown in Figure P12. It consists of a rectangle of length $L$ and width $W$, and a right triangle that is symmetrical about the central horizontal axis of the rectangle. Suppose the width $W$ is known (in meters), and the enclosed area $A$ is known (in square meters). Write a user-defined function file with $W$ and $A$ as inputs. The outputs are the length $L$ required so that the enclosed area is $A$, and the total length of fence required. Test your function for the values $W=6 \mathrm{~m}$ and $A=80 \mathrm{~m}^2$.

Erika Bustos

Numerade Educator

A fenced enclosure consists of a rectangle of length $L$ and width $2 R$, and a semicircle of radius $R$, as shown in Figure P13. The enclosure is to be built to have an area $A$ of $1600 \mathrm{ft}^2$. The cost of the fence is $\$ 40$ per foot for the curved portion, and $\$ 30$ per foot for the straight sides. Use the fminbnd function to determine with a resolution of 0.01 ft the values of $R$ and $L$ required to minimize the total cost of the fence. Also compute the minimum cost.

Lucas Finney

Numerade Educator

Using estimates of rainfall, evaporation, and water consumption, the town engineer developed the following model of the water volume in the reservoir as a function of time. $$ V(t)=10^9+10^5\left(1-e^{-t / 100}\right)-r t $$ where $V$ is the water volume in liters, $t$ is time in days, and $r$ is the town's consumption rate in liters/day. Write two user-defined functions. The first function should define the function $V(t)$ for use with the fzero function. The second function should use fzero to compute how long it will take for the water volume to decrease to $x$ percent of its initial value of $10^9 \mathrm{~L}$. The inputs to the second function should be $x$ and $r$. Test your functions for the case where $x=50$ percent and $r=10^7$ U/day.

Julian Wong

Numerade Educator

The volume $V$ and paper surface area $A$ of a conical paper cup are given by $$ V=\frac{1}{3} \pi r^2 h \quad A=\pi r \sqrt{r^2+h^2} $$ where $r$ is the radius of the base of the cone and $h$ is the height of the cone.

a. By eliminating $h$, obtain the expression for $A$ as a function of $r$ and $V$.

b. Create a user-defined function that accepts $R$ as the only argument and computes $A$ for a given value of $V$. Declare $V$ to be global within the function.

c. For $V=10 \mathrm{in}^3$, use the function with the fminbnd function to compute the value of $r$ that minimizes the area $A$. What is the corresponding value of the height $h$ ? Investigate the sensitivity of the solution by plotting $V$ versus $r$. How much can $R$ vary about its optimal value before the area increases 10 percent above its minimum value?

Kimberly Waterbury

Numerade Educator

A torus is a shaped like a doughnut. If its inner radius is $a$ and its outer radius is $b$, its volume and surface area are given by

$$ V=\frac{1}{4} \pi^2(a+b)(b-a)^2 \quad A=\pi^2\left(b^2-a^2\right) $$

a. Create a user-defined function that computes $V$ and $A$ from the arguments $a$ and $b$.

b. Suppose that the outer radius is constrained to be 2 in . greater than the inner radius. Write a script file that uses your function to plot $A$ and $V$ versus $a$ for $0.25 \leq a \leq 4$ in.

Victor Salazar

Numerade Educator

Suppose it is known that the graph of the function $y=a x^3+b x^2+c x+d$ passes through four given points $\left(x_i, y_i\right), i=1,2,3,4$. Write a user-defined function that accepts these four points as input and computes the coefficients $a, b, c$, and $d$. The function should solve four linear equations in terms of the four unknowns $a, b, c$, and $d$. Test your function for the case where $\left(x_i, y_j\right)=(-2,-20),(0,4),(2,68)$, and $(4,508)$, whose answer is $a=7, b=5, c=-6$, and $d=4$.

Joy Carpio

Numerade Educator

Use the gen_plot function described in Section 3.3 to obtain two subplots, one plot of the function $10 e^{-2 r}$ over the range $0 \leq x \leq 2$, and the other a plot of $5 \sin (2 \pi x / 3)$ over the range $0 \leq x \leq 6$.

WZ

Wen Zheng

Numerade Educator

Create an anonymous function for $10 e^{-2 x}$ and use it to plot the function over the range $0 \leq x \leq 2$.

Joseph Lentino

Numerade Educator

Create an anonymous function for $20 x^2-200 x+3$ and use it

a. to plot the function to determine the approximate location of its minimum, and

h. with the fminbna function to precisely determine the location of the minimum.

James Kiss

Numerade Educator

Create four anonymous functions to represent the function $6 e^{3 \cos x^2}$, which is composed of the functions $h(z)=6 e^2, g(y)=3 \cos y$, and $f(x)=x^2$. Use the anonymous functions to plot $6 e^{3 \cos x^2}$ over the range $0 \leq x \leq 4$.

Peter Winans

Numerade Educator

Use a primary function with a subfunction to compute the zeros of the function $3 x^3-12 x^2-33 x+90$ over the range $-10 \leq x \leq 10$.

Carson Merrill

Numerade Educator

Create a primary function that uses a function handle with a nested function to compute the minimum of the function $20 x^2-200 x+3$ over the range $0 \leq x \leq 10$.

Check back soon!

Use a text editor to create a file containing the following data. Then use the load function to load the file into MATLAB, and use the mean function to compute the mean value of each column.

Check back soon!

Enter and save the data given in Problem 24 in a spreadsheet. Then import the spreadsheet file into the MATLAB variable A. Use MATLAB to compute the sum of each column.

Hast Aggarwal

Numerade Educator

Use a text editor to create a file from the data given in Problem 24, but separate each number with a semicolon. Then use the Import Wizard to load and save the data in the MATLAB variable A.

Check back soon!

Use a text editor to create a file temperature. dat containing the temperature data given on page 175. Then use the Import Wizard to load and save the data in the MATLAB variable temperature. Compute the mean value of each column.

Check back soon!