Before you go on, you might want to work on the following exercises.
To test your understanding of assignment statements, write a few lines of code that swap the values of
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
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
c contain the number of bikes in each location at the beginning of the day. The postcondition is that
c have been modified to reflect the net movement of bikes.
To test your program, initialize
c at the prompt and then execute the script. The script should display the updated values of
c, but not any intermediate variables.
Remember that bikes are countable things, so
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
c over time.