simpson.ex: The Simpson Rule
- Posted by Alan Tu <ATU5713 at COMPUSERVE.COM> Dec 17, 1998
- 463 views
For all you calculists or whatever, the Simpson Rule for finding integrals in Euphoria! SIMPSON.EX will ask for A, B, and N (N must be even). There is one catch: the function f(atom x). This must be a global function in function.e. And it must be, well, euphoric. For example, BASIC recognizes the standard way of doing exponents on computer x^2 is x squared. But Euphoria does not. To write x squared, you must do power(x,2). x cubed would be power(x,3). Order of operations are consistent with standard algebraic practice. sin, cos, and tan are self-explanatory; Euphoria has these functions. All those who are interested in RAM programs (LRAM, MRAM, and RRAM), I've written those too. So, without further waiting, this is simpson.ex Last thing, speed does matter, so suggestions to improve speed and all other suggestions are welcome! Alan include get.e include function.e type even(integer x) return integer(0.5*x) end type atom delta, coeff, solution, real, answer object a, b, n even subd real = 0 clear_screen() puts(1,"Enter A: ") a = get(0) a = a[2] puts(1,"\nEnter B: ") b = get(0) b = b[2] puts(1,"\nEnter N: ") n = get(0) subd = n[2] delta = (b-a)/subd coeff = (1/3)*delta puts(1,"\n") for i = 0 to subd do solution = f(a+i*delta) if i = 0 or i = subd then real = real+solution elsif 0.5*i != floor(0.5*i) then real = real+4*solution elsif 0.5*i = floor(0.5*i) then real = real+2*solution end if end for answer = coeff*real puts(1,"The estimated integral is: ") print(1,answer) puts(1,"\nNote: Simpson's Rule often provides exact answers.") --begin sample function.e global function f(atom x) return 1/x end function