-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.R
85 lines (72 loc) · 2.2 KB
/
script.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
main = function()
{
a=0.1
b=2
h=0.001
E=0.01
f = function(x){
return(x-log(x, exp(1)))
}
fprime = function(x){
return(1-1/x)
}
iter=0
textpos = 2.4
x=seq(a,b,h)
ans=0
ymin=100000
while(1){
iter = iter + 1
plot(x,f(x), type="l", col="red", lwd="2",
ylim=c(-0.5,2.5), xlim=c(0,2))
grid()
aprime = fprime(a)
bprime = fprime(b)
a1=f(a)-aprime*a
a2=f(b)-bprime*b
abline(b=aprime,a=a1)
abline(b=bprime,a=a2)
xinter = (a2-a1)/(aprime-bprime)
finter = f(xinter)
if(finter < ymin){
xmin = xinter
ymin = finter
}
points(xinter, y=finter, lwd="2", pch=19)
lines(c(xinter,xinter),c(aprime*xinter + a1, finter),lty="dotted", lwd="3", col="blue")
text(0.8,textpos,paste("Iteration number = ", iter), adj = c(0,0))
text(0.8,textpos-0.2,paste("A = ", a), adj = c(0,0))
text(0.8,textpos-0.4,paste("B = ", b), adj = c(0,0))
text(0.8,textpos-0.6,paste("x = ", xinter), adj = c(0,0))
text(0.8,textpos-0.8,paste("f'(x) = ", fprime(xinter)), adj = c(0,0))
text(0.8,textpos-1.0,paste("f(x) = ", finter), adj = c(0,0))
text(1.5,textpos,paste("xmin = ", xmin), adj = c(0,0))
text(1.5,textpos-0.2,paste("ymin = ", ymin), adj = c(0,0))
if(fprime(xinter) > E){
b=xinter
}
else if(fprime(xinter) < -E){
a=xinter
}
else{
break
}
browser()
#Sys.sleep(1)
}
finter = f(xinter)
plot(x,f(x), type="l", col="red", lwd="2",
ylim=c(-0.5,2.5), xlim=c(0,2))
grid()
points(xinter, y=finter, lwd="3", pch=19)
lines(c(xinter,xinter,xinter, -1000),c(-1000, finter, finter, finter),lty="dotted", lwd="3", col="blue")
text(0.8,textpos,paste("Iteration number = ", iter), adj = c(0,0))
text(0.8,textpos-0.2,paste("A = ", a), adj = c(0,0))
text(0.8,textpos-0.4,paste("B = ", b), adj = c(0,0))
text(0.8,textpos-0.6,paste("x = ", xinter), adj = c(0,0))
text(0.8,textpos-0.8,paste("f'(x) = ", fprime(xinter)), adj = c(0,0))
text(0.8,textpos-1.0,paste("f(x) = ", f(xinter)), adj = c(0,0))
text(1.5,textpos,paste("xmin = ", xinter), adj = c(0,0))
text(1.5,textpos-0.2,paste("ymin = ", finter), adj = c(0,0))
}
main()