vendredi 5 juin 2020

Solving differential equations numerically

I tried solving a very simple equation f = t**2 numerically. I coded a for-loop, so as to use f for the first time step and then use the solution of every loop through as the inital function for the next loop.

I am not sure if my approach to solve it numerically is correct and for some reason my loop only works twice (one through the if- then the else-statement) and then just gives zeros.

Any help very much appreciatet. Thanks!!!

## IMPORT PACKAGES
import numpy as np
import math
import sympy as sym
import matplotlib.pyplot as plt

## Loop to solve numerically

for i in range(1,4,1):
    if i == 1:
        f_old = t**2
        print(f_old)
    else: 
        f_old = sym.diff(f_old, t).evalf(subs={t: i})
        f_new = f_old + dt * (-0.5 * f_old)
        f_old = f_new
        print(f_old)

Aucun commentaire:

Enregistrer un commentaire