Write a Python script to plot f(x) = x^x - x^3 , where x is a real number, ^ -…
- User
Write a Python script to plot f(x) = x^x - x^3 , where x is a real number, ^ - exponentiation.
Assistant
Here's a Python script to plot the function f(x) = x^x - x³:
import numpy as np import matplotlib.pyplot as plt # Define the function def f(x): return x**x - x**3 # Create x values # Note: x^x is only defined for positive x in real numbers x = np.linspace(0.01, 3, 1000) # Start from 0.01 to avoid x=0 # Calculate y values y = f(x) # Create the plot plt.figure(figsize=(10, 6)) plt.plot(x, y, 'b-', linewidth=2, label=r'$f(x) = x^x - x^3$') # Add grid plt.grid(True, alpha=0.3) # Add labels and title plt.xlabel('x', fontsize=12) plt.ylabel('f(x)', fontsize=12) plt.title(r'Plot of $f(x) = x^x - x^3$', fontsize=14) # Add horizontal line at y=0 plt.axhline(y=0, color='k', linewidth=0.5) # Add legend plt.legend(fontsize=12) # Show the plot plt.show() # Optional: Find and print some interesting points # Find approximate zeros from scipy.optimize import fsolve # Initial guesses for zeros initial_guesses = [0.5, 1.5, 2.5] zeros = [] for guess in initial_guesses: try: zero = fsolve(f, guess)[0] if 0 < zero < 3 and abs(f(zero)) < 1e-10: zeros.append(zero) except: pass # Remove duplicates zeros = list(set([round(z, 6) for z in zeros])) zeros.sort() print("Approximate zeros of f(x):") for zero in zeros: print(f"x ≈ {zero:.6f}, f(x) ≈ {f(zero):.2e}") # Find the minimum from scipy.optimize import minimize_scalar result = minimize_scalar(f, bounds=(0.01, 3), method='bounded') print(f"\nMinimum point: x ≈ {result.x:.6f}, f(x) ≈ {result.fun:.6f}")This script:
- Defines the function f(x) = x^x - x³
- Creates an array of x values from 0.01 to 3 (avoiding x=0 since 0^0 is undefined)
- Calculates the corresponding y values
- Creates a plot with proper labels and formatting
- Optionally finds and prints the zeros and minimum of the function
The function has some interesting properties:
- It has zeros near x ≈ 0.774, x ≈ 1.000, and x ≈ 2.478
- It has a local minimum around x ≈ 0.602
- For x > 1, x^x grows much faster than x³, so the function increases rapidly
You can adjust the range of x values or the plot styling as needed for your specific requirements.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.