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

Question:

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

Asked By: Cawen Tv

||

Answers:

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)
Answered By: Luckk

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()
Answered By: islam abdelmoumen
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.