arithmetic operations:
1+2
3
5.5/0.5
2^3
functions:
variables:
defining functions:
function FUNCTION NAME (ARGUMENTS)
INTERMEDIATE RESULTS
return FINAL RESULT
end
using Plots
list_of_days = [ALL THE X VALUES]
list_of_lemonades_sold = [ALL THE Y VALUES]
plot(list_of_days, list_of_lemonades_sold)
xlabel!("day")
ylabel!("lemonades sold")
my_list = [LIST ELEMENTS]
To get an element out of a list you can use its index (location in the list):
my_list[3]
And here is how to make an empty list of a fixed size:
my_empty_list = zeros(6)
for my_number in my_list
DO STUFF TO THE NUMBER
end
We can calculate the daily earnings from the lemonade stand:
Here is a way to do this in a neater way: not just calculating the earnings, but also storing them in a list for later use or plotting.
function calculate_cost(lemonades)
return COST CALCULATION EXPRESSION
end
list_of_lemonades_per_day = [ENTER THE LEMONADES HERE]
list_of_earnings_per_day = EMPTY LIST OF APPROPRIATE LENGTH
WRITE YOUR SOLUTION HERE
AND MAKE A PLOT
You can try something different: change the number of lemonades sold, change price, maybe calculate the total earnings since the beginning by each day.
TRY OUT DOT NOTATION HERE
if A CONDITION
DO SOMETHING IF THE CONDITION IS TRUE
else
DO SOMETHING IF THE CONDITION IS FALSE
end
TRY THIS OUT WITH A FUNCTION
Now that we have these tools we can use them to study how entities evolve in time. One of the easier ways to describe change through time is to describe how the next state of a system depends on its current state.
We will start with an example from population growth (we will measure population relative to some fixed resource, i.e. 0 for empty population and 1000 for the maximum population that the resource can provide).
function new_population(current_population)
CALCULATE NEW POPULATION
return THE NEW POPULATION
end
function new_population(current_population)
available_resources = (1000-current_population)/1000
change_in_population = 0.1*current_population*available_resources
new_population = current_population + change_in_population
return new_population
end
We can call this the "law governing the system" (it has many names, most of them unnecessarily pretentious and complicated sounding).
Over the last 200 years human scientists have uncovered a great richness and hidden complexity and beauty in otherwise simple-looking laws.
Let us continue with the example of population growth. For instance, we can study how the population evolves over a hundred years.
population_history = zeros(100)
population_history[1] = INITIAL POPULATION
for EACH YEAR
CALCULATE NEW POPULATION
end
plot(population_history)
xlabel!("year")
ylabel!("population")
It would be interesting to study how populations evolve depending on the initial population. To do that without having to rewrite the above code every time we can encapsulate it in a function. Then we can just call that function for different initial populations.
function pop_history_function(initial_population)
CALCULATE THE ENTIRE HISTORY IN A LIST
return THE HISTORY OF THE POPULATION
end
The initial growth will be very slow if we start with a very small population. Note the peculiar curve this makes!
plot(pop_history_function(SMALL INITIAL POPULATION))
xlabel!("year")
ylabel!("population")
Try different initial populations to explore whether the population grows or decays.
plot(pop_history_function(DIFFERENT INITIAL POPULATION))
xlabel!("year")
ylabel!("population")