-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1-1One-Variable Optimization.Rmd
More file actions
90 lines (66 loc) · 2.01 KB
/
1-1One-Variable Optimization.Rmd
File metadata and controls
90 lines (66 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
---
title: "1-1-1 One-Variable Optimization"
author: "Hao Li"
date: "01/10/2019"
output:
pdf_document: default
html_document: default
word_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
This R repository is for demonstration of algorithms involved in the book
Mathematical Modeling (4th Edition) written by Prof. Mark. M. Meerschaert
```{r}
#re-edited Mar 2021
#This R repository is for demonstration of algorithms involved in the book
#Mathematical Modeling (4th Edition) written by Prof. Mark. M. Meerschaert
#coded, edited and tested by Hao Li during Dec. 2018 - Jan. 2019.
#1-1-1
#1 var optimization Symbolic&Numeric calculations and visualization with R
#
#1 Variable Optimization sample question
#-------------------------------------------------------------------------
#AIM: FIND P MAX
#ASSUMPTIONS
library(Ryacas)
w<-expression(200+5*t1)
yac_assign(w,"w")
p<-expression(0.65-0.01*t1)
yac_assign(p,"p")
C<-expression(0.45*t1)
yac_assign(C,"C")
R<-expression(p*w)
yac_assign(R,"R")
P<-expression(R-C)
yac_assign(P,"P")
P = yac("P",rettype = "expr")
P
dPdt = yac(paste0("D(","t1",")", as.character(P)),rettype = "expr")
d2Pdt2 = yac(paste0("D(","t1",")", as.character(dPdt)),rettype = "expr")
sln = yac(paste0("Solve(",dPdt,",t1)"),rettype = "str")
sln
l = nchar(sln)
l
sln_str = gsub("==","=",substr(sln,2,l-1))
sln_expr = parse(text = sln_str)
sln_expr
eval(sln_expr)
(P.max = eval(P))
eval(dPdt)
eval(d2Pdt2)
#-------------------------------------------------------------------------
plot(0:20,eval({t1=0:20;P}),type="l",xlab="Time(D)",ylab="Profit($)")
title("Profit~Time")
abline(v=8,untf=FALSE)
#------------------------------------------------------------------------
sln_expr = parse(text = sln_str)
sln_expr
eval(sln_expr)
t.opti=t1
P.max=eval({t1<-t.opti;P})
list("t.opti"=t.opti,"P.max"=P.max)
#detach("package:Ryacas", unload=TRUE)
#detach("package:Deriv", unload=TRUE)
```