I am trying to solve Euler's integrals for different values of h (0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625, 0.0078125, and 0.00390625)
The function is dy/dx = x^3
I am trying to do it using 2 for loops (nested). The outer loop creates different values of h, and the inner loop will compute the Eulers for a particular value of h. The problem that I am facing is I am unable to make these loops work with each other. For e.g., for a fixed value of h=0.5 in the first iteration, it should compute the Eulers and save the answer. Once that's done, it will come out of that loop, re-initialize the values for the initial condition, take the new value of h and continue doing the same. Following is the code that I've tried so far.
import matplotlib.pyplot as plt
import numpy as np
# right hand side of the ODE
def f(x) :
return x^3;
# initial condition
#y [0] = 0
#x = np.zeros()
#x[0] = 0
# time step
#h = 0.1
#import array as arr
x0 = 0.5
lim = np.arange(0,8,1)
x_new = np.zeros(len(lim))
x_new_arr = []
count_arr = []
s = []
#y = []
for count in range (0,len(lim)-1):
x_new = x0/2
x0 = x_new
count_arr.append(count)
#print(count_arr)
x_new_arr.append(x0)
#print(x_new_arr)
i = 0
# initial condition
y = np.zeros()
y [0] = 0
x = np.zeros()
x[0] = 0
for i in np.arange(0, 2, x_new_arr[i]):
#while x_new_arr:
#s[i + 1] = s[i] + h*f(t[i], s[i])
y[i] = y[i-1] + f(x[i-1],y[i-1])*x_new_arr[i]
print(count_arr)
#print(x0)
print(x_new_arr)
plt.plot(count_arr,x_new_arr,'ro--')
plt.show()