- #1
Hi Im Paul
- 2
- 0
Hello, I am assigned to write a code using bisection method (aka binary search method)
The equation is 10sin(x) - x^3 - N where N = 1,2,3,4,5,6,7
My code is
Mod note: added code tags, which brought back the indentation.
When I enter this, my x value is correct, however my y value is very wrong. I have tried every variation of y, and I get it wrong or an error. please help
The equation is 10sin(x) - x^3 - N where N = 1,2,3,4,5,6,7
My code is
Python:
from math import sin
def neg(a, b):
return a*b> 0
def bisectx(funcx, lowx, highx,n):
assert not neg(funcx(lowx,n), funcx(highx,n))
for i in range(20):
midx = (lowx + highx)/2.0
if neg(funcx(lowx,n), funcx(midx,n)):
lowx = midx
else:
highx = midx
return midx
def h(x,n):
return 10*sin(x) - x**3 - n
def pos(c, d):
return c*d<0
def bisecty(funcy, lowy, highy,n):
assert pos(funcy(lowy,n), funcy(highy,n))
for i in range(100):
midy = (lowy - highy)/2.0
if pos(funcy(lowy,n), funcy(midy,n)):
lowy = midy
else:
highy = midy
return midy
def l(y,n):
return 10*sin(y) - y**3 - n
for i in [1,2,3,4,5,6,7]:
x = bisectx(h, -3, 4, i)
y = bisecty(l, -3, 4, i)
print(x, h(x,i))
print(y, l(y,i))
When I enter this, my x value is correct, however my y value is very wrong. I have tried every variation of y, and I get it wrong or an error. please help
Last edited by a moderator: