Home > Blockchain >  Why my code is giving the error name 'sin' is not defined?
Why my code is giving the error name 'sin' is not defined?

Time:11-01

I'm trying to implement a code for the following function: f(x) = sen(2πx), x ∈ [0, 2], to get the graph of it. But I'm getting the return that the sine is not defined. I'm not quite understanding what I would have to correct. I would be grateful if someone can help me

Code I used:

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

def f(x): return sin*(2*np.pi*x)  
  
x = np.linspace(0,2)  
plt.plot(x, f(x))    
plt.grid()  
plt.show()

This was the only code I thought of to solve this issue, because I thought it would print correctly and without errors

CodePudding user response:

Because the sine function is defined inside numpy, so you should explicitly specify the library where the function is defined:

def f(x): return np.sin*(2*np.pi*x)

CodePudding user response:

The problem is that you are trying to use the sin function without import it. You can find the function in the numpy package:

import numpy as np
import matplotlib.pyplot as plt

def f(x):
    return np.sin(2*np.pi*x)  

x = np.linspace(0,2)  
plt.plot(x, f(x))    
plt.grid()  
plt.show()
  • Related