Skip to main content
Logo image

πŸ§‘πŸ»β€πŸ’» 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.
The error message is:
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.

Package the projectile height model script as a 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.
  1. \(\displaystyle 2x^2 - 7x + 5 = 0\)
  2. \(\displaystyle 15x^2 - 135x + 300 = 0\)
  3. \(\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.