Generic Symbolic function as input in actual funciton
So I have this assignment for university, and I'm trying to write a generic Newton's method which will take any symbolic function as input. I just don't know how to declare this on the function's inputs. Here's the function I want as input:
f(x) = 14 * x * exp(x-2) - 12 * exp(x-2) - 7 * x^3 + 20 * x^2 - 26 * x + 12
and here's the function I want to create:
def Newton_Raphson(foo, start):
dfoo = diff(foo)
ddfoo = diff(dfoo)
t0 = time.time()
while foo(start) * ddfoo(start) <= 0:
start += 10^-6
NR = start - foo(start) / dfoo(start)
noNR = 1
while abs(foo(NR)) > 10^-6:
NR = start - foo(start)/dfoo(start)
start = NR
error = (NR - start)
noNR += 1
t1 = time.time()
tNR = t1 - t0
return NR, noNR, error, tNR
Any ideas on this? Also do you have any other suggestions on this code?
Side suggestion: semicolons are not needed. Removing them makes code more readable.
Old habits don't change ๐๐ Yes I will remove them.
Replacing
a = a + b
bya += b
also increases readability.Body of the
def
needs indenting.If using
time
, addimport time
as first line of the function or before the function.It's indented, for some reason the indent disappeared while copying the code.
Yes time is imported and everything.
This function, if used with exact input, will keep everything exact, which might be very slow.
Floating-point will be hugely faster, with the drawback of being not certified.
The best compromise will probably be to use Arb.