-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW#5.R
96 lines (80 loc) · 1.65 KB
/
HW#5.R
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
91
92
93
94
95
96
# JAMES HYER SOLEY
# NUMERICAL METHODS
# MATH 504
# 02/15/2015
# HOMEWORK #5
# Problem #2b
# Create function for the gradient
library(numDeriv)
# Create Banana Function Function
banana <- function(x){
ans <- 100*(x[2]-x[1]^2)^2+(1-x[1])^2
return(ans)
}
# Create function for gradient norm
norm <- function(grad){
gradnorm <- sqrt(sum(grad)^2)
return(gradnorm)
}
# Create Numerator of NM Algo
gradient <- function(x){
dfx.1 <- 2*(200*x[1]^3-200*x[1]*x[2]+x[1]-1)
dfx.2 <- 200*(x[2]-x[1]^2)
grad <- c(dfx.1,dfx.2)
return(grad)
}
# Newton's Method
newton <- function(x,error){
step <- 1
while (norm(gradient(x)) > error){
x.new <- x - (solve(hessian(banana,x))%*%gradient(x))
x <- x.new
step <- step + 1
}
return(c(x,step))
}
newton(c(4,4),.000001)
# Problem #2c
# Create function for gradient norm
norm <- function(grad){
gradnorm <- sqrt(sum(grad)^2)
return(gradnorm)
}
# Create Numerator of NM Algo
gradient <- function(x){
dfx.1 <- 10*(5*x^4-15*x^2+4)
grad <- c(dfx.1)
return(grad)
}
# Second Derivative of NW Algo for 1D
gradient.two <- function(x){
dfx.1 <- 100*x*(2*x^2-3)
grad <- c(dfx.1)
return(grad)
}
# Newton's Method
newton <- function(x,error){
step <- 1
path <- x
while (norm(gradient(x)) > error){
x.new <- x - (gradient(x)/gradient.two(x))
path <- c(path,x.new)
x <- x.new
step <- step + 1
}
return(c(x,step))
}
newton( 3,.000001)
newton(-3,.000001)
plot(ans, ylim=c(-3,3))
lines(ans.2)
# Problem #4c
# Calculate the Eigenvalues and Vectors of A
A <- matrix(c(2,6,14,6,10,13,14,13,12), nrow=3)
A
eigen(A)
Q <- eigen(A)$vectors
Q
D <- diag(eigen(A)$values,nrow=3,ncol=3)
D
Q%*%D%*%t(Q)