# Solving a system of equations using R

I'm trying to use R for my Macro practise and model solve for the equilibrium price where the inverse demand curve PD(q) and supply curve P.

How do i do this? Thanks!

• Comments are not for extended discussion; this conversation has been moved to chat.
– 1muflon1
May 17 at 16:15

Note 1.: It is rude to edit a question after it was answered; I had to make significant edits to make my answer consistent.

Note 2.: this is not a system of equations. There are two functions defined, but only one equation: $$P_D(q) - P_S(q) = 0$$

What helps here is that inverse demand is decreasing in quantity while inverse supply is increasing. So given any $$q$$, if $$P_D(q) > P_S(q)$$ we know that $$q$$ is under the equilibrium value of $$q^*$$, and if $$P_D(q) < P_S(q)$$ then $$q > q^*$$.

You can also narrow down the range $$q^*$$ can be found in. Clearly $$q^* \geq 0$$.

Also $$P_D(q)$$ should not dip into negative territory, so you can find a very large $$\bar{q}$$ for which $$P_D(q) \leq 0$$. One can do this easily by starting from $$\bar{q} = 1$$, evaluating $$P_D(\bar{q})$$, and doubling $$\bar{q} = 1$$ iff $$P_D(\bar{q}) > 0$$.

Given the interval $$I = [0, \bar{q}]$$ which we know contains $$q^*$$, you can now apply interval halving.

An algorithm for approximating $$q^*$$ with a desired level of precision $$\epsilon$$:

Define the interval $$I = [0, \bar{q}]$$.

Start of LOOP
Select the midpoint of the interval as a "guess" for $$q^*$$. (In the first iteration this is $$q = \bar{q}/2$$.)

Evaluate the statement $$P_D(q) > P_S(q)$$.
If true, then
$$q^*$$ should be smaller, the solution is in the lower half of the interval, so in the next iteration we will use that as our new interval $$I$$.
If false, then
$$q^*$$ should be larger, the solution is in the upper half of the interval, so in the next iteration we will use that as our new interval $$I$$.

Evaluate the statement new interval is very small, i.e. has length less than $$2\epsilon$$.
If true, then
select its midpoint and say that you have approximated $$q^*$$ with reasonable precision, end program.
If false, then
Go to start of LOOP.

• Thorough answer. But once you have $\bar q$, you can simply use optimize to minimize the absolute difference over the relevant interval---will work fine since the functions are presumably monotone. This is probably easier and more efficient than writing your own interval-halving algorithm.
– kyle
May 17 at 19:40
• @kyle I've never heard of this function before. (I mostly used R for econometrics.) If you were to write a short answer based on your comment (explaining what the function does) that would be lovely! May 17 at 21:35

This builds on @Giskard's answer above.

Once you know the range of feasible market-clearing quantities, $$q \in [ 0, \bar q ]$$, you can directly apply R's uniroot function (R manual), which searches a given interval for the zeros of a function.

# What are my demand and supply functions? 1 - q and q, because economics.
P_D <- function ( q ) { 1 - q }
P_S <- function ( q ) { q }

# Set @Giskard's upper bound for the search
qbar <- 1

# Find the equilibrium quantity
uniroot( function ( q ) { P_D( q ) - P_S( q ) }, lower = 0, upper = qbar )

##########
# OUTPUT #
##########
#
# $$root # [1] 0.5 # #$$f.root
# [1] 0
#
# $$iter # [1] 1 # #$$init.it
# [1] NA
#
# $estim.prec # [1] 0.5  After you find the intersection, verify that $.root == 0, or is within a tolerance of zero.

If you are lazy, you can set qbar <- 1e9 (for example) and it will still behave reasonably nicely. Do be cautious if you take this approach, in case your demand and supply functions are nonmonotone on economically-irrelevant intervals.

Per my comment on @Giskard's answer, you could also implement this search as

optimize( function ( q ) { abs( P_D( q ) - P_S( q ) ) }, interval = c( 0, qbar ) )


However, R has a built-in root-finding function (uniroot), so why not use it.