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
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)
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
P = makeFun(-100+40*q-q^2~q)
P(50)
## [1] -600
findZeros(((50-0.5*q)*q)-(100+10*q+0.5*q^2)~q)
## q
## 1 2.6795
## 2 37.3205