Using Julia as a calculator

arithmetic operations:

In [1]:
1+2
Out[1]:
3
In [ ]:
5.5/0.5
In [ ]:
2^3

functions:

In [ ]:

variables:

In [ ]:

defining functions:

In [ ]:
function FUNCTION NAME (ARGUMENTS)
    INTERMEDIATE RESULTS
    return FINAL RESULT
end

Making "plots"

In [ ]:
using Plots
In [ ]:
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")

Storing numbers in "lists"

In [ ]:
my_list = [LIST ELEMENTS]

To get an element out of a list you can use its index (location in the list):

In [ ]:
my_list[3]

And here is how to make an empty list of a fixed size:

In [ ]:
my_empty_list = zeros(6)

Doing repetitive stuff in "loops"

In [ ]:
for my_number in my_list
    DO STUFF TO THE NUMBER
end

We can calculate the daily earnings from the lemonade stand:

In [ ]:

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.

In [ ]:
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
In [ ]:
WRITE YOUR SOLUTION HERE
In [ ]:
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.

In [ ]:
TRY OUT DOT NOTATION HERE

Conditions

In [ ]:
if A CONDITION
    DO SOMETHING IF THE CONDITION IS TRUE
else
    DO SOMETHING IF THE CONDITION IS FALSE
end
In [ ]:
TRY THIS OUT WITH A FUNCTION

Modeling the growth of an entity

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).

In [ ]:
function new_population(current_population)
    CALCULATE NEW POPULATION
    return THE NEW POPULATION
end
In [ ]:
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.

In [ ]:
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.

In [ ]:
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!

In [ ]:
plot(pop_history_function(SMALL INITIAL POPULATION))
xlabel!("year")
ylabel!("population")

Try different initial populations to explore whether the population grows or decays.

In [ ]:
plot(pop_history_function(DIFFERENT INITIAL POPULATION))
xlabel!("year")
ylabel!("population")