-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2-2Multivariable Optimization.R
More file actions
45 lines (39 loc) · 1.37 KB
/
2-2Multivariable Optimization.R
File metadata and controls
45 lines (39 loc) · 1.37 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
#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-2-2 Multivariable Optimization
#Plot contour with restrictions
#Load results from 1-2-1
#1. SOLVE THE EQUATIONS
(ym=matrix(c((-174),(-144)),ncol=1))
(B=rbind(c((-0.007),(-0.02)),c((-0.02),-0.007)))
x=solve(B,ym)
remove(ym)
remove(B)
x[1]#s
x[2]#t
y<-function(s,t) (339 - 0.01 * s - 0.003 * t) * s + (399-0.004*s-0.01*t) * t - (195 * s + 225 * t + 4e+05)
y(x[1],x[2])
#Visualization
library(plot3D)
x1=seq(from = 0, to = 10000, length.out = 100);x2=seq(from = 0, to = 10000, length.out = 100)
m = mesh(x=x1,y=x2)
z = y(m$x,m$y)
layout(matrix(1:2,1))
contour(x1,x2,z,xlab='x1',ylab='x2')
abline(v=x[1],untf=FALSE)
abline(h=x[2],untf=FALSE)
title("Contours of f(x1,x2)")
#Add the margins of domain there
abline(h=8000,col = 'red',untf=FALSE)
abline(v=5000,col = 'red',untf=FALSE)
abline(coef=c(10000,-1),col = 'red',untf=FALSE)
persp3D(x1,x2,z)
#3d Visualization with rgl and Plot3Drgl
library(plot3Drgl)
persp3Drgl(x1,x2,z)
grid3d(c('x+','y+','z+'))
grid3d(c('x-','y-','z-'))
sapply(c('x+','y+','z+'),axis3d)
#points3D(c(x[1],x[2],y(x[1],x[2])),col ='purple')
#lines3d(cbind(c(x[1],x[2],y(x[1],x[2])),c(x[1],x[2],0)))