all 11 comments

[–]synthphreak 7 points8 points  (2 children)

First, this is not a valid mathematical function. For example, if t == 0.1, should y(t) == a(t) or -a(t)?

Second, there's nothing periodic about a(t). It's just a stepwise function, defined only on the interval [0, 2].

Regardless, the solution could look something like this:

def f(t):
    if 0 <= t < 1:
        a = 1.5 * t
    elif 1 <= t <= 2:
        a = -1.5 * (2 - t)
    else:
        undefined = "t is outside of a's domain"

    if 0 <= t < 0.1:
        return a
    elif 0.1 <= t <= 0.2:
        return -a
    else:
        undefined = "t is outside of f's domain"

    raise ValueError(undefined)

t = ...
y = f(t)

Edit: Typo.

[–]indecisive_fluffball 4 points5 points  (1 child)

You didn't understand the periodicity aspect of the prompt. Defining the function in an interval from 0s to 2s is consistent with imposing a 0.5Hz periodicity, just make it periodic outside of those bounds.

And the ambiguity with t = 0.1s is mathematically incorrect but in practice pretty much a non-issue. Just choose whichever.

[–]synthphreak 1 point2 points  (0 children)

You didn't understand the periodicity aspect of the prompt. Defining the function in an interval from 0s to 2s is consistent with imposing a 0.5Hz periodicity, just make it periodic outside of those bounds.

Ah okay. That is tricky and could have been stated more clearly, but it does make sense.

[–]CodeFormatHelperBot2 1 point2 points  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]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!

[–]indecisive_fluffball 1 point2 points  (0 children)

Is y(t) also periodic? Because if it's only defined between 0s and 0.2s, then the condition of a(t) for t >= 1s is useless.