def ODE_Solver(x, x_f, n, y):

    h = (x_f - x) / (n - 1)

    x1 = x2 = x
    y1 = y2 = y

    y1_points = []
    y1_points.append(y1)
    y2_points = []
    y2_points.append(y2)
    x_points = []
    x_points.append(x1)

    for i in range(n - 1):

        y1, x1 = RK_Method(x1, h, y1)
        y2, x2 = Euler_Forward(x2, h, y2)

        y1_points.append(y1)
        y2_points.append(y2)
        x_points.append(x1)

    print(x_points)

    plt.plot(x_points, y1_points, label='RK4')
    plt.plot(x_points, y2_points, label='Euler-Forward')
    plt.legend()
    pl.prutorsaveplot(plt, 'plot2d.pdf')
def System_of_ODE(t, t_f, h, y_i, z_i):

    w = np.zeros((2, 1))
    w[0] = y_i
    w[1] = z_i

    f = F(w, t)

    y_points = []
    y_points.append(y_i)
    z_points = []
    z_points.append(z_i)
    t_points = []
    t_points.append(t)

    while t < t_f:

        w, t = RK_Method(t, h, w, f)
        y_points.append(w[0])
        z_points.append(w[1])
        t_points.append(t)

    print('Plotting Y v/s T and Z v/s T')
    plt.plot(t_points, y_points)
    plt.plot(t_points, z_points)
    pl.prutorsaveplot(plt, 'plot2d.pdf')
示例#3
0
def Shooting_Method(x, x_f, h, a, b, v1, v2, e, i_max):

    i = 0
    err = 1
    
    while err >= e:
        
        x_i = x #starting_x
        
        y = np.zeros((2,1))
        y[0] = a
        y[1] = v1
        
        while (x < x_f):
        
            f = F(y, x)
            y, x = RK_Method(x, h, y, f)
        
        u11 = y[0] #value_at_boundary_using_v1
        
        x = x_i
        y = np.zeros((2,1))
        y[0] = a
        y[1] = v2

        y_points = []
        y_points.append(a)
        x_points = []
        x_points.append(x)
        
        while (x < L):
            
            f = F(y, x)
            y, x = RK_Method(x, h, y, f)
            
            x_points.append(x)
            y_points.append(y[0])
        
        u12 = y[0]  #value_at_boundary_using_v2
        
        v3 = Secant_Method(v1, v2, b, u11, u12)
        v1, v2 = v2, v3
        
        
        err = abs(u12-b) 
        i = i+1
        x = x_i
        
        if i > i_max :
            print('ERROR: Limit Exceeded')
            return None
        
    print('The solution was obtained after', i, 'iterations')
        
    plt.plot(x_points, y_points)
    pl.prutorsaveplot(plt, 'plot2d.pdf')
    
    return None
示例#4
0
def Direct_Method(a, b, h, l, flag):

    n = int(l / h)
    x = np.arange(n + 1) * h

    L = np.zeros(n)
    D = np.zeros(n)
    U = np.zeros(n)
    B = np.zeros(n)

    for i in range(0, n):
        L[i] = p(x[i + 1]) / (h**2) - (q(x[i + 1]) / (2 * h))

    for i in range(0, n):
        D[i] = -2 * p(x[i + 1]) / (h**2) + r(x[i + 1])

    for i in range(0, n):
        U[i] = p(x[i + 1]) / (h**2) + (q(x[i + 1]) / (2 * h))

    for i in range(0, n):
        B[i] = s(x[i + 1])

    B[0] = B[0] - L[0] * a

    if flag == 1:
        print('Backward Difference')
        L[n - 2] = L[n - 2] - (U[n - 2] / 3)
        D[n - 2] = D[n - 2] + (4 * U[n - 2] / 3)
        B[n - 2] = B[n - 2] - (2 * U[n - 2] * b * h / 3)

        L[0] = U[n - 1] = L[n - 1] = D[n - 1] = B[n - 1] = 0

        Y = np.array(a)
        Y = np.append(Y, Thomas_Algo(L, D, U, B, flag))
        Yn = (2 * b * h / 3) + (4 * Y[-1] / 3) - (Y[-2] / 3)
        Y = np.append(Y, Yn)
        print(Y)
        print(x)

    if flag == 0:

        print('Ghost Node')
        L[n - 1] = L[n - 1] + U[n - 1]
        B[n - 1] = B[n - 1] - (2 * U[n - 1] * b * h)
        L[0] = U[n - 1] = 0

        Y = Thomas_Algo(L, D, U, B, flag)
        Y = np.array(a)
        Y = np.append(Y, Thomas_Algo(L, D, U, B, flag))
        print(Y)
        print(x)

    plt.plot(X, Y)
    plt.scatter(X, Y, c='k')
    pl.prutorsaveplot(plt, 'plot2d.pdf')
def Plot(Q, x, y, a, Value): #Plot the Spline
    
    n = len(x)
    
    for i in range(n-1):
        X = np.linspace(x[i], x[i+1], 100)
        Y = Cubic_Spline_Value(Q, x, i, X)
        plt.plot(X, Y)
        plt.tight_layout()
    
    plt.scatter(x, y, s=10, color='k', zorder=10)
    
    if Value:
        plt.scatter(a, Value, s=10, color='blue', zorder=10) #Plot the point in blue if its inside data range
         
    pl.prutorsaveplot(plt, 'plot2d.pdf')
def Plots(x, u, ue, t, errmax, phi):
    
    rcParams['figure.figsize'] = 5, 3
    
    plt.figure()
    plt.plot(x,u, '+r',label = r'FTCS solution') 
    plt.plot(x,ue, label = r'Analytic solution') 
    plt.xlabel(r'distance', fontsize=10)
    plt.ylabel(r'function', fontsize=10)
    pl.prutorsaveplot(plt, 'plot1.pdf')
    
    plt.figure()
    plt.plot(t,errmax) 
    plt.xlabel(r'time', fontsize=10)
    plt.ylabel(r'maxerror', fontsize=10)
    pl.prutorsaveplot(plt, 'plot2.pdf')
    
    dist, time = np.meshgrid(x, t) # no idea why to do that
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.plot_surface(dist, time, np.transpose(phi),cmap=cm.jet, linewidth=0.1)
    pl.prutorsaveplot(plt, 'plot3.pdf')
示例#7
0
# Function to give spline values at different x values
def Qvalue(r, A, B, C, D):
    q_i = 0
    for i in range(n):
        if (r > X[i]):
            q_i = i
    q = A[q_i] * ((r - X[q_i])**3) - B[q_i] * ((
        r - X[q_i + 1])**3) + C[q_i] * (r - X[q_i]) - D[q_i] * (r - X[q_i + 1])
    return q


# code for prutor plot

X_spline = np.linspace(X[0], X[n - 1], 100)
Y_spline = np.zeros(shape=(len(X_spline)))
for i in range(len(X_spline)):
    Y_spline[i] = Qvalue(X_spline[i], A, B, C, D)

plt.plot(X,
         Y,
         color='green',
         marker='o',
         label='Original Data',
         linestyle='None')
plt.plot(X_spline, Y_spline)
plt.legend(["Original Data", "Cubic Spline"])
plt.grid()
plt.xlabel('x')
plt.ylabel('F(x)')
pl.prutorsaveplot(plt, 'CubicSpline.pdf')