-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewton Raphson Method.R
51 lines (38 loc) · 1.35 KB
/
Newton Raphson Method.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
### INITIAL INFORMATION FOR LABORATORY SOLUTION
presetError <- 0.0001
xi <- 2
### FUNCTIION CREATED TO EVALUATE THE PROPOSED FUNCTION GIVEN A X VALUE
evaluateFunction <- function(xValue) {
return (7*xValue**3 + 2*xValue**2 - 3*xValue - 1)
}
### FUNCTIION CREATED TO EVALUATE THE DERIVATE OF THE PROPOSED FUNCTION GIVEN A X VALUE
evaluateDerivate <- function(xValue) {
return (21*xValue**2 + 4*xValue - 3)
}
percentageRelativeError <- 101
cont <- 1
while(percentageRelativeError >= presetError && cont <= 25){
if (cont != 1){
xi <- xiPlusOne
}
xiPlusOne <- xi - ((evaluateFunction(xi))/(evaluateDerivate(xi)))
Fxi <- evaluateFunction(xi)
FprimeXiPlusOne <- evaluateDerivate(xiPlusOne)
percentageRelativeError <- abs((xiPlusOne - xi)/xiPlusOne)*100
### PROCEDURAL OUTPUT
cat("Iteration", cont, "\n")
cat("xi ->", xi, "\n")
cat("xi plus one ->", xiPlusOne, "\n")
cat("Fxi ->", Fxi, "\n")
cat("FprimeXiPlusOne ->", FprimeXiPlusOne, "\n")
cat("percentageRelativeError ->", percentageRelativeError, "\n\n")
cont <- cont + 1
}
### SOLUTION OUTPUT
if (cont < 25){
cat("The percentage real error is:", percentageRelativeError, "\n")
cat("The result is finally:", xi)
}else{
cat("The program has not been able to calculate the result, it has exceeded 25 iterations")
}
### STUDENT: CRISTIAN JULIAN MUNOZ BUENAHORA - 000430876