Computational Tools for Economics and Management Assignment 1 The User-guide Valentina Antoniazzi (832424) Nora Grammar Piros (832595) Eder Belluzzo (832416) Calculating equilibrium Price and Quantity, Producer and Consumer Surplus Suppose the market demand for cranberries is given by the equation Qd=500-4P, while the market supply curve for cranberries (when P is ≥50) is described by the equation Qs=-100+2P, where P is the price of cranberries expressed in dollars per barrel, and quantity (Qd or Qs) is in thousands of barrels per year. Problem. • At what price and quantity is the market for cranberries in equilibrium? • Consider the two non-linear functions, P=-Q2+20 and P=-Q2+4Q+16 find their intersections. • Find then the consumer surplus and the producer surplus Solution Since we are interested in the equilibrium price and quantity, we then need to look for the intersection between the demand and supply curve. Since it is a system of two linear equations, one possible way to find it is by constructing a matrix by first rearranging the original demand and supply curve in the following way: Qd +4P = 500 Qs -2P = -100 1 4 500 1 -2 - 100 A b What we do here is to create a matrix , where in the left side (A), we find the values corresponding to and Q, and on the right side (b) we find the constant of the two functions. Calculating it with R… A<-matrix(c(1,4,1,-2),2,2,byrow=T) A [,1] [,2] [1,] 1 4 [2,] 1 -2 b<-c(500,-100) solve(A,b) 100 100 => Q*=100 and P*=100 In letters… Dear R, we need to solve a system of two equations and we need your help. Please create a matrix of two rows and two columns, with the data I provide you, remember to read it by rows. We also give you the vector b. Then use “solve “ for A and b to find the result. R responds: it’s 100, 100 ! From these computations we find out that the equilibrium price, P* is equal to 100, as well as the equilibrium quantity Q* is also equal to 100. Note: you can use this procedure only if it is a system of two linear equations We only believe in what we see! To make sure that the result is correct and to see the patterns of the demand and the supply curve we need to make a graph so to explain it also visually. Calcualting it with R demand<-function(Q)-Q/4+500/4 curve(demand,0,550,xlab="Quantity",ylab="Price ",main=”Demand and Supply",col=1) text(500,20,"Demand") supply<-function(Q)Q/2+100/2 curve(supply,col=4,add=T) text(200,120,"Supply") text(100,110,"E") abline(h=100,v=100,lty=2) R kindly responds with a graph Comparing the graph and the results given by the matrix we can see that there is indeed consistency, also in the graph both P* and Q* are equal to 100 ! Note! If you use the function plot, the x axes will go from 0 to 1. If you want the real values of the function instead of a percentage, you should use the function “curve” and specify the interval you want to have displayed. In letters…. Dear R, we also need you to show us graphically those two equations. This is our demand function: -Q/4+500/4 we need you to graph it, and call it “Demand”, writing on the x axis “Quantity”, on the y axis “Price” and we also need a nice title that says “Demand and Supply”. Please do the same with the supply function adding it to the previous graph and calling it “Supply”. we would also like to see the ablines passing through the equilibrium point (100.100). Thanks Non-Linear Equations F <-function(Q) -(Q)**2+20 curve(F,0,2,col=2) G <-function(Q) -Q**2 +4*Q+16 curve(G,0,2,add=T) H <-function(Q) F(Q)-G(Q) curve(H,0,2,add=T) Note! also H is a function! Do not forget to say F<-function(Q)……. uniroot(H,c(0,2)) curve(H,0,2) abline(h=19,v=1,lty=2) points(1,19,pch=19) text(0.95,19.2,"E") Plugging x =1 in the function F <-function(Q) -(Q)**2+20 F(1) [1] 19 To have a further proof we plug it in the other function as well G <-function(Q) -Q**2 +4*Q+16 G(1) [1] 19 If the equations are not linear, as we have already said, it is not possible to use the matrix to find the intersection point of the two equations, but the following procedure can help us finding it! Having the two equations F(Q) and G(Q), we call “H(Q)” the difference between F(Q) and G(Q) H(Q)=F(Q)-G(Q) With the function “uniroot” we find where the two equations equal out. Uniroot gives us the value of Q for which this happens but we can easily find the value of P plugging it into one of the two functions. Finding Producer and Consumer Surplus We now have to find the consumer and producer surplus. The consumer surplus is the area BEC, while the producer surplus is the area enclosed in the triangle ABE. From the demand equation: d <-function(Q) -(Q)**2+20, and the supply equation: s <-function(Q) -(Q-2)**2+20, we draw a graphical representation using the R command “ curve” curve(s,0,2,col=2,ylab="Price",xlab="Quantity",lwd=3,main="Supply and Demand Equilibrium") curve(d,0,2,col=4,lwd=3,add=T) This is our initial graph. Now we add our abline and grid, and this will improve our graph, and show better the area of consumer and producer surplus. A common mistake in this step, is on the abline, we should write it as a formula because in this way we would be able to color the areas. Also, we should add points and name it. line <- function(Q)19+0*Q) curve(line,lwd=3,add=T) grid(col=1) text(0.05,16,"A") text(1,19.2,"E") text(0.05,19.1,"B") text(0.05,19.9,"C") points(1,19,pch=19) points(0,16,pch=19) points(0,19,pch=19) points(0,20,pch=19) Using the function “polygon” In order to color, we will be using the R command “polygon”. To do so, we must create a vector, in this case x, that would be a sequence of points, from a specific point to another and length. This vector, will create determine and unify the points between 2 lines using a reverse path. On our case, we define the vector x, a starting point, the reverse function and the final point. x <- seq(0,1,len=10) polygon(c(x,0),c(d(x),19),col="pink") polygon(c(x,0),c(s(x),19),col="light blue") And, our final touch will be to reinforce the line since they fade once we color the areas. Also, we can name the specific areas. curve(g,0,2,col=4,lwd=3,add=T) curve(line,lwd=3,add=T) curve(f,0,2,col=2,lwd=3,add=T) text(0.4,19.4,"Consumer Surplus") text(0.4,18.4,"Producer Surplus") Calculating the producer and consumer surplus The final step, will be to calculate the surplus areas. The R function for such is “integrate”. We must define a function, and the interval area to be calculated. R will actually calculate the whole area under the specific curve, and this is a possible common mistake. The graph we have, have the y axis from 16 to 20. So, once we calculate the integrals, R will give values from zero up the number given by the function. This is why to must subtract the undesired area values given the rectangular area 19.(1,19) In the case of supply function, the value would be negative, and that's why we must multiply the value by (-1) and then add 19 to get a precise absolute value for the area. Another common mistake in this process, is that we can't just add or subtract the value, we must extract it from the integrate area value. integrate(d,0,1)$value-19 integrate(s,0,1)$value*(-1)+19 Consumer Surplus area= 19.6666667-19=0.6666667 Producer Surplus area= 17.66667*(-1)+19=1.33333 The End Dear User, We hope our guide has been useful. We thank you for your attention. Best wishes Valentina, Eder, Nora
© Copyright 2026 Paperzz