<jupyter_start><jupyter_text>Challenge 1:    Use arrange() instead of linspace.<jupyter_code>T = 100.0
dt = 0.02
#N = int(T/dt) +1
t2 = numpy.arange(0.0, T+dt, dt)
print(t2[0], t2[-1])
check = t1==t2 #compare both arrays
print(type(check[0]))
#print(check)
False in check #search for a response 'False' where strings\
                    #are not equal<jupyter_output><class 'numpy.bool_'><jupyter_text>Euler's Method<jupyter_code>#ICs
z0 = 100 #altitude
b0 = 10 #upward velocity resulting from gust
zt = 100 #
g = 9.81

u= numpy.array([z0, b0])

#initialize array for elevation values
z = numpy.zeros(N)
z[0] = z0
#time-loop using Euler's method
for n in range(1,N):
    u = u + dt*numpy.array([u[1], g*(1-u[0]/zt)])
    z[n] = u[0]

pyplot.figure(figsize=(10,4))
pyplot.ylim(40, 160)
pyplot.tick_params(axis='both', labelsize=14)
pyplot.xlabel('t', fontsize=14)
pyplot.ylabel('z', fontsize=14)
pyplot.plot(t1,z, 'k-');
z_exact = b0*(zt/g)**.5*numpy.sin((g/zt)**.5*t1)\
+(z0-zt)*numpy.cos((g/zt)**0.5*t1)+zt   

pyplot.figure(figsize=(10,4))
pyplot.ylim(40,160)
pyplot.tick_params(axis='both', labelsize=14)
pyplot.xlabel('t', fontsize=14)
pyplot.ylabel('z', fontsize=14)
pyplot.plot(t1,z)
pyplot.plot(t1,z_exact)
pyplot.legend(['Numerical Solution', 'Analytical Solution']);<jupyter_output><empty_output><jupyter_text>Challenge 2: Write a function for Eulers method<jupyter_code>def my_euler(u, t, z0, zt, N):
    z_func = numpy.empty_like(t)
    z[0]=z0
    for n in range(1,N):
        u = u+ dt*numpy.array([u[1], g*(1-u[0]/zt)])
        z_func[n] = u[0]
    return z_func
dt_values = numpy.array([0.1, 0.05, 0.01, 0.005, 0.001, 0.0001])

z_values = numpy.empty_like(dt_values, dtype=numpy.ndarray)

for i, dt in enumerate(dt_values):
    N= int(T/dt)+1 #number of timesteps
    t = numpy.linspace(0., T, N)
    
    #ICs
    u = numpy.array([z0, b0])  
    
    z = my_euler(u, t, z0, zt, N)   
    z_values[i] = z.copy()
def get_error(z,dt):
    N= len(z)
    t = numpy.linspace(0., T, N)
    
    
    z_exact = b0*(zt/g)**.5*numpy.sin((g/zt)**.5*t)+\
                (z0-zt)*numpy.cos((g/zt)**.5*t)+zt
    
    return dt * numpy.sum(numpy.abs(z-z_exact))
error_values = numpy.empty_like(dt_values)
for i, dt in enumerate(dt_values):
    error_values[i] = get_error(z_values[i], dt)
pyplot.figure(figsize=(10,6))
pyplot.tick_params(axis='both', labelsize=14)
pyplot.grid(True)
pyplot.title('Error vs. step size')
pyplot.xlabel('$\Delta t$', fontsize=14)
pyplot.ylabel('Error', fontsize=14)
pyplot.loglog(dt_values, error_values, 'ko-')
pyplot.axis('equal');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1907514450867052, 'fraction_numerical': 0.05009633911368015, 'mean_word_length': 4.287169042769857, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '198852', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 1202, 'n_tokens_neox': 1134, 'n_words': 213, 'path': 'lessons/01_phugoid/01_02_phugoid_oscillation_solutions.ipynb', 'repo_name': 'rbds/Numerical_Methods_working_folder', 'size': '2595'}