Differential Equations and Error of Estimations

asked 2014-09-29 17:45:08 -0500

updated 2017-08-01 05:01:44 -0500

FrédéricC gravatar image

So I'm tasked with the following two questions:

  1. For the initial value problem y′=y sin(x), y(0)=1, first find the exact solution. Then make log-log plots of the error versus n (the number of steps) at x=pi for Euler's method and the fourth-order Runge-Kutta method.

  2. Repeat the above exercise for the IVP y′=y+sin(x), y(0)=−0.48.

So we have a defined function for funding the Euler estimation:

def EulerMethod(xstart, ystart, xfinish, nsteps, f): ''' Returns a list of x and y values for the initial value problem y' = f, y(xstart) = ystart, up to x=xfinish, using n steps of Euler's Method.

EXAMPLE:
    var('x,y')
    f(x,y) = -y + cos(x)
    EulerMethod(0,1,2,4,f)
    [(0, 1), (0.500000000000000, 1), (1.00000000000000, 0.938791280945),

(1.50000000000000, 0.739546793407), (2.00000000000000, 0.405141997537)]

    #If you just want to get the last value, you could do:
    sol = EulerMethod(0,1,2,4,f)
    sol[-1]
    (2.00000000000000, 0.405141997537)
'''
sol = [ystart]
xvals = [xstart]
h = N((xfinish-xstart)/nsteps)
for step in range(nsteps):
    sol.append(sol[-1] + h*f(xvals[-1],sol[-1]))
    xvals.append(xvals[-1] + h)
return zip(xvals,sol)

As written by the teacher. We are given that and no function for the RK4 so I assume we are to use the built in Sage RK4 estimation.

Anyways I have no idea what to do. I'm absolutely lost. Can anyone please help me? Massive thanks in advance.

edit retag flag offensive close merge delete