绘制函数时出错

时间:2014-04-02 14:56:57

标签: matlab function

我正在尝试构建并绘制一个幅度为1的Triangular函数,从第二个1开始,在第二个3中达到最大值,在第二个4中返回到零,

  • x(A, t1, t2, t3, t) = A/t2-t1 * (t-t1)t1 <= t <= t2
  • x(A, t1, t2, t3, t) = A/t2-t3 * (t-t3)t2 <= t < t3
  • x(A, t1, t2, t3, t) = 0其他地方

问题是我找不到将这两个条件添加到函数的方法,即使有一个条件我一直收到这个错误:

Error in Untitled1 (line 6)
x2 = mytri(A,t1,t2,t3,t);

这是我到目前为止所做的,请给我一个如何添加它们并且不会出错的想法:

fs = 20;       %freq
t = 0:1/fs:5;
t1=1;
t2=3;
t3=4;
A=1;           %amplitude

x2 = mytri(A, t1, t2, t3, t);
plot (t,x2,'.-')
axis([ -2 5 -2 5])

功能:

function x2 = mytri(A, t1, t2, t3, t)
    x2 = A/t2 - t1*t - t1*(t1 <= t <= t2);

1 个答案:

答案 0 :(得分:0)

这是一个示例,在给定波形的百分比/占空比和周期T的情况下手动创建三角波形的上升和下降部分。

T = 3;              % period in secs
rise_pct = 2/3;     % percentage of wave rising
fall_pct = 1/3;     % percentage of wave falling
dt = 0.01;          % time interval

N_repeats = 2;

rise = linspace(0,1,T*rise_time/dt);    % create rising section
fall = linspace(1,0,T*fall_time/dt);    % create fallingsection
wave = [rise,fall];                     % join rise and fall strokes

wave = repmat(wave,1,N_repeats); %repeat wave

t = (1:length(wave)).*dt + 1; %time vec starting at 1sec
plot(t,wave);