0

嗨,我有这个分段函数及其图:余弦和右图

我应该在 python 中分段实现,并在下一个得到相同的图表。我的代码是这样的:

import numpy as np 
import matplotlib.pyplot as plt

def f(time):

    T   = time 
    Ts1 = 0.3
    Ts2 = 0.4
    ind = 0

    t_axis = np.linspace(0,T,100)
    ft=np.arange(100.0)

    for t in t_axis:

        if t<Ts1:
            ft[ind] = (np.cos(np.pi*t/Ts1))
            ind += 1
        elif Ts1<=t<Ts2:
            ft[ind] = (np.cos(np.pi*(t+Ts2-Ts1)/(Ts1)))
            ind += 1
        else:
            ft[ind] = (0)
            ind +=1   

return ft,t_axis

a,b = f(1)
plt.plot(b,a)
plt.show()

不幸的是,我得到了这个奇怪的图表:

我奇怪的图表

{{{ 我没有做 e(t) 和常数的乘法。因为最终这不能改变图表(我发现只是带来了一些放大或电平转换。)}}}

这是我的问题:

1-我的代码适用于该功能吗?

2-可能与 Ts1 和 Ts2 的值有关。因为我是随机选择的。

3-如果我获得了正确的图表,我应该如何更改余弦参数以便在几个时期内可视化图表?

4

1 回答 1

0

我不确定第一个链接的图表代表什么。

1 - 您的代码适用于e(t)功能。

2 - 如果我们谈论图表中的不连续性,它可能与给定值有关。您可以尝试 Ts1=0.2, Ts2=0.6 并在方程中加 1。

ft[ind] = (np.cos(np.pi*(t+Ts2-Ts1)/(Ts1))) + 1像那样。

3 - 我认为,最简单的方法是在 for 循环的开头添加以下行:

t = t % period

通过应用其中的一些步骤,我得到了这样的结果:

在此处输入图像描述

不确定它是否是你想要的。如果您想要第一个链接中的输出,那么您应该提供有关该图的更多信息。为什么有两个情节?什么是 E(la) 和 E(lv)?等等

更新 :

生成上图的代码:

import numpy as np 
import matplotlib.pyplot as plt

def f(time):

    T   = time 
    Ts1 = 0.2
    Ts2 = 0.6
    ind = 0
    period = 1

    t_axis = np.linspace(0,T,1000)
    ft=np.arange(1000.0)

    for t in t_axis:

        t = t % period
        if t<Ts1:
            ft[ind] = (np.cos(np.pi*t/Ts1)) + 1
            ind += 1
        elif Ts1<=t<Ts2:
            ft[ind] = (np.cos(np.pi*(t+Ts2-Ts1)/(Ts1))) + 1
            ind += 1
        else:
            ft[ind] = (0)
            ind +=1   

    return ft,t_axis

a,b = f(2)
plt.plot(b,a)
plt.show()
于 2018-03-28T13:20:36.450 回答