matlab中分段函数怎么画??

syms x t
for t=[0:0.25:11]
if t>0&t<=2
y=0.2;
plot(t,subs(y));
else if t>2&t<=5
y=2.5*t-4.8;
plot(t,subs(y));
else if t>5&t<=8
s=exp(-(x-6.4)^2/0.29^2)/(sqrt(2*pi)*0.29);
y=5.2-(8.25*int(s,5,'t')/40)*(t-2)/3;
plot(t,subs(y));
else
y=4.79375;
plot(t,subs(y));
end
end
end
end
这个程序怎么画不出图形呢??
刚开始开始用matlab,请见谅...

你这个程序问题太多了

    不推荐使用for循环。

    你每得出一组y就画一次曲线,最终得出的是关于t的四条y曲线。

    elseif 应该连用,不然每一个if都要一个end,而且else最好和if在不同的行

    t到底定义成符号变量还是向量呢?

错误还有很多,任重而道远啊。

建议你先多了解一下语法,我这里按照你的意思编程如下:

clear
clc
%第一、二段
t1=[0:0.05:5];
y1 = 0.2*(t1>0&t1<=2) + (2.5.*t1-4.8).*(t1>2&t1<=5);
plot(t1,y1)
%第三段
t2 = [5:0.1:8];
syms x t
s=exp(-(x-6.4)^2/0.29^2)/(sqrt(2*pi)*0.29);
y=5.2-(8.25*int(s,5,'t')/40)*(t-2)/3;
y2 = subs(y,t,t2);
hold on
y2(1) = y1(end);
plot(t2,y2)
%第四段
t3 = [8:0.02:10];
y3 = 4.79375*ones(1,length(t3));
y3(1) = y2(end);
plot(t3,y3)

图如下:

当然方法不唯一,你可以将你的分段函数定义为m函数,然后再调用求取一组函数值。进行绘图。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-03-02
clc;clear
syms x t
yy=[];
for t=[0:0.25:11]
if t>=0 && t<=2
    y=0.2;
  
elseif t>2 && t<=5
        y=2.5*t-4.8;
     
elseif t>5 && t<=8
        s=exp(-(x-6.4)^2/0.29^2)/(sqrt(2*pi)*0.29);
        y=5.2-(8.25*int(s,5,'t')/40)*(t-2)/3;
     y=subs(y,t);
else 
            y=4.79375;
          
end
yy=[yy,y];
end
t=[0:0.25:11];
plot(t,yy)

第2个回答  2019-10-29
第3个回答  2014-03-02
分别plot,在画新的图之前先hold on本回答被提问者采纳
第4个回答  2014-03-02
分开画、hold在一张图里就是了。
相似回答