So for my assignment I have to code a program to solve first order ODE's using Euler's Method. My program works, it returns the right values. (I checked using an online calculator). However, solution to the assignment returns something very different.
The initial condition is x(0) = 0, from t = 0 to t = 10. With 10 steps. (I presume it to be the green line)
Using my program I get the results: ([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0], (the x values)
And the y-values respectively
[[0.0]
[-2.0]
[-12.909297426825681]
[-43.245542352772254]
[-110.57365446753619]
[-239.994320283207]
[-462.9378998529216]
[-812.0361590626763]
[-1333.0340119617658]
[-2071.8747943733247]
[-3080.8748098338056]
So as you can see, in the solutions, the y values don't even go as far as -1.0. So I'm wondering how does this happen?
EDIT CODE: f = function y0 = initial condition at time t0 t0 = initial time h = step size N = number of steps
def integrateEuler(f,y0,t0,h,N):
t = t0
y = y0
z = []
v = []
yf = N*h #final xval
while t <= N:
xval = t
yval = [y]
t += h
y += h * f(t,y)
z.append(xval)
v.append(yval)
return z, v
#FUNCTION
def f(x,t):
vv = -x**3 - x + sin(t)
return vv
I enter in the shell:
>>>Euler(f, 0., 0., 1., 10)
f(x,t)
, but you callf(t,y)
. Switch the order of arguments in either call or definition. $\endgroup$