Beispiel #1
0
To determine the time at which velocity is 1000m/sec
'''

u = 2200  #given
v = 1000  #given
m = 160000  #given
q = 2680  #given
g1 = 9.81


def g(t):
    return u * math.log(m / (m - q * t)) - g1 * t - v


_plot.graph(g, xl=10, xu=50, title="g(t)", ylabel="g", xlabel="t")
#Graph reveals root to be between 25 and 30

_plot.graph(g, xl=25, xu=30, title="g(t)", ylabel="g", xlabel="t")
#Graph reveals root between 25 and 26, approximately 25.9

n = 4
es100 = _error.es100(n)

print('false position:')
R1 = _roots1.falsepos(g, xl=10, xu=50, es100=es100, debug=True, tab=10)
print("R = {}".format(R1))

print("\nusing bisection method:")
R1 = _roots1.bisect(g, xl=10, xu=50, es100=es100, debug=True, tab=12)
print("R = {}".format(R1))
Beispiel #2
0
import math
import _plot, _numinteq


def f(x):
    return x * math.exp(2 * x)


#true integral of f(x)
def Itf(x):
    return .25 * math.exp(2 * x) * (2 * x - 1)


#evaluation range
x0 = 0
xn = 3

#plot function
_plot.graph(f, xl=x0, xu=xn, title="f(x) = x.e^2x")

#true value of integral of f(x) between x0 and xn
tv = Itf(xn) - Itf(x0)
print("tv = {}".format(tv))

#approx. value of integral of f(x) using romberg's algorithm
av = _numinteq.romberg(f, x0, xn, es100=.5, tv=tv, debug=True)
print("av = {}".format(av))

et = (tv - av) / tv
print("et = {}".format(et))
# script: ex-bairstow-poly3.py
# bairstow's method
# 3. f(x) = -2 + 6.2x - 4x2 + 0.7x3

import matplotlib.pyplot as plt
import _plot, _poly, _polyroots2_bairstow

# a = [-2, 6.2, -4, 0.7]

def f(x): return _poly.eval(a, x)  # evaluate polynomial a as a function

xl, xu = -5, 5
_plot.graph(f, xl, xu, title="y = {}".format(_poly.tostr(a)))

prec = 4

xr1, xr2 = _polyroots2_bairstow.bairstow(a, r=1, s=1, es100=.5 * 10 ** (2 - prec), debug=True, tab=11, precision=prec)
print("xr1 = {:.{p}}\nxr2 = {:.{p}}".format(xr1, xr2, p=prec))

# plot roots
xr = [xr1, xr2]  # add additional roots here
_plot.graph(f, xl, xu, title="y = {}".format(_poly.tostr(a)), show=False)
axes = plt.gca()  # get current axes object and obtain dimensions
left, right = axes.get_xlim();
width = right - left
down, up = axes.get_ylim();
height = up - down
for i in range(len(xr)):
    plt.annotate("xr{} = {:.{p}}".format(i + 1, xr[i], p=prec),
                 xy=(xr[i].real, 0), xytext=(xr[i].real + width / 25, (i + 1) * height / 25),
                 arrowprops=dict(width=.25, headwidth=4, headlength=9))
def dfdx(i,x):
    return {
        0: f(x),
        1: -math.sin(x),
        2: -math.cos(x),
        3: math.sin(x),
        4: math.cos(x)
    }.get(i,0)

xi = 0.25*math.pi
h = (1/12) * math.pi

#plot function
x0 = 0;
xn = xi + 1;
_plot.graph(f, xl=x0, xu=xn, title="f(x) = cos(x)")
def df(x): return dfdx(1,x)
_plot.graph(df,xl=x0, xu=xn, title="f(x) = -sin(x)")

tv = dfdx(1,xi)
print("tv = {}".format(tv))

print("************* Regular 1st finite differences ****************")

fav = _series.fwddif1(f, xi, h)
bav = _series.bwddif1(f, xi, h)
cav = _series.cntdif1(f, xi, h)
print("Forward av = {}\nBackward av = {}\nCentered av = {}".format(fav, bav, cav))

etf = abs(1 - fav/tv)
etb = abs(1 - bav/tv)
    return {
        0: f(x),
        1: 1 / x,
        2: -1 / (x**2),
        3: 2 / (x**3),
        4: -6 / (x**4)
    }.get(i, 0)


xi = 25
h = 2

#plot function
x0 = 20
xn = xi + 1
_plot.graph(f, xl=x0, xu=xn, title="f(x) = log(x)")


def df(x):
    return dfdx(1, x)


_plot.graph(df, xl=x0, xu=xn, title="f(x) = 1/x")

tv = dfdx(1, xi)
print("tv = {}".format(tv))

print("************* Regular 1st finite differences ****************")

fav = _series.fwddif1(f, xi, h)
bav = _series.bwddif1(f, xi, h)
Beispiel #6
0
# Bracketing Method
# Excercise 5.3

import math
import _plot, prob5_3_roots


# function f(X) = – 25 + 82x – 90x2 + 44x3 – 8x4 + 0.7x5
def f(x):
    return -25 + 82 * x - 90 * x**2 + 44 * x**3 - 8 * x**4 + 0.7 * x**5


#graphical method

_plot.graph(f, xl=0.5, xu=1.0)
# visual inspection reveals that root lies between 0.5 and 0.6

_plot.graph(f, xl=0.5, xu=0.6)
# visual inspection reveals that root is approx 0.5794

print("Bisection Method: ")
xr_b = prob5_3_roots.bisect(f, xl=.5, xu=1.0, debug=True)
print(f"The Root Using Bisection method is : {xr_b} ")

print("========================================================")

print("False Position Method: ")
xr_f = prob5_3_roots.falsepos(f, xl=.5, xu=1.0, debug=True)
print(f"The Root Using False Position method is : {xr_f:.4}")
import math
import _plot, _roots2

def f(x): return x**3 - 6 * x**2 + 11 * x - 6.1

_plot.graph(f, xl=0, xu=5)
#visual inspection reveals root between x = 1 and x = 4

_plot.graph(f, xl=1, xu=4)
_plot.graph(f, xl=3, xu=3.5)

print('newton method:')
def df(x): return 3 * x**2 - 12 * x + 11
_roots2.newton(f, df, x0=3.5, debug=True)

print('\nsecant method:')
_roots2.secant(f, xi1=2.5, xi2=3.5, debug=True)

print('\nmodified secant method:')
_roots2.modified_secant(f, x0=3.5, p_f=0.01, debug=True)
Beispiel #8
0
import math
import _plot, _numinteq


def f(x):
    return (x + 1 / x)**2


#true integral of f(x)
def Itf(x):
    return (1 / 3 * x**3) - 1 / x + 2 * x


#evaluation range
x0 = 1
xn = 2

#plot function
_plot.graph(f, xl=x0, xu=xn, title="f(x) = (x + 1/x)^2 ")

#true value of integral of f(x) between x0 and xn
tv = Itf(xn) - Itf(x0)
print("tv = {}".format(tv))

#approx. value of integral of f(x) using romberg's algorithm
av = _numinteq.romberg(f, x0, xn, es100=.5, tv=tv, debug=True)
print("av = {}".format(av))

et = (tv - av) / tv
print("et = {}".format(et))