Skip to content
🤔prompts chat🧠
🔍
question:Write me a script in Python to create an animated double pendulum, where the lengths of the two pendulum arms can be dynamically adjusted by the user through an interactive widget. The program should display the angles of each arm relative to the vertical axis, and include a feature that allows the user to save the animation as a video file with a custom filename.

answer:import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation from scipy.integrate import odeint from ipywidgets import interact, widgets from IPython.display import display class DoublePendulum: def __init__(self, length1=1.0, length2=1.0, mass1=1.0, mass2=1.0): self.length1 = length1 self.length2 = length2 self.mass1 = mass1 self.mass2 = mass2 def equations(self, y, t): theta1, z1, theta2, z2 = y c, s = np.cos(theta1 - theta2), np.sin(theta1 - theta2) theta1dot = z1 theta2dot = z2 z1dot = (self.mass2 * self.length2 * z2**2 * s * c + self.mass2 * 9.81 * np.sin(theta2) * c + self.mass2 * self.length2 * z2**2 * s - (self.mass1 + self.mass2) * 9.81 * np.sin(theta1)) / (self.length1 * (self.mass1 + self.mass2 * s**2)) z2dot = ((self.mass1 + self.mass2) * (self.length1 * z1**2 * s - 9.81 * np.sin(theta2) + 9.81 * np.sin(theta1) * c) + self.mass2 * self.length2 * z2**2 * s * c) / (self.length2 * (self.mass1 + self.mass2 * s**2)) return theta1dot, z1dot, theta2dot, z2dot def animate_pendulum(length1, length2): pendulum = DoublePendulum(length1, length2) y0 = [np.pi / 2, 0, np.pi / 2, 0] t = np.linspace(0, 10, 250) solution = odeint(pendulum.equations, y0, t) x1 = pendulum.length1 * np.sin(solution[:, 0]) y1 = -pendulum.length1 * np.cos(solution[:, 0]) x2 = x1 + pendulum.length2 * np.sin(solution[:, 2]) y2 = y1 - pendulum.length2 * np.cos(solution[:, 2]) fig = plt.figure() ax = fig.add_subplot(111, autoscale_on=False, xlim=(-2, 2), ylim=(-2, 2)) ax.set_aspect('equal') ax.grid() line, = ax.plot([], [], 'o-', lw=2) time_template = 'time = %.1fs' time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes) def init(): line.set_data([], []) time_text.set_text('') return line, time_text def update(i): x = [0, x1[i], x2[i]] y = [0, y1[i], y2[i]] line.set_data(x, y) time_text.set_text(time_template % t[i]) return line, time_text ani = animation.FuncAnimation(fig, update, frames=np.arange(len(t)), init_func=init, blit=True) def save_animation(file_name): Writer = animation.writers['ffmpeg'] writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800) ani.save(file_name, writer=writer) plt.show() display(widgets.Text(value='animation.mp4', description='Filename:'), widgets.Button(description="Save as")) def on_button_clicked(b): file_name = widget_text.value save_animation(file_name) widget_text = widgets.Text(value='animation.mp4', description='Filename:') button = widgets.Button(description="Save as") button.on_click(on_button_clicked) display(widget_text, button) interact(animate_pendulum, length1=widgets.FloatSlider(min=0.1, max=2.0, step=0.1, value=1), length2=widgets.FloatSlider(min=0.1, max=2.0, step=0.1, value=1));

question:Create a Python function that truncates a given string to display only the first 4 unique characters in alphabetical order and outputs the unique set of characters. In case the string has less than four unique characters, display the unique characters only.

answer:def truncate_unique_chars(s): Truncates the input string to display only the first 4 unique characters in alphabetical order. If the string has less than four unique characters, display the unique characters only. # Create a set to capture unique characters and sort them alphabetically unique_chars = sorted(set(s)) # Get the first 4 unique characters truncated_chars = unique_chars[:4] # Join the list of characters into a string and return return ''.join(truncated_chars)

question:Write a code to generate the Fibonacci series for the first n numbers, and then modify it to print only the even numbers in the generated series.

answer:def fibonacci(n): Generates the first n numbers of the Fibonacci series. fib_series = [0, 1] if n <= 0: return [] elif n == 1: return [0] else: while len(fib_series) < n: next_value = fib_series[-1] + fib_series[-2] fib_series.append(next_value) return fib_series[:n] def even_fibonacci(n): Generates the first n numbers of the Fibonacci series and returns only the even numbers. fib_series = fibonacci(n) return [num for num in fib_series if num % 2 == 0]

question:Given an array [3, 5, 1, 9, 7, 2], write a Python function that sorts the array and reverse it.

answer:def sort_and_reverse(arr): Sorts the array in ascending order and then reverses it. arr.sort() arr.reverse() return arr

Released under the chat License.

has loaded