Skip to main content
Contents
Calc
Dark Mode Prev Next Profile
\(\newcommand\DLGray{\color{Gray}}
\newcommand\DLO{\color{BurntOrange}}
\newcommand\DLRa{\color{WildStrawberry}}
\newcommand\DLGa{\color{Green}}
\newcommand\DLGb{\color{PineGreen}}
\newcommand\DLBa{\color{RoyalBlue}}
\newcommand\DLBb{\color{Cerulean}}
\newcommand\ds{\displaystyle}
\newcommand\ddx{\frac{d}{dx}}
\newcommand\os{\overset}
\newcommand\us{\underset}
\newcommand\ob{\overbrace}
\newcommand\obt{\overbracket}
\newcommand\ub{\underbrace}
\newcommand\ubt{\underbracket}
\newcommand\ul{\underline}
\newcommand\tikznode[3][]
{\tikz[remember picture,baseline=(#2.base)]
\node[minimum size=0pt,inner sep=0pt,#1](#2){#3};
}
\newcommand\del{\nabla}
\newcommand\R{\mathbb{R}}
\newcommand\C{\mathcal{C}}
\newcommand\N{\mathcal{N}}
\newcommand\eps{\varepsilon}
\renewcommand\epsilon{\varepsilon}
\renewcommand\subset{\subseteq}
\newcommand\norm[1]{\|{#1}\|}
\newcommand\matrixbrackets[4][1]{
\draw (#3,#2) -- (\fpeval{#3+0.2},#2);
\draw (#3,#1) -- (#3 ,#2);
\draw (#3,#1) -- (\fpeval{#3+0.2},#1);
\draw (#4,#2) -- (\fpeval{#4-0.2},#2);
\draw (#4,#1) -- (#4 ,#2);
\draw (#4,#1) -- (\fpeval{#4-0.2},#1);
}
\tikzstyle{circle node 0}=[fill=white, draw=black, shape=circle, inner sep=0pt]
\tikzstyle{circle node 2}=[fill=white, draw=black, shape=circle, inner sep=2pt]
\tikzstyle{hrect node}=[fill=white, draw=black, inner sep=2pt, outer sep=3pt]
\tikzstyle{vrect node}=[fill=white, draw=black, inner sep=0pt, outer sep=0pt]
\tikzstyle{hidden node 0}=[inner sep=0pt, outer sep=0pt]
\tikzstyle{hidden node 2}=[fill=white, inner sep=2pt, outer sep=2pt]
\tikzstyle{rect node 1}=[fill=white, inner sep=2pt, outer sep=0pt]
\tikzstyle{rect node 2}=[fill=white, draw=black, inner sep=2pt, outer sep=0pt]
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
π§π»βπ» Class Activities π§π»βπ» Class Activities
1. Hello Function.
(a)
Create a function named
hello with no inputs and outputs that displays
'Hello World' using the
fprintf command. Test your function by typing
hello in the command window.
Solution .
function hello()
fprintf('Hello World!\n');
end
(b)
Modify the function to accept one input (
name) and display a greeting, such as
'Hello Alice, nice to meet you!' where
name is
'Alice'.
Solution .
function hello(name)
fprintf('Hello %s, nice to meet you!\n', name);
end
(c)
Try typing
hello with no input and run it. What error message do you get, and what is MATLAB telling you to fix?
Solution .
Not enough input arguments.
Error in hello (line 3)
fprintf('Hello %s, nice to meet you!\n', name);
This means that the function
hello expects one input argument, but none was provided when it was called.
(d)
Now, test your function with an input:
hello('Robyn').
Solution .
When you run
hello('Robyn'), the output is:
Hello Robyn, nice to meet you!
2. Projectile Height Function.
(a)
Create a function named
projectile_height that takes four inputs: initial height
h0, initial velocity
v0, gravity
g, and time
t.
The function should return one output, the height h at time t, given by:
\begin{equation*}
h(t) = h_0 + v_0 t - \frac{1}{2} g t^2\text{.}
\end{equation*}
Do not include any
fprintf commands inside the function. The functionβs body should contain only a single line that computes the height.
Solution .
function h = projectile_height(h0, v0, g, t)
h = h0 + v0*t - 0.5*g*t^2;
end
(b)
Test your function using
h0 = 1.5,
v0 = 12,
g = 9.81, and
t = 0.8.
Solution .
h = projectile_height(1.5, 12, 9.81, 0.8);
fprintf('h = %.2f m\n', h);
The computed height is
h = 7.96 meters.
3. Circle Metrics Function.
Write a function that returns more than one output.
(a)
Create a function named
circle_metrics that takes one input
r (radius) and returns two outputs:
C (circumference) and
A (area).
Solution .
function [C, A] = circle_metrics(r)
C = 2*pi*r;
A = pi*r^2;
end
(b)
Test your function with
r = 4.5 and print the results with three decimal places.
Solution .
[C, A] = circle_metrics(4.5);
fprintf('C = %.3f\t A = %.3f\n', C, A);
4. Coin Total Function.
In the piggy bank script, you computed a total value from the number of pennies, nickels, dimes, and quarters. Now, package that computation into a function so you can reuse it with different coin counts.
(a)
Create a function named
coin_total with four inputs:
nPennies,
nNickels,
nDimes, and
nQuarters. The function should return one output,
totalDollars.
Solution .
function totalDollars = ...
coin_total(nPennies, nNickels, nDimes, nQuarters)
% Compute total value in cents
cents = nPennies + 5*nNickels + 10*nDimes + 25*nQuarters;
totalDollars = cents/100;
end
(b)
Test your function by running the following command in the Command Window.
total = coin_total(1, 1, 1, 1)
Solution .
Your result should be a single dollar amount stored in
total.
(c)
In the Command Window, compute dollars and leftover cents from
total (do not change the function).
Solution .
nDollars = floor(total);
nCents = total - 100 * nDollars;
fprintf('Total value: $%i.%02i\n', nDollars, nCents);
5. Quadratic Solver Function.
Write a function that computes the two solutions of
\(a x^2 + b x + c = 0\) using the quadratic formula. This function may return complex solutions when the discriminant is negative.
(a)
Create a function named
quadratic with inputs
a,
b, and
c. Return two outputs,
x1 and
x2.
Solution .
function [x1, x2] = quadratic(a, b, c)
discriminant = b^2 - 4*a*c;
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b - sqrt(discriminant)) / (2*a);
end
(b)
Test your function on each equation below, and print the results. Use the real-and-imaginary printing approach so your output also works for complex solutions.
\(\displaystyle 2x^2 - 7x + 5 = 0\)
\(\displaystyle 15x^2 - 135x + 300 = 0\)
\(\displaystyle x^2 + 36 = 0\)
Solution .
[x1, x2] = quadratic(2, -7, 5);
fprintf('Eqn 1: x1 = %g%+gi, x2 = %g%+gi\n', real(x1), imag(x1), real(x2), imag(x2));
[x1, x2] = quadratic(15, -135, 300);
fprintf('Eqn 2: x1 = %g%+gi, x2 = %g%+gi\n', real(x1), imag(x1), real(x2), imag(x2));
[x1, x2] = quadratic(1, 0, 36);
fprintf('Eqn 3: x1 = %g%+gi, x2 = %g%+gi\n', real(x1), imag(x1), real(x2), imag(x2));
(c)
Are there any inputs for which this function fails? Give an example and explain why.
Solution .
The function fails when
a = 0, because the quadratic formula divides by
\(2a\text{.}\) Handling
a = 0 properly requires making a decision inside the function, which needs flow control.