0

Recently I have managed to do the animation. But now I can't make the tangent line went as I want (not even displayed yet). The formula of the tangent line is y=(2/r)(sqrt(1-((r^2)/4))-1)x +r. The formula is obtained from 2 circles equation (C1 and C2). C1(blue) : x^2+y^2=r^2, and C2(green) : (x-1)^2+y^2=1. My goal is to obtain this kind of animation as reference and my current animation goes like this now.

How should the code looks like when the animation looks like the reference one (the first one)? Any comments and answers would be very helpful for me as a beginner, I appreciate it, Thank you.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots(1)
line, = ax.plot([], [], lw=2)
line2, = ax.plot([], [], lw=2)
ax.set_xlim(-5,5)
ax.set_ylim(-5,5)
# theta goes from 0 to 2pi
theta = np.linspace(0, 2*np.pi, 100)

# the radius of the circle
r = np.sqrt(1)
r2 = np.sqrt(4)

# compute x1 and x2
x1 = 1+r*np.cos(theta)
y1 = r*np.sin(theta)
x2 = r2*np.cos(theta)
y2 = r2*np.sin(theta)

# Move left y-axis and bottim x-axis to centre, passing through (0,0)
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')

# Eliminate upper and right axes
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')

# Show ticks in the left and lower axes only
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

def init():
    line.set_data([], [])
    return line,
def init2():
    line.set_data([], [])
    return line2,
def animate(i):
    x2 = np.sqrt(i)*np.cos(theta)
    y2 = np.sqrt(i)*np.sin(theta)
    line.set_data(x2, y2)
    return line,
def animate2(i):
    x3 = np.linspace(0,r2**2,100)
    y3 = ((2/r2)*(np.sqrt(1-(r2**2)/4)-1)*x3)+r2
    line.set_data(x3, y3)
    return line,

# create the figure
ax.plot(x1,y1)
ax.set_aspect(1)
plt.grid()
anim = animation.FuncAnimation(fig, animate, init_func=init,
                                interval=1000, blit=False,\
                                frames=np.hstack([range(0), 
                                                  range(4)[::-1]]))
anim2 = animation.FuncAnimation(fig, animate2, init_func=init2,
                                interval=1000, blit=False)
                                
plt.show()

f = r"D:/UNPAR/Semester 2/Pemrograman Komputer/Project/SK.gif" 
writergif = animation.PillowWriter(fps=30) 
anim.save(f, writer=writergif)
Q-Max_Lu
  • 15
  • 4

1 Answers1

0

The animation functions need to be combined into one. We will combine them into an initialization function and an animation function.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots(1)
line, = ax.plot([], [], lw=2, color='green')
line2, = ax.plot([], [], lw=2, color='red')
ax.set_xlim(-5,5)
ax.set_ylim(-5,5)

# theta goes from 0 to 2pi
theta = np.linspace(0, 2*np.pi, 100)

# the radius of the circle
r = np.sqrt(1)
r2 = np.sqrt(4)

# compute x1 and x2
x1 = 1+r*np.cos(theta)
y1 = r*np.sin(theta)
x2 = r2*np.cos(theta)
y2 = r2*np.sin(theta)

# create the figure
ax.plot(x1,y1)
ax.set_aspect(1)
plt.grid()

def init():
    line.set_data([], [])
    line2.set_data([], [])
    return line,line2

def animate(i):
    x2 = np.sqrt(i)*np.cos(theta)
    y2 = np.sqrt(i)*np.sin(theta)
    print(max(y2))
    line.set_data(x2, y2)

    x3 = [0, 4-(0.1*i)]
    y3 = [max(y2), 0]
    line2.set_data(x3, y3)    
    return line,line2


anim = animation.FuncAnimation(fig, animate, init_func=init, interval=1000, blit=False,frames=np.arange(10,0,-1))
                 
plt.show()

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32