我需要将原始输入转换为函数
dt = 0.01;
t1 = 0:dt:1;
t2 = 1+dt:dt:2.5;
t3 = 2.5+dt:dt:3.5;
t4 = 3.5+dt:dt:4;
t5 = 4+dt:dt:4.5;
y1 = ones(size(t1))*-1;
y2 = 2*(t2-2.5);
y3 = 1*sin(2*pi*(t3-1));
y4 = ones(size(t4))*0;
y5 = rand( size(t5) )-0.5;;
plot(t1, y1, t2, y2, t3, y3, t4, y4, t5 ,y5)
“这个工作正常^^^^。”
我已经完成了,但脚本中出了问题。
我在哪里弄错了?
现在看起来如何
function [t1, y1, t2, y2, t3, y3, t4, y4, t5 ,y5]=funct(t)
t = 0.01; % Its every step (0<=t) = (0<=0.01)
t1 =t((0<=t)&(t<1));
y1 = ones(size(t1))*-1;
t2 =t((1<=t)&(t<2.5));
y2 = 2*(t2-2.5);
t3 = t((2.5<=t)&(t<3.5));
y3 = 1*sin(2*pi*(t3-1)); %you wrote sn here
t4 =t((3.5<=t)&(t<4));
y4 = ones(size(t4))*0;
t5=t((4<=t)&(t<4.5));
y5 = rand( size(t5) )-0.5;
plot(t1, y1, t2, y2, t3, y3, t4, y4, t5 ,y5)
答案 0 :(得分:0)
我获得了相同的结果。 首先我运行你的代码:
dt = 0.01;
t1 = 0:dt:1;
t2 = 1+dt:dt:2.5;
t3 = 2.5+dt:dt:3.5;
t4 = 3.5+dt:dt:4;
t5 = 4+dt:dt:4.5;
y1 = ones(size(t1))*-1;
y2 = 2*(t2-2.5);
y3 = 1*sin(2*pi*(t3-1));
y4 = ones(size(t4))*0;
y5 = rand( size(t5) )-0.5;;
plot(t1, y1, t2, y2, t3, y3, t4, y4, t5 ,y5)
我尝试运行你的功能后:
t = 0:dt:5;
funct(t)
,其中
function y=funct(t)
%dt = 0.01; %this is not necessary I think
%t1 = 0:dt:1;
t1 =t((0<=t)&(t<1));
y1 = ones(size(t1))*-1;
%t2 = 1+dt:dt:2.5;
t2 =t((1<=t)&(t<2.5));
y2 = 2*(t2-2.5);
%t3 = 2.5+dt:dt:3.5;
t3 = t((2.5<=t)&(t<3.5));
y3 = 1*sin(2*pi*(t3-1)); %you wrote sn here
%t4 = 3.5+dt:dt:4;
t4 =t((3.5<=t)&(t<4));
y4 = ones(size(t4))*0;
%t5 = 4+dt:dt:4.5;
t5=t((4<=t)&(t<4.5));
y5 = rand( size(t5) )-0.5;
y=[y1 y2 y3 y4 y5];
figure
plot(t1, y1, t2, y2, t3, y3, t4, y4, t5 ,y5)
我刚刚在你的代码中纠正了sn-&gt; sin。
如果你试图在函数外部执行plot(t1, y1, t2, y2, t3, y3, t4, y4, t5 ,y5)
它也无法工作,因为它的功能没有足够的输出。
您应该按以下方式修改第一行:
function [t1, y1, t2, y2, t3, y3, t4, y4, t5 ,y5]=funct(t)
希望这会有所帮助。这些错误是不同的吗?