Question

In: Computer Science

Write a python code that uses the Euler Implicit method to approximate/plot the solutions to each...

Write a python code that uses the Euler Implicit method to approximate/plot the solutions to each of the following initial-value

  1. y′=−ty+4t/y, 0 ≤ t ≤ 1, y(0)=1, with h=0.1
  2. y′=y2+yt, 1 ≤ t ≤ 3, y(l)=−2, with h=0.2

Solutions

Expert Solution

I have writtend required comments along with the code please go through them thoroughly.

the euler implici formula is as follows:

y[n+1] = y[n] + h * (1/1+h) * (f(y,t[n+1]))

Please go through the formula ones the same will be reflected in the code.

import numpy as np # numpy library
import matplotlib.pyplot as plt # For Graph Plotting


def feval (funcName , *args): # given a function and its arguments it evaluates the value of that function
    return eval (funcName) (*args)


def myfunction_1(t,y): # Declare of  function  1
    return -1.0*y*t + (4.0*t)/y


def myfunction_2(t,y): # Declaration of function 2
    return 2.0*y + t*y

def euler_implicit(func, t_min, t_max, y_init, h): # implicit Euler method
    N = int((t_max - t_min)/h) # Calculating the number of intervals between min and max time
    t = np.linspace(t_min, t_max, N+1) # using numpy library we calcuate values added with h.
    y = np.zeros(N+1) # for same interval as time period we have to calculate the y slope that dy/dt approximation
    # set initial value
    y[0] = y_init
    for n in range(N): # in range interval
        y_prime = (1/(1 + h)) * feval(func, t[n+1], y[n]) # formula for euler implicit is y[n+1] = y[n] + h * (1/1+h) * (f(y,t[n+1]))
        y[n+1] = y[n] + h * y_prime
    plt.plot(t,y) # plotting between time interval and y(t)
    plt.xlabel('Time Interval(t)')
    plt.ylabel('y(t)')
    plt.title('Euler Implicit Method Approximation')
    plt.show()

def main():
    euler_implicit('myfunction_1', 0, 1, 1, 0.1)
    euler_implicit('myfunction_2', 1, 3, -2, 0.2)
if __name__ == "__main__":
    main()

Code Image Snippet:

Code Output Image:

For any further doubts please leave a comment. you can run this code snippet as a script s well just copy paste the code in script and run it through command line.


Related Solutions

1. Write a python code that uses the Runge Kutta Method method to approximate the solutions...
1. Write a python code that uses the Runge Kutta Method method to approximate the solutions to each of the following initial-value problems and compare/plot the results to the actual values. a) y′=te^(3t) − 2y, 0 < t < 1, y(0) = 0 with h = 0.5; actual solution y(t)=1/5te^(3t) − 1/25e^(3t) + 1/25e^(−2t). - Use the Runge Kutta method to approximate/plot the solutions to each of the following initial-value b) ?′=1+(?−?)2,2<?<3,?(2)=1y′=1+(t−y)2,2 c) ?′=1+??,1<?<1,?(1)=2y′=1+yt,1
write a code in python for evolutionary search method for multivarible
write a code in python for evolutionary search method for multivarible
An improvement to the Forward Euler method is Heun’s method, a “predictor-corrector” approach that uses the...
An improvement to the Forward Euler method is Heun’s method, a “predictor-corrector” approach that uses the predicted values at the next time step to create a second improved (“corrected”) approximation. Given the first order ODE y′ = f (x, y), and a point on the solution curve (xn ,  yn), we want to estimate the next point at a step size h later. We make a first prediction of the next value, y*, using the forward Euler approach: xn+1  = ...
Plot the Trapezoid Method approximate solution on [0,1] for the differential equation y = 1 +...
Plot the Trapezoid Method approximate solution on [0,1] for the differential equation y = 1 + y2 and initial condition (a) y0 = 0 (b) y0 = 1, along with the exact solution (see Exercise 6.1.7). Use step sizes h = 0.1 and 0.05 (Code In Matlab)
Plot the Euler’s Method approximate solution on [0,1] for the differential equation y* = 1 +...
Plot the Euler’s Method approximate solution on [0,1] for the differential equation y* = 1 + y^2 and initial condition (a) y0 = 0 (b) y0 = 1, along with the exact solution (see Exercise 7). Use step sizes h = 0.1 and 0.05. The exact solution is y = tan(t + c)
Write python 3 code to define a function that uses three arguments, and returns a result....
Write python 3 code to define a function that uses three arguments, and returns a result. The function returns True if the first argument is more than or equal to the second argument and less than or equal to the third argument, otherwise it returns False. Write a python 3 code for function main, that does the following: creates a variable and assign it the value True. uses a while loop which runs as long as the variable of the...
***Please code in Python Write another code Newton (in double precision) implementing the Newton-Raphson Method   (copy...
***Please code in Python Write another code Newton (in double precision) implementing the Newton-Raphson Method   (copy your Bisect code and modify).   Evaluation of F(x) and F'(x) should be done in a subprogram FCN(x).   The code should ask for input of: x0, TOL, maxIT (and should print output similar to Bisect code).   Debug on a simple problem, like x2−3 = 0.   Then use it to find root of F(x) in [1,2] with TOL=1.e-12. Now consider the problem of finding zeros of      ...
"Using Python, code to find the smallest and largest planet mass (if known), and plot these...
"Using Python, code to find the smallest and largest planet mass (if known), and plot these as two curves against the year of discovery" Basically looking to use data from an excel sheet where 'disc_year' and 'pl_mass' are columns of data; and code Python to find the maximum value of mass and the minimum value of mass for each year, then plot this as two curves. There are multiple planets for any given year hence multiple values for mass. Below...
Please write in beginner level PYTHON code! Your job is to write a Python program that...
Please write in beginner level PYTHON code! Your job is to write a Python program that asks the user to make one of two choices: destruct or construct. - If the user chooses to destruct, prompt them for an alternade, and then output the 2 words from that alternade. - If the user chooses construct, prompt them for 2 words, and then output the alternade that would have produced those words. - You must enforce that the users enter real...
This code is to be written in Matlab. Write a function that will plot cos(x) for...
This code is to be written in Matlab. Write a function that will plot cos(x) for x values ranging from -pi to pi in steps of 0.1, using black *'s. It will do this three times across in one Figure Window, with varying line widths. If no arguments are passed to the function, the line widths will be 1, 2, and 3. If on the other hand, an argument is passed to the function, it is multiplier for these values....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT