-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBisection.jl
80 lines (66 loc) · 1.92 KB
/
Bisection.jl
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
""" Simple Bisection Algorithm
This is a simple bisection algorithm that takes in a function and a bracket
and returns the root of the function with predefined tolerance 1e-8.
Args:
f: function
a: lower bound
b: upper bound
Returns:
iter: number of iterations
a: lower bound
b: upper bound
root: Average of a and b
err: error
"""
# Import Packages
import Pkg
Pkg.add("Plots")
Pkg.add("Roots")
Pkg.add("Printf")
Pkg.add("LaTeXStrings")
# Load Packages
using Printf
using Plots
using Roots
using LaTeXStrings
# Define Function to be Rooted
# We considered f(x) = x²-2, x ∈ [0,2] with the solution x = 1.4142135623730951
function f(x)
return x^2 - 2
end
# Interval
a = 0.0;
b = 2.0
# Bisection Method
function bisection(f,a,b)
@printf("__________________________________________________\n \n");
@printf("k a b Root Error \n");
@printf("__________________________________________________\n \n");
if f(a)*f(b)>0
println("Failed")
else
p = (a + b)/2;
i = 0
err = abs(f(p));
while err > 1e-8
i += 1
if f(a)*f(p)<0
b = p;
else
a = p;
end
p = (a + b)/2;
err = abs(f(p));
@printf("%1.0f %1.9f %1.9f %1.9f %1.9f \n", i, a, b, p, err)
end
return p
end
end
res = bisection(f,0,2)
k1 = find_zero(f, (0,2), Bisection())
x = range(0,stop=2,length=1000)
plot(x,f.(x),linewidth=3.0,xlabel=L"x",label=L"f(x)", legend=:topleft)
plot!([res],[0], markershape=:circle,markercolor=:red, markerstrokecolor=:black, markerstrokewidth=3,
label= "Root",markersize=10)
plot!([k1],[0], markershape=:star,markercolor=:blue, markerstrokecolor=:black, markerstrokewidth=3,
label= "Root by using Bisection Method from Roots.jl",markersize=6)