Skip to main content
Engineering LibreTexts

17: Exercises

  • Page ID
    85949
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Before you go on, you might want to work on the following exercises.

    Exercise 2.1

    To test your understanding of assignment statements, write a few lines of code that swap the values of x and y. Put your code in a script called swap.m and test it.

    If it works correctly, you should be able to run it like this:

    >> x = 1, y = 2
    x = 1
    y = 2
    
    >> swap
    
    >> x, y
    x = 2
    y = 1
    
    Exercise 2.2

    Imagine that you are the operator of a bike-share system with two locations: Boston and Cambridge.

    You observe that every day 5 percent of the bikes in Boston are dropped off in Cambridge, and 3 percent of the bikes in Cambridge get dropped off in Boston. At the beginning of the month, there are 100 bikes at each location.

    Write a script called bike_update.m that updates the number of bikes in each location from one day to the next. The precondition is that the variables b and c contain the number of bikes in each location at the beginning of the day. The postcondition is that b and c have been modified to reflect the net movement of bikes.

    To test your program, initialize b and c at the prompt and then execute the script. The script should display the updated values of b and c, but not any intermediate variables.

    Remember that bikes are countable things, so b and c should always be integer values. You might want to use the round function to compute the number of bikes that move each day.

    If you execute your script repeatedly, you can simulate the passage of time from day to day (you can repeat a command by pressing the arrow and then ).

    What happens to the bikes? Do they all end up in one place? Does the system reach an equilibrium, does it oscillate, or does it do something else?

    In the next chapter, we will see how to execute your script automatically and how to plot the values of b and c over time.


    This page titled 17: Exercises is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.