Created
October 9, 2020 15:29
-
-
Save nmanumr/9651910bec53f556f7e11c3f9d3c8532 to your computer and use it in GitHub Desktop.
NC assignment Bisection method, false position
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
def r(x): | |
return str(round(x, 6)) | |
def bisection_print(fn, xl, xu, last_xm=None, last_err=None, iter=1): | |
out_str = f"\\textbf{{Iteration {iter}}}\n\n" | |
out_str += "\n\\vspace{20pt}\\textbf{Step 1}\n" | |
out_str += f"$$ x_{{l}} = {r(xl)}, x_{{u}} = {r(xu)} $$\n" | |
out_str += f"$$ f(x_{{l}}) = {r(fn(xl))} $$\n" | |
out_str += f"$$ f(x_{{u}}) = {r(fn(xu))} $$\n" | |
if iter == 1: | |
x = fn(xl) * fn(xu) | |
out_str += f"$$ f(x_{{u}}) \\times f(x_{{l}}) = {r(fn(xl))} \\times {r(fn(xu))} = {r(x)} $$\n" | |
if x < 0: | |
out_str += f"\nAs, $ f(x_{{u}}) \\times f(x_{{l}}) < 0 $ so $[{r(xl)}, {r(xu)}]$ bracket the roots.\n" | |
out_str += "\n\\vspace{20pt}\n\\textbf{Step 2}\n" | |
xm = (xl + xu) /2 | |
out_str += f"$$ x_{{m}} = \\frac{{x_{{l}} + x_{{u}}}}{{2}} = \\frac{{{r(xl)} + {r(xu)}}}{{2}} = {r(xm)} $$\n" | |
error = None | |
if last_xm: | |
out_str += "\n\\vspace{20pt}\n\\textbf{Error}\n" | |
error = abs((xm - last_xm) / last_xm) * 100 | |
out_str += f"$$ \\text{{Error}} = \\frac{{ {r(xm)} - {r(last_xm)} }}{{ {r(last_xm)} }} \\times 100 $$\n" | |
out_str += f"$$ \\text{{Error}} = {r(error)} \\% $$\n" | |
if error > .5: | |
return out_str | |
out_str += "\n\\vspace{20pt}\n\\textbf{Step 3}\n\n" | |
out_str += "Now, either $[x_{l}, x_{m}]$ or $[x_{m}, x_{u}]$ will bracket the roots.\n" | |
x = fn(xl) * fn(xm) | |
if x < 0: | |
out_str += f"$$ f(x_{{l}}) \\times f(x_{{m}}) = {r(fn(xl))} \\times {r(fn(xm))} < 0 $$\n" | |
out_str += f"\nSo, $[x_{{l}}, x_{{m}}] = [{r(xl)}, {r(xm)}]$ bracket the roots\n\n" | |
xu = xm | |
else: | |
out_str += f"$$ f(x_{{m}}) \\times f(x_{{u}}) = {r(fn(xm))} \\times {r(fn(xu))} < 0 $$\n" | |
out_str += f"\nSo, $[x_{{m}}, x_{{u}}] = [{r(xm)}, {r(xu)}]$ bracket the roots\n\n" | |
xl = xm | |
out_str += bisection_print(fn, xl, xu, last_xm=xm, last_err=error, iter=iter+1) | |
return out_str | |
def false_position(fn, xl, xu, last_xm=None, last_err=None, iter=1): | |
out_str = f"\\textbf{{Iteration {iter}}}\n\n" | |
out_str += "\n\\vspace{20pt}\\textbf{Step 1}\n" | |
out_str += f"$$ x_{{l}} = {r(xl)}, x_{{u}} = {r(xu)} $$\n" | |
out_str += f"$$ f(x_{{l}}) = {r(fn(xl))} $$\n" | |
out_str += f"$$ f(x_{{u}}) = {r(fn(xu))} $$\n" | |
if iter == 1: | |
x = fn(xl) * fn(xu) | |
out_str += f"$$ f(x_{{u}}) \\times f(x_{{l}}) = {r(fn(xl))} \\times {r(fn(xu))} = {r(x)} $$\n" | |
if x < 0: | |
out_str += f"\nAs, $ f(x_{{u}}) \\times f(x_{{l}}) < 0 $ so $[{r(xl)}, {r(xu)}]$ bracket the roots.\n" | |
out_str += "\n\\vspace{20pt}\n\\textbf{Step 2}\n" | |
xm = xu - (fn(xu)*(xl-xu)/(fn(xl)-fn(xu))) | |
out_str += f"$$ x_{{m}} = x_{{u}} - \\left[\\frac{{f(x_{{u}}) \\times (x_{{l}} - x_{{u}}) }}{{f(x_{{l}}) - f(x_{{u}})}}\\right] $$\n" | |
out_str += f"$$ x_{{m}} = {r(xu)} - \\left[\\frac{{{r(fn(xu))} \\times ({r(xl)} - {r(xu)}) }}{{{r(fn(xl))} - {r(fn(xu))}}}\\right] $$\n" | |
out_str += f"$$ x_{{m}} = {r(xm)} $$\n" | |
error = None | |
if last_xm: | |
out_str += "\n\\vspace{20pt}\n\\textbf{Error}\n" | |
error = abs((xm - last_xm) / last_xm) * 100 | |
out_str += f"$$ \\text{{Error}} = \\frac{{ {r(xm)} - {r(last_xm)} }}{{ {r(last_xm)} }} \\times 100 $$\n" | |
out_str += f"$$ \\text{{Error}} = {r(error)} \\% $$\n" | |
if last_err is not None and last_err < error: | |
return out_str | |
if iter > 5: | |
return out_str | |
out_str += "\n\\vspace{20pt}\n\\textbf{Step 3}\n\n" | |
out_str += "Now, either $[x_{l}, x_{m}]$ or $[x_{m}, x_{u}]$ will bracket the roots.\n" | |
x = fn(xl) * fn(xm) | |
if x < 0: | |
out_str += f"$$ f(x_{{l}}) \\times f(x_{{m}}) = {r(fn(xl))} \\times {r(fn(xm))} < 0 $$\n" | |
out_str += f"\nSo, $[x_{{l}}, x_{{m}}] = [{r(xl)}, {r(xm)}]$ bracket the roots\n\n" | |
xu = xm | |
else: | |
out_str += f"$$ f(x_{{m}}) \\times f(x_{{u}}) = {r(fn(xm))} \\times {r(fn(xu))} < 0 $$\n" | |
out_str += f"\nSo, $[x_{{m}}, x_{{u}}] = [{r(xm)}, {r(xu)}]$ bracket the roots\n\n" | |
xl = xm | |
out_str += false_position(fn, xl, xu, last_xm=xm, last_err=error, iter=iter+1) | |
return out_str | |
# Question 1 | |
print(bisection_print(lambda x: x**2 - 18, 4, 5)) | |
# Question 2 | |
print(false_position(lambda x: x**2 * abs(math.cos(x) * math.sqrt(x)) - 5, 2, 2.5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment