-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lisp
103 lines (93 loc) · 1.71 KB
/
main.lisp
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
97
98
99
100
101
102
103
(defun chords (f a b)
(-
b
(/
(*
(funcall f b)
(- b a)
)
(-
(funcall f b)
(funcall f a)
)
)
)
; (/
; (-
; (*
; a
; (funcall f b)
; )
; (*
; b
; (funcall f a)
; )
; )
; (-
; (funcall f b)
; (funcall f a)
; )
; )
)
(defun chordsRecursive (f a b eps)
(setq x (chords f a b))
(if (> (abs (- x b)) eps)
; (return-from chordsRecursive (chordsRecursive f a x eps))
(setq x (chordsRecursive f a x eps))
)
(return-from chordsRecursive x)
)
(defun chordsLinear (f a b eps)
(setq x (chords f a b))
(setq it 0)
(loop for i from 1
while (> (abs (- x b)) eps)
do
(setq b x)
(setq x (chords f a b))
(setq it (+ it 1))
)
(return-from chordsLinear x)
)
(defun function1(x)
(+
(* x x)
(* 4 (sin x))
)
)
(defun function2(x)
(sin x)
)
(defun function3(x)
(*
(cos x)
2
)
)
(write-line "x^2 + 4 * sin(x)")
(write-line "Linear:")
(setq result (chordsLinear 'function1 -1 1 0.00000001))
(write result)
(format t "~C" #\linefeed)
(write-line "Recursive:")
(setq result (chordsRecursive 'function1 -1 1 0.00000001))
(write result)
(format t "~C~C" #\linefeed #\linefeed)
(write-line "sin(x)")
(write-line "Linear:")
(setq result (chordsLinear 'function2 -1 1 0.00000001))
(write result)
(format t "~C" #\linefeed)
(write-line "Recursive:")
(setq result (chordsRecursive 'function2 -0.5 1 0.00000001))
(write result)
(format t "~C~C" #\linefeed #\linefeed)
(write-line "2 * cos(x)")
(write-line "Linear:")
(setq result (chordsLinear 'function3 4.6 5 0.00000001))
(write result)
(format t "~C" #\linefeed)
(write-line "Recursive:")
(setq result (chordsRecursive 'function3 4.6 5 0.00000001))
(write result)
(format t "~C~C" #\linefeed #\linefeed)