【发布时间】:2015-03-03 07:29:38
【问题描述】:
如何在 MATLAB 中绘制在不同时间间隔内具有不同定义的函数?
例如函数等于t加1,t在区间-1到0,等于t在0到1区间。我们需要在定义函数后进行移位和缩放。所以请在回答时考虑一下。
我尝试将 t 定义为数组t = [-2:0.01:2] 和f = zeros( length( t ) ),然后在f 中找到与其t 值相对应的位置并为其分配值。
我必须将两个信号相乘(它们是原始信号的移位和采样版本)。我写的代码没有给出想要的输出。
clc
clear
t=(-10:0.01:10);
y=zeros(size(t));
y(t>=-1 & t<=0)=-1*t(t>=-1 & t<=0)-1;
y(t>=0&t<=1)=t(t>=0&t<=1);
y(t>=1&t<=2)=1;
y(t>=2&t<=3)=-1*t(t>=2&t<=3)+3;
subplot(5,1,1);
plot(t,y);
axis([-5 5 -2 2]);
n1=input('enter the shifting parameter-');
n2=input('enter the scaling parameter-');
t=(t+n1)/n2;
subplot(5,1,2);
plot(t,y);
axis([-5 5 -2 2]);
n3=min(t);
n4=max(t);
p=-10:0.01:10;
y1=zeros(size(p));
y1(p>=-2 & p<=-1)=1;
y1(p>=-1&p<=0)=-1;
y1(p>=0&p<=1)=p(p>=0&p<=1)-1;
y1(p>=1&p<=2)=1;
subplot(5,1,3);
plot(p,y1);
axis([-5 5 -2 2]);
m1=input('enter the shifting parameter-');
m2=input('enter the scaling parameter-');
p=(p+m1)/m2;
subplot(5,1,4);
plot(p,y1);
axis([-5 5 -2 2]);
m3=min(p);
m4=max(p);
a1=max(n3,m3);
a2=min(n4,m4);
r=a1:0.01:a2;
y2=zeros(size(r));
y2=y(r<=a2&r>=a1).*y1(r>=a1&r<=a2);
subplot(5,1,5);
plot(r,y2);
axis([-5 5 -2 2]);
【问题讨论】:
-
请将您尝试过的代码添加到您的帖子中,它会显示您已经尝试过,它可能会帮助人们更好地理解您的请求并可能对您有所帮助。
-
我也更新了问题和代码。
-
通过查找输入向量的位置来定义函数并分配结果的部分不是最有效的,但对我来说看起来没问题。它对我来说运行良好(没有错误)。这段代码产生了 5 个图,你在哪个阶段对结果不满意?
-
最后阶段。我猜这两个函数相乘的最终结果不是想要的。它不正确。核实。运行时错误。