This is an example on how you can use R to solve some the exercises in your textbook.
Lets start with exercise A.1 (p.653)
p = seq(from = 0, to = 50, by = 1) # Create p
q = -3 + 1.5 * p # Supply function
df=data.frame(q, p) # Store p & q in a data frame, and print it out
df
q p
1 -3.0 0
2 -1.5 1
3 0.0 2
4 1.5 3
5 3.0 4
6 4.5 5
7 6.0 6
8 7.5 7
9 9.0 8
10 10.5 9
11 12.0 10
12 13.5 11
13 15.0 12
14 16.5 13
15 18.0 14
16 19.5 15
17 21.0 16
18 22.5 17
19 24.0 18
20 25.5 19
21 27.0 20
22 28.5 21
23 30.0 22
24 31.5 23
25 33.0 24
26 34.5 25
27 36.0 26
28 37.5 27
29 39.0 28
30 40.5 29
31 42.0 30
32 43.5 31
33 45.0 32
34 46.5 33
35 48.0 34
36 49.5 35
37 51.0 36
38 52.5 37
39 54.0 38
40 55.5 39
41 57.0 40
42 58.5 41
43 60.0 42
44 61.5 43
45 63.0 44
46 64.5 45
47 66.0 46
48 67.5 47
49 69.0 48
50 70.5 49
51 72.0 50
Lets look at a plot of these two variables. The first one is the mathematica perspective where q is on the y-axis:
plot(q ~ p, type="l", main="Mathematical perspective")
The second one is the economic perspective where p is on the y-axis:
plot(p ~ q, type="l", main="Economic perspective")
We could make the plot a bit nicer, adding lables and a solid line.
plot(p ~ q, xlab = "Quantity", ylab = "Price", type = "l", lwd=2, main="Supply")
Another way of doing this, is using the curve() function, but it must contain “x” as an argument.
curve(-3 + 1.5 * x, 0, 50)
Find the slope by taking the derivative of the function. We call the derivative slope.
slope = D(expression(-3 + 1.5 * x), "x")
slope
[1] 1.5
The elasticity is \(\frac{dy}{dx}\frac{x}{y}\):
elasticity = slope * p/(-3 + 1.5 * p)
df=data.frame(q, p, e=elasticity)
A more advanced plot of the relationship. As we can see, the elasticity along a linear function is a non-linear relationship. Note that at p=2, q=0, and the elasticity is equal to \(\infty\). A supply elasticity is positive, hence we plot the elasticity from p=3.
plot(elasticity ~ p, type = "l", xlim = c(3, 50), ylim = c(0, 3))
title("Elasticity as a function of price")
abline(h = 1, col = 4, lty = 3)
text(16, 2, expression(frac(partialdiff(y), partialdiff(x)) == 1.5))
text(20, 1.5, expression(Elasticity == frac(partialdiff(y), partialdiff(x)) %*% frac(x, y)))
curve(-2 + 6 * (1/x), 1, 10)
D(expression(-2 + 6 * (1/x)), "x") # The derivative
-(6 * (1/x^2))
curve(-(6 * (1/x^2)), 1, 10, main="Plot of the derivative")
#install.packages('Ryacas')
library(Ryacas)
yacas("Limit(x,0)(-(6 * (1/x^2)))") # The derivative is largest as x goes to zero -> -Infinity
expression(-Inf)
yacas("Limit(x,Infinity)(-(6 * (1/x^2)))") # x -> Infinity, derivative -> 0
expression(0)
fn = yacas(expression(-(6 * (1/x^2)))) # Define the derivative as a function (fn)
Eval(fn, list(x = 5)) # Evaluate the function at x=5
[1] -0.24
A.3
A.5
curve(0.5 + 0.2 * log(x), 1, 50)
dx = D(expression(0.5 + 0.2 * log(x)), "x")
dx
0.2 * (1/x)
require(pacman)
Loading required package: pacman
p_load(mosaic)
f <- makeFun(0.5 + 0.2 * log(x) ~ x)
f(x = 49)
[1] 1.278364
curve(f(x), 0,50, main="function")
g <- makeFun(0.2 * (1/x) ~ x)
g
function (x)
0.2 * (1/x)
g(x = 49) # The slope at t=49
[1] 0.004081633
curve(g(x), 0,50, main="slope")
g(x = 49) * 49/f(x = 49) # The elasticity at t=49
[1] 0.15645
e <- function(x) { g(x)*x/f(x)}
e(49)
[1] 0.15645
curve(e(x), 0,50, main="elasticity")