Esempio n. 1
0
b = 5
p = b - ((b - a) * f(b)) / (f(b) - f(a))


def animate(i):
    global a, b, p
    ax.cla()
    d = linspace(1, 5, 1000)
    ax.axis('off')
    ax.set_title('Regula-falsi-method')
    ax.axhline(color='white', linewidth=3)
    ax.axvline(color='white', linewidth=3)
    ax.plot(d, f(d))
    ax.scatter(a, f(a), marker='.', color='#E7183F')
    ax.scatter(b, f(b), marker='.', color='#E7183F')
    ax.scatter(p, 0, marker='o', color='#18E7C0')
    ax.plot([a, b], [f(a), f(b)], color='red', label='approximate')
    ax.plot([p, p], [0, f(p)], color='blue')
    ax.plot([p, b], [f(p), f(b)], color='#95E718', label='next-approximate')
    ax.legend()
    if f(p) * f(a) > 0:
        a = p
    else:
        b = p
    p = b - ((b - a) * f(b)) / (f(b) - f(a))


manim = fn(fig, animate, frames=arange(10), repeat=False, interval=500)
manim.save('Animation-regula_falsi-method.mp4', fps=2, writer='ffmpeg')
plt.show()
    ax.plot(a, a)
    ax.scatter(p0, p, color='red', label='Inititial-approximation')
    ax.axvline(color='white', linewidth=3)
    ax.axhline(color='white', linewidth=3)


def animate(i):
    global p0, p
    ax.plot([p0, g(p0)], [g(p0), g(p0)],
            color='#AE40Bf',
            linewidth=0.8,
            label='p0')
    ax.plot([g(p0), g(p0)], [g(p0), g(p)],
            color='#6A18E7',
            linewidth=0.8,
            label='g(p0)')
    if i == 0:
        ax.legend()
    p0 = p
    p = g(p0)


manim = fn(fig,
           animate,
           frames=arange(50),
           interval=1000,
           repeat=False,
           init_func=inti)
manim.save('Animation-fixed_point-method.mp4', fps=2, writer='ffmpeg')
plt.show()
#plt.style.use('dark_background')
def f(x):
    return 2**x * sin(x)


def df(x, h):
    return (f(x + h) - f(x - h)) / (2 * h)


def derivat(a, h=0.2, n=4):
    x = ndarray((n, n))
    for i in range(n):
        x[i, 0] = df(a, h)
        h /= 2
    for j in range(1, n):
        for i in range(j, n):
            x[i,
              j] = x[i, j - 1] + (x[i, j - 1] - x[i - 1, j - 1]) / (4**j - 1)
    return x[n - 1, n - 1]


fig, ax = plt.subplots()


def animate(i):
    print(i)


anim = fn(fig, animate, frames=arange(6, 10), interval=0, repeat=False)
#anim.save('approx.mp4',fps=30,writer='ffmpeg')
plt.show()
from matplotlib.animation import FuncAnimation as fn
import matplotlib.pyplot as plt
plt.style.use('dark_background')
def f(x):
    return 2**x*sin(x)
def df(x,h):
    return (f(x+h)-f(x-h))/(2*h)
def derivat(a,h=0.2,n=4):
    x=ndarray((n,n))
    for i in range(n):
        x[i,0]=df(a,h)
        h/=2
    for j in range(1,n):
        for i in range(j,n):
            x[i,j]=x[i,j-1]+(x[i,j-1]-x[i-1,j-1])/(4**j-1)
    return x[n-1,n-1]           
fig,ax=plt.subplots()

def animate(i):
    b=linspace(-2,4,(i+1))
    ax.cla()
    ax.axis('off')
    a=linspace(-2,4,1000)
    ax.axis('off')
    ax.axhline(color='white',linewidth=3)
    ax.axvline(color='white',linewidth=3)
    ax.plot(a,f(a),color='red')
    ax.bar(b,f(b),width=7/(i+1),alpha=0.7,color='#57A897')
anim=fn(fig,animate,frames=arange(1,300),interval=500)
anim.save('approx.mp4',fps=30,writer='ffmpeg')
plt.show()