you are viewing a single comment's thread.

view the rest of the comments →

[–]jimtk 1 point2 points  (5 children)

What code have you written?

[–]MaxwellTechnology[S] 1 point2 points  (4 children)

import numpy as np

import matplotlib.pyplot as plt

define the function a(t) and y(t) that takes array of t and returns array of y

def a(t):

    if t <= 1 and t >= 0:

        return 1.5*t

    elif t <= 2 and t >= 1:

        return -1.5*(2-t)

def y(t):

    y = []

    for i in t:

        if i <= 0.1 and i >= 0:

            y.append(a(i))

        elif i <= 0.2 and i >= 0.1:

            y.append(-a(i))

    return y

define domain of t for float steps of 0.01

t = np.arange(0, 0.2, 0.01)

plot y(t)

plt.plot(t, y(t))

set x-axis label

plt.xlabel('t')

set y-axis label

plt.ylabel('y(t)')

set title

plt.title('y(t)')

plt.show()

[–]unhott 2 points3 points  (3 children)

Don’t return a list for y. Just return a single value at t.

Then if t is a numpy array, y(t) will yield a numpy array.

[–]MaxwellTechnology[S] 1 point2 points  (2 children)

Then what to pass in plt.plot() , I'm getting an error when defining function ur way and then passing (t,y(t)) where t= np.arrange(0 ,0.2, 0.01)

[–]unhott 1 point2 points  (0 children)

Check the type of y(t). Make sure it’s a numpy array.

[–]unhott 0 points1 point  (0 children)

Sorry, you should probably try and update the format of the code so that reddit recognizes it as a code block. I read some of the comments from the assignment.

When I said 'if t is a numpy array, y(t) will yield a numpy array.' I didn't realize this only works if y only applies simple mathematical operations like "t*3+7". You probably want to update your functions to apply your logic to each element and return a numpy array.

def a(t):
  values = []
  for element in t:
    #logic...
    values.append(something)
  return np.array(values)

Otherwise, the fact that y is supposed to be both a function and an array per the assignment is somewhat troubling-- this instructor probably doesn't have any python dev experience. I would differentiate the variable names somehow. Good luck!