## Import library
library(mosaic)
## Registered S3 method overwritten by 'mosaic':
##   method                           from   
##   fortify.SpatialPolygonsDataFrame ggplot2
## 
## The 'mosaic' package masks several functions from core packages in order to add 
## additional features.  The original behavior of these functions should not be affected by this.
## 
## Attaching package: 'mosaic'
## The following objects are masked from 'package:dplyr':
## 
##     count, do, tally
## The following object is masked from 'package:Matrix':
## 
##     mean
## The following object is masked from 'package:ggplot2':
## 
##     stat
## The following objects are masked from 'package:stats':
## 
##     binom.test, cor, cor.test, cov, fivenum, IQR, median, prop.test,
##     quantile, sd, t.test, var
## The following objects are masked from 'package:base':
## 
##     max, mean, min, prod, range, sample, sum
library(mosaicCalc)
## 
## Attaching package: 'mosaicCalc'
## The following object is masked from 'package:stats':
## 
##     D
library(ggplot2)
## Find Revenue
P=makeFun(50-0.5*q~q)
P
## function (q) 
## 50 - 0.5 * q
C=makeFun(100+10*q+0.5*q^2~q)
C
## function (q) 
## 100 + 10 * q + 0.5 * q^2
R=makeFun((50-0.5*q)*q~q)
R
## function (q) 
## (50 - 0.5 * q) * q
## Find Profit
P=makeFun(-100+40*q-q^2~q)
P
## function (q) 
## -100 + 40 * q - q^2
## Make graph C(q) & R(q)
ggplot()+
  geom_function(fun=C, col="black", lwd=1)+
  geom_function(fun=R, col="yellow", lwd=1)+
  xlim(1, 100)+
  labs(title="Graph", x="number", 
       y="Profit")+
  geom_text(aes(30, 135, label = "C"), color="black")+
  geom_text(aes(20, 900, label="R"), color="black")

## Darative
MR= D(R(q)~q)
MR
## function (q) 
## 50 - q
MP= D(P(q)~q)
MP
## function (q) 
## 2 * (20 - q)
MC= D(C(q)~q)
MC
## function (q) 
## q + 10
## Find MC(q), MC is darative of C
MC=makeFun(q+10~q)
MC
## function (q) 
## q + 10
## Find MR(q), MR is darative of R
MR=makeFun(50-q~q)
MR
## function (q) 
## 50 - q
## Find MP(q), MP is darative of P
MP=makeFun(40-2*q~q)
MP
## function (q) 
## 40 - 2 * q
## Make graph of MC & MR
ggplot()+
  geom_function(fun=MC, col="blue")+
  geom_function(fun=MR, col="pink")+
  xlim(1, 100)+ 
  labs(title = "Graph of MC(q) and MR(q)")

## make graph of MP
ggplot()+
  geom_function(fun=MP, col="red")+
  xlim(1,100)

## Find q make the most profit
P= D(P(q)~q)
P
## function (q) 
## 2 * (20 - q)
findZeros(P(q)~q)
##    q
## 1 20
## Find the most profit
P=makeFun(-100+40*q-q^2~q)
P(10)
## [1] 200
## Find q that break even point
findZeros(((50-0.5*q)*q)-(100+10*q+0.5*q^2)~q)
##         q
## 1  2.6795
## 2 37.3205