I want to plot f(x) = x^(2/3) using python, however, I'm only able to get it to plot for the positive values of x.
Does anyone have any ideas where I'm going wrong?
import numpy as np
import matplotlib.pyplot as plt
# Define the function
def f(x):
return x ** (2/3)
# Generate x values for both positive and negative x
x_positive = np.linspace(0, 10, 200)
x_negative = np.linspace(-10, 0, 200)
# Calculate corresponding y values for both sets of x values
y_positive = f(x_positive)
y_negative = f(x_negative)
# Plot
plt.figure(figsize=(8, 6))
# Plot for positive x
plt.plot(x_positive, y_positive, label=r'$f(x) = x^{2/3}$', color='blue')
# Plot for negative x
plt.plot(x_negative, y_negative, linestyle='dashed', color='blue')
plt.title('f(x) = x^(2/3)')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid(True)
plt.legend()
plt.ylim(-10, 10)
plt.xlim(-10, 10)
plt.axhline(y=0, color='black', linewidth=0.8) # x-axis
plt.axvline(x=0, color='black', linewidth=0.8) # y-axis
plt.show()
Which results in a warning, and a plot with half the x range.
C:\Users\...\AppData\Local\Temp\ipykernel_35264\1241025677.py:3: RuntimeWarning: invalid value encountered in power
return x ** (2/3)

The reason only the plot for positive x values is visible in the graph of the function f(x) = x^(2/3) is because raising a negative number to the power of 2/3 results in a complex number. In a 2d graph, complex numbers cannot be directly plotted. So, the graph only shows the portion corresponding to the real values of x - which are the positive values. But you can plot the absolute values of the negative x values and indicate the negative sign in the plot.