0

I am trying to write a function to plot a sine graph. The function takes three sine arguments: amplitude being A, frequency being B, horizontal shift being C (see below)

A sin(B(x-C))

This is my code:

import numpy as np
import matplotlib.pylab as plt

x = np.linspace(-np.pi, np.pi, 201)


def sine(amplitude, frequency, horizontal_shift) -> object:
    si = np.sin(x - sine)
    si = si * frequency
    si = amplitude * si
    return si


test = sine(4, 1, 2)
print(test)

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


However, I keep getting this error:

TypeError: unsupported operand type(s) for -: 'float' and 'function'

I have tried to

  • input ints only
  • input numbers as strings
  • defining my function as this:
def sine(amplitude, frequency, horizontal_shift) -> object

but I get the same error.

1
  • What is x - sine supposed to be doing? sine is the function that you just defined. Math isn't defined for functions! Commented Nov 29, 2021 at 20:47

1 Answer 1

1

Couple of things. Firstly you don't want si = np.sin(x - sine). This is what the traceback is referring to. Secondly, your function even then isn't doing the calculation that you gave at the top. Try:

def sine(x, amplitude, frequency, horizontal_shift):
    si = amplitude * np.sin(frequency * (x - horizontal_shift))
    return si

Edit: plot of sample output

plt.plot(np.linspace(-np.pi, np.pi, 201), sine(np.linspace(-np.pi, np.pi, 201), 2 , 5, 7))

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.