The Student Room Group

Scroll to see replies

Reply 20
it is my code.. all the lecturer gave ups was the newton raphson method formula which is the formula i've put in my while loop. i've changed the diff to diff so that is an array also but i don't believe this is correct. my lecturer stated that we'd basically be using arrays for all the assessment exercises :frown:
Python would be so much easier.

Example:
def derivative (f, x, h):
return float((f(x + h) - f(x))) / h

def solve(f, x0, h, depth):
if depth > 0:
delta = f(x0) / derivative(f, x0, h)
return solve(f, x0 - delta, h, depth - 1)
else:
return x0
Reply 22
caaakeeey
Python would be so much easier.

Example:
def derivative (f, x, h):
return float((f(x + h) - f(x))) / h

def solve(f, x0, h, depth):
if depth > 0:
delta = f(x0) / derivative(f, x0, h)
return solve(f, x0 - delta, h, depth - 1)
else:
return x0


C would be so much easier.

Example:


double derivative( double(*f)(double), double x, double h )
{
return ( f( x + h ) - f( x ) ) / h;
}

double solve( double(*f)(double), double x0, double h, int depth )
{
if (depth > 0)
{
double delta = f(x0) / derivative( f, x0, h );
return solve( f, x0 - delta, h, depth - 1 );
}

return x0;
}


Algorithm is totally unchecked by me, I just wanted to see how much more difficult it would be to write your algortihm in C.
Reply 23
we're not allowed use python otherwise i'd be open to that idea - not that i've used that either :wink:

Latest

Trending

Trending