Ejemplo n.º 1
0
    def plot_N_iter_with_error( self ):
        N_iters, errors = self.N_iters, self.errors
        N_iter_with_error = Grapher( "V(r): N_iter vs. error", '', 'N_iter, the number of relaxations', 'error', 'upper right' )
        N_iter_with_error.add_data_scatter( N_iters, errors, 'Error = Sum(squared differences of phi[i][j], between iterations' )
#        N_iter_with_error.ax.set_ylim( 0, 2e-5 )
        N_iter_with_error.save_to_file( '/tmp/N_iter_with_error2.png' )
        N_iter_with_error.show_figure()
Ejemplo n.º 2
0
# construct pendulum objects
pendula = []
for omega_D in driving_frequencies:
    pendula.append( Pendulum( omega_D, alpha_D, length, gamma, linear ) )

nperiods = 10     # no of oscillation periods
T = 2 * pi * nperiods    # update this to work with omega_D, and eventually many values of omega_D to be graphed together.
#TODO: remove the following after debugging
T = 100

for method_class in ODE_Solver_v3.EulerCromer, ODE_Solver_v3.RungeKutta2:
    npoints_per_period = 500
    n = npoints_per_period * nperiods
    t_points = linspace( 0, T, n + 1 )      # should I make this a numpy array?
    u0_graph = Grapher( 'Theta(t)', '', 't (seconds)', 'theta (radians)', 'bottom right' )
    #title, subtitle, x_label, y_label, legend_loc 
    u1_graph = Grapher( 'Omega(t)', '', 't (seconds)', 'omega (radians per second)', 'bottom right' )
    for f in pendula:
        method = method_class( f )
        method.set_initial_condition( U0 )
        #TODO: how do I want to store results for comparing results by omega_D?
        u, t = method.solve( t_points )
        # u(t) is a 2 x n array with [u0,u1] for all t's
        u0_values = u[:, 0]  # get the u0 values from u for plotting
        u0_graph.add_data( t, u0_values, str( f.omega_D ) )
        u1_values = u[:, 1]
        u0_graph.add_data( t, u1_values, str( f.omega_D ) )
#    u0_graph.show_figure()
    u1_graph.show_figure()
Ejemplo n.º 3
0
for jac_grid in jac_grids:
    jac_grid.solve_jacobi( epsilon, N_iter_max )
#    print 'hi'
    jac_N_iters_list.append( jac_grid.N_iter )

    
for sor_grid in sor_grids:
    sor_grid.solve_SOR( epsilon, N_iter_max, accuracy )
#    print 'hey'
    sor_N_iters_list.append( sor_grid.N_iter )

# make plot figure of results
title = 'N iterations vs. n | SOR, Jacobi compared.'
subtitle = ''
x_label = 'n = sites on grid'
y_label = 'N = number of iterations'
legend_loc = 'upper left'
graph = Grapher( title, subtitle, x_label, y_label, legend_loc )
graph.add_data( n_list, jac_N_iters_list, 'Jacobi' )
graph.add_data( n_list, sor_N_iters_list, 'SOR' )
graph.save_to_file( '/home/res/Documents/duke/2012S/PHY260/midterm/jac_sor_' + time_string() + '.png' )

# save the data to be plotted
np.save( '/home/res/Documents/duke/2012S/PHY260/midterm/jac_N_iter_vs_n_' + time_string(), np.asarray( zip( n_list, jac_N_iters_list ) ) )
np.save( '/home/res/Documents/duke/2012S/PHY260/midterm/sor_N_iter_vs_n_' + time_string(), np.asarray( zip( n_list, sor_N_iters_list ) ) )

# show the plot
graph.show_figure()

print "Done with midterm_Dipole.py!"
Ejemplo n.º 4
0
 def plot_V_of_r( self ):
     r, V = self.get_V_of_r()
     graph = Grapher( "V(r): Electric Potential of a Static Dipole", '', 'radius from origin', 'Potential', 'upper right' )
     graph.add_data_scatter( r, V, 'V(r)' )
     graph.save_to_file( '/home/res/Documents/duke/2012S/PHY260/midterm/V_r.png' )
     graph.show_figure()
Ejemplo n.º 5
0
######## 
# Execute parts of the question
########

# some helper stuff
R = side_length * 0.5 # radius described as half the langth of one side of the current loop.
b_z_circular = lambda z: ( mu0_I * R ** 2 / ( 2 * ( z ** 2 + R ** 2 ) ** ( 1.5 ) ) )

# Part A:
y1_vectors = np.asarray( map( b_field, part_A_points ) )
y2 = map( b_z_circular, axis_points )
for ( label, i ) in zip( ( 'x', 'y', 'z' ), range( 3 ) ):
    title = 'Part A: B(' + label + ') for x = y = 0 (Numerical and Analytical) '
    y1 = np.hsplit( y1_vectors, 3 )[i]   # get z-axis points, in third column
    x_label = label
    graph = Grapher( title, subtitle, x_label, y_label, legend_loc )
    graph.add_data( axis_points, y1, 'Numerical: B for x = y = 0' )
    graph.add_data( axis_points, y2, 'Analytical: B for x = y = 0' )
    graph.save_to_file( '/tmp/' + 'A' + label + '.png' )

# Part B:
y1_vectors = np.asarray( map( b_field, part_B_points ) )
for ( label, i ) in zip( ( 'x', 'y', 'z' ), range( 3 ) ):
    title = 'Part B: B(' + label + ') for y=0, z=1'     
    y1 = np.hsplit( y1_vectors, 3 )[i]   # get x-axis points
    x_label = label
    graph = Grapher( title, subtitle, x_label, y_label, legend_loc )
    graph.add_data( axis_points, y1, 'Approximation: B for y=0, z=1' )
    #graph.show_figure()
    graph.save_to_file( '/tmp/' + 'B' + label + '.png' )