Conjunto de funções para métodos numéricos em Emacs Lisp (ELisp) (ainda em construção).
Última actualização: 2008-05-03 [16:50]
É dada permissão para copiar, distribuir e/ou modificar todos os programas
aqui disponibilizados nos
termos da GNU Free Documentation License Versão 3 ou qualquer outra versão
posterior publicada pela Free Software Foundation. Uma cópia da licença -
GNU Free Documentation License.
(defun fixedpoint (f x &optional tol)
"Find the fixed point of f, i. e., f(p)=p."
(when (null tol)
(setq tol 0.00001))
(let*
((oldx x)
(x (funcall f x))
)
(if (< (abs (- x oldx)) tol) x (fixedpoint 'f x tol))
))
(defun f (x) (cos x)) (fixedpoint 'f 1 .001) ; 0.7387603198742113
Notas:
É possível usar o código anterior de modo a construir o método de Newton, basta para isso definir como função iteradora a função
à custa da função
cujo zero se quer determinar, i. e., a
solução da equação
.
(defun bisection (f a b &optional tol)
"Solve the function f(x)=0 with bounds a and b and tolerance tol. a
and b need to bracket the solution."
(when (null tol)
(setq tol 0.00001))
(let*
((m (/ (+ a b) 2.0))
(fm (funcall f m))
(fa (funcall f a))
)
(if (< (abs fm) tol)
m
(if (> (* fa fm) 0.0)
(bisection 'f m b tol)
(bisection 'f a m tol)))))
(defun f (x) (- 2 (* x x))) (bisection 'f 1 3) ; 1.414215087890625
A ideia é construir um conjunto de funções que implementem o cálculo, e efectuem operações sobre vectores e matrizes, de uma forma semelhante aos procedimentos em Octave.
(defun inner (x y)
"Inner product of two vectors x and y."
(let ((i 0) (aux 0))
(while (< i (length x))
(setq aux (+ aux (* (nth i x) (nth i y))))
(setq i (1+ i)))
aux))
(defun sum (x)
"Sum of element of x."
(let ((i 0) (aux 0))
(while (< i (length x))
(setq aux (+ aux (nth i x)))
(setq i (1+ i)))
aux))
(defun sumsq (x)
"Sum of squares of elements x."
(let ((i 0) (aux 0))
(while (< i (length x))
(setq aux (+ aux (* (nth i x) (nth i x))))
(setq i (1+ i)))
aux))
1999-2008 (c) Tiago Charters de Azevedo São permitidas cópias textuais parciais/integrais em qualquer meio com/sem alterações desde que se mantenha este aviso.