Professional Documents
Culture Documents
style. You can find this style under the format tab in the menu, go to StyleSheet and then to Article and youll see the JournalArticle style ticked. Evidently you can change the style to another Stylesheet if you like. If you work in, for example, an Article style or a Book style and you just start typing somewhere then Mathematica will assume you are going to write text and open a text-cell. However if you work in the Default stylesheet and just start typing somewhere, then Mathematica will assume you are going to do calculations and thus open up a Input-cell.
Summary: The purpose of subsubsection 1.1.1 is to quickly elucidate how you can draw diagrams in Mathematica by hand. This will be useful in the submission of any coursework involving diagrams or sketches of graphs. You may want to draw some diagrams in your text by hand. Mathematica will support you in doing that. First you create a figure cell and then you go to the menu Graphics and open up DrawingTools. Subsequently you put the cursor in the Figure cell and click on the New button.
Demand
Supply
MathematicaPrimer2013.nb
Demand
Supply
Quantity
Price
The way of drawing diagrams this was is probably cleaner and easier than with pencil, paper and rules. Evidently on the exams you will have to use pens, pencils and paper. However, if you get it right using the drawing tools in Mathematica, it would be very surprising if you would get it terribly wrong with pencil and paper. Drawing diagrams by hand is especially useful when either you have no idea about the functional forms youre looking for (which would be a tad worrisome I have to admit) or when you want to draw figures unrelated to plotting functions, such as organizational diagrams, concept-clouds or flow-diagrams.
1.3 What is the typical structure of a Mathematica command and how do I execute it?
Summary: The purpose of subsection 1.3 is to explain how to evaluate/execute or activate commands in Mathematica. Mathematica commands are written into input cells and input cells automatically generate output cells as soon as you Evaluate them. An input cell can be something as simply as a direct arithmetic instruction, such as for example 2 2
MathematicaPrimer2013.nb
In order for anything to happen we need to evaluate it. Evaluation The process of "Evaluation" in Mathematica means calculating something. A separate part of the Mathematica programme called the "Kernel" is responsible for executing the calculations you do. As soon as you ask Mathematica to do a first calculation then "Kernel" will start up. The first time it does this it might take anywhere between 5 and 30 seconds. All commands after that will be processed quicker. The quick way to evaluate an input cell is to put the cursor in it and hit SHIFT-Enter, if we do that to the input cell above we get 2 2
Alternatively we could also have selected the cell using the mouse-pointer and the left mouse-button and then choose evaluate cells from the Evaluation tab in the menu. Thats cumbersome and I never do use it. But you might if you have set-up your notebook as demonstration and you only wish to execute a selected few commands (input cells) from your notebook in a presentation. If you select several cells (with your left mouse-button while keeping Ctrl pressed for example) and then evaluate them, Mathematica will evaluate them in the order in which they appear in the notebook from title to end. If an evaluation takes very long, then usually you have either sent Mathematica into the jungle with a set of commands that are almost impossible to execute properly, or it has gone into a loop from which it finds it hard to break away. In such cases it can be expedient to use the Abort Evaluation from the Evaluation tab menu in order to stop the running evaluation and check your commands whether youre over-asking Mathematica. Interrupt Evaluation is useful when Mathematicas calculations are drawing heavily on your CPU and you need to pick up that Skype call or watch that YouTube video before allowing Mathematica to complete its job. One particularly relevant command in the "Evaluation" drop-down menu is "Quit the Kernel" which shuts off any calculations and throws away all the intermediate results of calculation up until that moment. It is sometimes the only way to get Mathematica to stop doing something you don't want it to do if an "Abort Evaluation" or "Interrupt Evaluation" fail. These two you can also find in the drop-down menu but they are only visibly as black letters when there is actually an evaluation running.
1.4 How can I do basic numerical and symbolic calculations with Mathematica?
Summary: The purpose of subsection 1.4 is to familiarize you with the basic computational functionalities in Mathematica. Now lets do some calculations! I will try to set up some context that has some relevance to Economics. For this first section I will look at long-run and short-run cost curves.
K .
(1)
K1
1 Q 2 K
You can turn the command into standard English as follows: it says take Cost replace Q by 10000 and then replace K by 1000. The symbol /. is the replace command and the Q 10 000 is the rule. Together such combinations are called replacement rules and they are very useful in manipulating expressions.
MathematicaPrimer2013.nb
Evidently you can also replace a variable with another expression. Suppose you would like to assume that the capital K required for the production of a quantity Q is actually linearly related to the quantity you want to produce, i.e. suppose we would have a relationship like K K0 aQ
where K0 represents some fixed capital you need in order to be able to produce anything and the coefficient a represents the increase in required capital if we increase the quantity we want to produce. We can substitute this into our Cost as follows Cost . K K0 aQ
Note that you need to write a space between the Q and the a otherwise Mathematica will think aQ is a name of something rather than a product of two things. When we evaluated the cost with two numbers substituted you will have seen that the result in the output cell appears symbolic in a weird way. Why doesnt Mathematica provide you with a decimal number answer? Well, it can but you have to tell it to! As long as you dont tell it Mathematica will even treat ordinary numbers are symbols in some ways. By explicitly requesting Mathematica to turn it into a numerical value you will get an ordinary number. There are two ways of doing this N Cost . Q 10 000 . K 1000 Cost . Q 10 000 . K 1000 N In the first line I used the official command N ... " that turns the expression in the brackets [] into a numerical expression as much as possible. In the second line I appended our earlier expression by N which is Mathematica speak for after youve done all the stuff before the // then please apply the command written after the //. Another very common command you will use in these two fashions in the Simplify command. When I apply Simplify to the cost expression with the two numbers inserted it gives me Simplify Cost . Q 10 000 . K 1000 Cost . Q 10 000 . K 1000 Simplify It has explicitly factorized a factor 100. Whether you consider that simpler is largely a matter of taste I would say. As a result Simplify gives you expressions which Mathematica considers simpler than the one you started out with. In my experience the result of Simplify is indeed often simpler than what you started out from, but on many occasions it is not. In addition to Simplify there is also a FullSimplify command. The difference between these two is the amount of time and the amount of tricks Mathematica is going to invest in simplifying an expression. The main drawback of FullSimplify is that sometimes Mathematica will use excessive amounts of time merely to produce an expression that is just a little bit simpler than what a Simplify would have produced ... a diminishing marginal product of CPU time ... I dont need to explain that I guess! If we again wish to substitute our relationship between quantity Q and required capital K we can Cost . K K0 aQ . Q 10 000 . K0 1000 Simplify N
giving us a function of the parameter a only. Boiling down symbolic expressions with lots of substitutions simplifications and numerical values inserted can be quite cumbersome and yet great care must be taken! Note: When you use several replacement rules and commands the order in which you apply them may affect the final result! So always take care that you check whether the order is the one you want! Mathematica will not think for you.
Its okay, but not very pretty. Lets put this plot in a frame Plot Cost . K K0 , Frame True aQ . Q 10 000 . K0 1000 Simplify N, a, 0, 0.25
Adding a frame is a so-called Plot Option and Mathematica offers you loads of them. Now a name would be nice as well Plot Cost . K K0 a Q . Q , Frame True, PlotLabel 10 000 . K0 1000 Simplify "Cost for Q 1000 and K0 1000" N, a, 0, 0.25
MathematicaPrimer2013.nb
And how about saying what is on the axes? Plot Cost . K K0 a Q . Q , Frame True, PlotLabel FrameLabel "a", "Cost" 10 000 . K0 1000 Simplify N, a, 0, 0.25 "Cost for Q 1000 and K0 1000",
I would say that if you want to use Mathematica to produce plots for coursework that you need to submit then such a plot with a name and legible axes-labels is the minimum standard.
K Cost
Minimizing the cost means setting the derivative of the cost with respect to K equal to 0, and checking the sign of the second derivative with respect to K . Let's first look at the first step. Here we can use the Mathematica command Solve to come up with a solution Solve
K Cost
0, K
Before evaluating it, please carefully read this command into standard English. It says Solve the equation K Cost 0 for the variable K . The fact that we use a double is equal sign, == with no space in between, is to let Mathematica understand the difference between assigning the two sides to be equal and requiring the two sides to be equal. Earlier in our definition of Cost we assigned the symbol Cost to be equal to the expression we used. Now however we are requiring the derivative to be 0, we are not assigning the value 0 to it. Note: The difference between assigning and requiring may sound like pedantic nitpicking to you, but it is actually crucial. What this shows is that Mathematica forces you to be really precise about what you want it to do and how you want it to do it. Mathematica will not think for you. If you think rubbish ... Mathematica will produce rubbish. You will not believe how many students find Mathematics hard because they are not careful with such distinctions as the one here between assigning a value and requiring a value. Mathematica will require you to be precise, for me that is a life-saving property of Mathematics despite that it can terribly annoy me when the programme exposes my sloppy thoughts. The process of solving the equation is hopefully going to produce a solution that I can use. To make it easy for me to call it back I will just give it a name. SolK Solve
K Cost
0, K
Note that it writes down the solution as a replacement rule within curly brackets. The curly brackets indicate that the Solve commands expects that equations could have multiple solutions and in that case it would use toe curly brackets to write lists of solutions rather than pick one. We will come to handling lists later on. Here we only have one solution fortunately. If we wish to use the solution in an expression we must use it like a replacement rule. Note that when we use it directly to replace K with the solution of requiring the derivative to vanish K . SolK Mathematica retains one set of curly brackets. However when you ask Mathematica not to use SolK but the rule within the first set of curly brackets which is SolK[[1]] K . SolK 1
we do get what we expected. Of course more interesting is substituting this solution back into the cost function Cost . SolK 1
If we want to check whether this is actually a minimum we should evaluate the second derivative at the solution we found, i.e.
K K Cost
. SolK
The second derivative is always positive for non-vanishing quantities, i.e. we are indeed dealing with a minimum!
MathematicaPrimer2013.nb
NSolve
Cost . K
100
5000, Q
In fact we could define a function in Mathematica which gives us this solution for Q whenever we specify both K and the Cost. In Mathematica this command would read QSol Cst_, k_ : Q . NSolve Cost . K k Cst, Q 1
Before you activate the command note: the variables in the function QSol are specified by a symbol followed by an underscore. This tells Mathematica that these symbols are arbitrary or wildcards and that we want Mathematica to try and compute QSol no matter what we put in the place of these arguments! Secondly the function is defined using a := sign which tells Mathematica to evaluate the value of the function only when we explicity call for it and not immediately after activating the command. If you now activate it, then the command below will compute the same solution as above QSol 5000, 100 This set-up is useful when you want to make plots of solutions! As we will do on Friday! The "FindRoot" command is similar to the "NSolve". The main difference is that in the FindRoot command we specify the equation we want to solve, the variable for which we want to solve it and we specify a value which is probably close to the real solution. In the case of our previous example FindRoot Cost . K 100 5000, Q, 4615
The "FindRoot" command is particularly useful when NSolve cannot find a solution because the equations are to complicated but you nevertheless have an idea where the solution should be. Also when an equation has many solutions, then FindRoot can be used to focus on those solutions you are mst interested in by asking Mathematica to look for the solution in the right neighborhood.
MathematicaPrimer2013.nb
sF K t , L t
K t .
(2)
sF K t , L t
K t
0 .
(3)
A K a L1
(4)
F K_, L_ :
A Ka L1
The underscores in the definition tell Mathematica that these K and L are arguments of the function. Mathematica will memorize this function for arbitrary first and second inputs. The arguments have a different colour than the parameters! The symbol : basically means the same thing as the standard assigning-is-equal . The added : merely indicated that the evaluation of the command is delayed. Mathematica will not calculate F K , L unless we explicitly ask for it, and then it will evaluate it anew every time it is called for. We can now use the solve command to find the equilibrium economy SolSS Solve s F K, L K 0, K
Substituting this into the productivity then gives us the equilibrium output F K, L . SolSS 1
s
a
1 a
L A1
Question 1: Plot the equilibrium output per capita as a function of the ratio of investment and depeciation rate for a few choices of exponent a. When powers are concerned Mathematica is often more careful than it needs to be! Note: If you become precise in distinguishing between arguments of a function and the parameters describing the type and shape of a function, you will find out that it also becomes easier to understand which parts of a problem are Economics and which parts are actually Mathematics. The standard pictures of the Equilibrium Economies You may have seen this use of the Solow-Swan model before as well as a graphical representation of the equilibrium as the intersection of two graphs, the graph of f K and the graph of gK K . s F K, L ,
MathematicaPrimer2013.nb
Evidently we can plot that to using the simple plot-commands used earlier.
In[3]:=
Plot s F K, L . L 100 000 . a 0.25 . A 1 . s 0.05, K . 0.02 , K, 0, 600 000 , Frame True, PlotLabel "Solow Swan", FrameLabel "K ", "Value" Solow Swan
12 000
10 000
8000 Value
Out[3]=
6000
4000
2000
0 0 100 000 200 000 300 000 K 400 000 500 000 600 000
It is also easy to reproduce the typical depreciation shock picture Plot s F K, L . L 100 000 . a 0.25 . A 1 . s 0.05, K . K . 0.025 , K, 0, 600 000 , Frame True, PlotLabel "Solow Swan: Depreciation shock", FrameLabel "K ", "Value" 0.02,
Question 2: How do you intepret this plot? What does it say about an economy that experiences a shock increasing the depreciation? or the typical technology-shock picture
In[4]:=
Plot s F K, L . L 100 000 . a 0.25 . A 1 . s 0.05, s F K, L . L 100 000 . a 0.25 . A 1.1 . s 0.05, K . K, 0, 600 000 , Frame True, PlotLabel "Solow Swan: Technology shock", FrameLabel "K ", "Value" Solow Swan: Technology shock
12 000
0.02 ,
10 000
8000 Value
Out[4]=
6000
4000
2000
0 0 100 000 200 000 300 000 K 400 000 500 000 600 000
Question 3: How do you intepret this plot? What does it say about an economy that experiences a shock increasing the technology? But maybe you have always wondered what would happen if there was a shock in the exponents a, an elasticity shock?
10
MathematicaPrimer2013.nb
In[5]:=
Plot s F K, L . L 100 000 . a 0.25 . A 1 . s 0.05, s F K, L . L 100 000 . a 0.45 . A 1 . s 0.05, K . , Frame True, PlotLabel "Solow Swan: Elasticity shock ? FrameLabel "K ", "Value" Solow Swan: Elasticity shock ? at low 0.02
12 000
10 000
8000 Value
Out[5]=
6000
4000
2000
0 0 100 000 200 000 300 000 K 400 000 500 000 600 000
Question 4: How do you intepret this plot? What does it say about an economy that experiences a shock increasing the output elasticity of capital? Can you think of an example of such a shock? As you might be able to see from this graph: depending on the value of the depreciation the response of the economy to the shock is very different. At a very high depreciation rate of 10% for example we would find
In[6]:=
Plot s F K, L . L 100 000 . a 0.25 . A 1 . s 0.05, s F K, L . L 100 000 . a 0.45 . A 1 . s 0.05, K . , Frame True, PlotLabel "Solow Swan: Elasticity shock ? FrameLabel "K ", "Value" Solow Swan: Elasticity shock ? at high 0.1
10 000
8000
6000
Out[6]=
Note: Never forget that you can only make graphs and plots of functions that actually produce numbers! If you are trying to plot something and it doesnt work ... there is a 99% chance that you forgot to assign a value to some of the parameters in your functions. Question 5: First save this file normally! Then remove all the commands (but leave the results) and change all the output cells containing graphs or figures into figure cells. Finally choose save as and choose to save the file as pdf. Then upload the pdf into turnitin and dont worry about the similarity between your submission and others.