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(ggplot2)
library(Deriv)
library(mosaicCalc)
## 
## Attaching package: 'mosaicCalc'
## The following object is masked from 'package:stats':
## 
##     D

អនុគមន៍តម្លៃ

p = makeFun((50 - 0.5 * q)*q ~ 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

ប្រាក់ចំណេញ

P = makeFun(-100+40*q-q^2~q)
P
## function (q) 
## -100 + 40 * q - q^2

ក្រាបសម្រាប់ C(q) និង R(q)

ggplot() +
  geom_function(fun = C, col = "red", size = 1.1) +
  geom_function(fun = R, col = "blue", size = 1.1) +
  xlim(1, 100) +
  geom_text(aes(75, 4200), label = "C(q)", col = "red", size = 5) +
  geom_text(aes(85, 1000), label = "R(q)", col = "blue", size = 5)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

labs(x="Q",
      y="P")
## $x
## [1] "Q"
## 
## $y
## [1] "P"
## 
## attr(,"class")
## [1] "labels"

គណនាដេរីវេ

MC = makeFun(D(C(q) ~ q)) 
MR = makeFun(D(R(q) ~ q))  
MP = makeFun(D(P(q) ~ q))
MP
## function (q) 
## 2 * (20 - q)

ក្រាប MC & MR

ggplot() +
  geom_function(fun = MC, col = "green", size = 1.1) +
  geom_function(fun = MR, col = "purple", size = 1.1) +
  xlim(1, 100) +
  geom_text(aes(75, 25), label = "MC(q)", col = "green", size = 5) +
  geom_text(aes(85, 15), label = "MR(q)", col = "purple", size = 5)

labs(x="Q",
       y="P")
## $x
## [1] "Q"
## 
## $y
## [1] "P"
## 
## attr(,"class")
## [1] "labels"
ggplot()+
  geom_function(fun=MP, col="red")+
  xlim(1, 50)

## Find q make the most profit

P1= D(P(q)~q)
P1
## function (q) 
## 2 * (20 - q)
findZeros(P1(q)~q)
##    q
## 1 20

Find the most profit

P = makeFun(-100+40*q-q^2~q)
P(50)
## [1] -600

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