Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot a function defined with def?

I have a function

np.sin(x / 2.) * np.exp(x / 4.) + 6. * np.exp(-x / 4.)

and I can plot it by using following code:

x = np.arange(-5, 15, 2)
y = np.sin(x / 2.) * np.exp(x / 4.) + 6. * np.exp(-x / 4.)
plt.plot(x, y)
plt.show()

but if I define function plotting doesn't work:

rr = np.arange(-5, 15, 2)

def y(o): 
    return np.sin(o / 2.) * np.exp(o / 4.) + 6. * np.exp(-o / 4.)

def h(b):
    return int(y(b))

plt.plot(rr, h)
plt.show()

Why does it happen, and how could I change the code to plot the function?

like image 291
Akmal Salikhov Avatar asked Apr 22 '16 07:04

Akmal Salikhov


Video Answer


1 Answers

Try this instead:

import numpy as np
import matplotlib.pyplot as plt

rr = np.arange(-5, 15, 2)

def y(o): 
    return np.sin(o / 2.) * np.exp(o / 4.) + 6. * np.exp(-o / 4.)

plt.plot(rr, y(rr).astype(np.int))
plt.show()
like image 144
Hun Avatar answered Oct 12 '22 09:10

Hun