-1
import math 
import matplotlib.pyplot as plt

import numpy as np

x = np.arange(-4,5,1)

y=(math.sqrt(math.cos(x)))*(math.cos(300*x))+(math.sqrt(abs(x))-0.7*(4-x*x)**0.1) 


plt.plot(x,y)
plt.show()

I want to plot this function. It's simple, but I'm having trouble with this. When I run the program, it says:

File "funct.py", line 7, in <module>
    y=(math.sqrt(math.cos(x)))*(math.cos(300*x))+(math.sqrt(abs(x))-0.7*(4-x*x)**0.1) 
TypeError: only length-1 arrays can be converted to Python scalar
Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56

1 Answers1

1

Only the numpy versions of the functions, not the ones from math or builtins, can broadcast instead of processing a scalar argument. You want this:

y = (np.sqrt(np.cos(x)))*(np.cos(300*x))+(np.sqrt(np.abs(x))-0.7*(4-x*x)**0.1)

Granted, this has its own problem: np.cos(x) gives some negative values, which you attempt to square-root.

Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56