import numpy as np, matplotlib.pyplot as plt 

# Resolver a equação diferencial u'(s)=2su(s), com u(0)=1.

def f(s,u):   # du/dt=2su
    p=2*s*u
    return p

n=10
h=(1-0)/n
x=np.linspace(0,1,n+1)
# Método de Euler y_(i+1)=y_i+h*f(t_i,y_i)
y=[]
y.append(1) # condição inicial 

for i in range(1,n+1): 
    y.append(y[i-1]+h*f(x[i-1],y[i-1]))
    
plt.plot(x,y,color='red')
plt.show()

# Método dos trapézios y_(i+1)=y_i+h*(f(t_i,y_i)+f(t_(i+1),y_(i+1)))/2

y=[]
y.append(1) # condição inicial 

for i in range(1,n+1): 
    y.append((y[i-1]+h*f(x[i-1],y[i-1])/2)/(1-x[i]*h))
    
plt.plot(x,y,color='blue')
plt.show()

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: