Beispiel #1
0
def c5p22():
    # Given Functions
    def force_grav(rho_object, v, g=9.8):
        return rho_object * v * g

    def force_buoy(rho_medium, v_displaced, g=9.8):
        return rho_medium * v_displaced * g

    def vol_sphere(rad):
        return 4.0 / 3 * np.pi * rad**3

    def vol_dry(rad, h):
        return (np.pi * h**2 / 3) * (3 * rad - h)

    def vol_disp(rad, h):
        return vol_sphere(rad) - vol_dry(rad, h)

    # Constants
    rho_object, rho_medium, rad = 200., 1000., 1.
    # args for solutions
    f = lambda h: force_grav(rho_object, vol_sphere(rad)) - force_buoy(
        rho_medium, vol_disp(rad, h))
    a, b = 0., 2.

    return {
        'bisection': num.bisection(f, a, b),
        'false_position': eng_form(num.false_position(f, a, b))
    }
Beispiel #2
0
def c5p7():

    f, a, b, *kwargs = [
        lambda x: np.log(x**2) - 0.7, 0.5, 2.0, {
            'maxSteps': 3
        }
    ]

    return {
        'bisection': num.bisection(f, a, b, **kwargs[0]),
        'false_position': num.false_position(f, a, b, **kwargs[0])
    }
Beispiel #3
0
def c6p19():
	# Impedance
	def z(r, c, l, w):
		return np.sqrt( r**(-2) + ( w * c - (w * l)**(-1) )**2 )**(-1)

	# Constants
	r, c, l, z_desired = 225., 0.6 * 10**(-6), 0.5, 100.

	# Args for solution
	f = lambda w: z(r, c, l, w) - z_desired
	x0, x1, dx = 210, 215, 10**(-4)

	return {'newton_raphson': num.newton_raphson( f,x0 ), 'secant_method': num.secant_method( f, x0, x1 ), 'modified_secant': num.modified_secant( f,x1,dx) }
Beispiel #4
0
def c6p19():
    # Impedance
    def z(r, c, l, w):
        return np.sqrt(r**(-2) + (w * c - (w * l)**(-1))**2)**(-1)

    # Constants
    r, c, l, z_desired = 225., 0.6 * 10**(-6), 0.5, 100.

    # Args for solution
    f = lambda w: z(r, c, l, w) - z_desired
    x0, x1, dx = 210, 215, 10**(-4)

    return {
        'newton_raphson': num.newton_raphson(f, x0),
        'secant_method': num.secant_method(f, x0, x1),
        'modified_secant': num.modified_secant(f, x1, dx)
    }
Beispiel #5
0
def c8p3():
    A = np.array([[0, -6, 5], [0, 2, 7], [-4, 0, -4]])

    b = np.array([[50], [-30], [50]])

    return {
        'Solution Vector': num.gauss_elimination(A, b),
        "Coefficient Transpose": np.transpose(A),
        "Coefficient Inverse": inv(A)
    }
Beispiel #6
0
def c6p21():
    # Trajectory
    def y(theta, x, v0, y0, g=9.8):
        return np.tan(theta) * x - (g * x**2) / (2 * v0**2 *
                                                 np.cos(theta)**2) + y0

    # Constants
    v0, x, y0, y1 = 30., 90, 1.8, 1.
    deg = (180. / np.pi)

    # Args for solution
    f = lambda theta: y(theta, x, v0, y0) - y1

    return {
        'newton_raphson th1': deg * num.newton_raphson(f, 0.4),
        'newton_raphson th2': deg * num.newton_raphson(f, 0.9),
        'secant_method th1': deg * num.secant_method(f, 0.4, 0.45),
        'secant_method th2': deg * num.secant_method(f, 0.9, 0.95)
    }
Beispiel #7
0
def c5p16():
	# Given functions:
	def rho(q, n, mu):
		return 1.0 / ( q * n * mu )

	def n( N, ni ):
		return 0.5 * ( N + np.sqrt( N**2 + 4 * ni**2 ) )

	def mu( T, T0, mu0 ):
		return mu0 * ( T / T0 )**(-2.42)

	# Constants
	T0, T, mu0, q, ni, rho_desired = 300., 1000., 1360., 1.7 * 10**(-19), 6.21 * 10**9, 6.5 * 10**6

	# args for solutions
	f = lambda N: rho( q, n(N,ni), mu(T,T0,mu0) ) - rho_desired
	a,b = 0., 2.5 * 10**10

	return {'bisection': eng_form( num.bisection( f,a,b ) ),'false_position':eng_form( num.false_position(f,a,b) ) }
Beispiel #8
0
def update():
    global slider
    global ax1
    global ax2
    global ax3

    # create new values that depends on slider
    graph = nm.NumMethods(xStart, xFinite, yStart, int(slider.val))
    graph.buildPlot()
    graph.eulerMethod()
    graph.improvedEulerMethod()
    graph.rungeKutta()
    graph.errors()

    # clear subplots
    ax1.clear()
    ax2.clear()
    ax3.clear()

    # set the grid
    ax1.grid(True)
    ax1.set_ylabel("y")
    ax1.set_xlabel("x")
    ax2.grid(True)
    ax2.set_ylabel("y")
    ax2.set_xlabel("x")
    ax3.grid(True)
    ax3.set_xlabel("x\n\n")
    ax3.set_ylabel("y")

    # plot new values
    ax1.set_title("Numerical Methods")
    ax1.plot(graph.x, graph.yEuler, "blue", label="Euler Method")
    ax1.legend(loc='upper right')
    ax1.plot(graph.x, graph.yImprEuler, "cyan", label="Improved Euler")
    ax1.legend(loc='upper right')
    ax1.plot(graph.x, graph.yRunge, "red", label="Runge-Kutta")
    ax1.legend(loc='lower left')

    ax2.set_title("Exact solution")
    ax2.plot(graph.x, graph.y, "black", label="Grapth")
    ax2.legend(loc='upper center')
    ax3.set_title("Errors")

    ax3.plot(graph.x, graph.errorEuler, "blue", label="Euler Method")
    ax3.plot(graph.x, graph.errorImprEuler, "cyan", label="Improved Euler")
    ax3.plot(graph.x, graph.errorRunge, "red", label="Runge-Kutta")

    ax1.xaxis.set_major_locator(mplt.ticker.MultipleLocator(base=0.5))
    ax1.yaxis.set_major_locator(mplt.ticker.MultipleLocator(base=0.5))
    ax2.xaxis.set_major_locator(mplt.ticker.MultipleLocator(base=0.5))
    ax2.yaxis.set_major_locator(mplt.ticker.MultipleLocator(base=0.5))
    ax3.xaxis.set_major_locator(mplt.ticker.MultipleLocator(base=0.5))
    ax3.yaxis.set_major_locator(mplt.ticker.MultipleLocator(base=0.5))
    plt.draw()
Beispiel #9
0
def c6p21():
	# Trajectory
	def y(theta, x, v0, y0, g=9.8 ):
		return np.tan( theta ) * x - (g * x**2) /( 2 * v0**2 * np.cos( theta )**2 ) + y0

	# Constants
	v0, x, y0, y1 = 30., 90, 1.8, 1.
	deg = ( 180. / np.pi )

	# Args for solution
	f = lambda theta: y( theta, x, v0, y0 ) - y1

	return { 'newton_raphson th1': deg * num.newton_raphson( f,0.4 ), 'newton_raphson th2': deg * num.newton_raphson( f, 0.9 ) ,'secant_method th1': deg * num.secant_method(f,0.4, 0.45 ), 'secant_method th2':deg * num.secant_method( f,0.9,0.95 )  }
Beispiel #10
0
def c5p16():
    # Given functions:
    def rho(q, n, mu):
        return 1.0 / (q * n * mu)

    def n(N, ni):
        return 0.5 * (N + np.sqrt(N**2 + 4 * ni**2))

    def mu(T, T0, mu0):
        return mu0 * (T / T0)**(-2.42)

    # Constants
    T0, T, mu0, q, ni, rho_desired = 300., 1000., 1360., 1.7 * 10**(
        -19), 6.21 * 10**9, 6.5 * 10**6

    # args for solutions
    f = lambda N: rho(q, n(N, ni), mu(T, T0, mu0)) - rho_desired
    a, b = 0., 2.5 * 10**10

    return {
        'bisection': eng_form(num.bisection(f, a, b)),
        'false_position': eng_form(num.false_position(f, a, b))
    }
Beispiel #11
0
def c5p22():
	# Given Functions
	def force_grav( rho_object, v, g=9.8 ):
		return rho_object * v * g

	def force_buoy( rho_medium, v_displaced, g=9.8 ):
		return rho_medium * v_displaced * g

	def vol_sphere( rad ):
		return 4.0/3 * np.pi * rad**3

	def vol_dry( rad, h ):
		return (np.pi * h**2 / 3) * (3 * rad - h)

	def vol_disp( rad, h ):
		return vol_sphere(rad) - vol_dry( rad, h )

	# Constants
	rho_object, rho_medium, rad = 200., 1000., 1.
	# args for solutions
	f = lambda h: force_grav( rho_object, vol_sphere( rad )) - force_buoy( rho_medium, vol_disp( rad,h ))
	a,b = 0.,2.

	return {'bisection': num.bisection( f,a,b ) ,'false_position':eng_form( num.false_position(f,a,b) ) }
Beispiel #12
0
def c8p3():
	A = np.array([[0,-6,5],[0,2,7],[-4,0,-4]])

	b = np.array([[50],[-30],[50]])

	return {'Solution Vector':num.gauss_elimination(A,b),"Coefficient Transpose": np.transpose(A),"Coefficient Inverse": inv(A) }
Beispiel #13
0
def c5p7():

	f,a,b,*kwargs = [lambda x: np.log( x**2 ) - 0.7,  0.5,  2.0, {'maxSteps':3}]

	return {'bisection':num.bisection( f,a,b,**kwargs[0] ), 'false_position':num.false_position( f,a,b,**kwargs[0] )}