Matlab——关于分段函数的一个错误写法

clc;clear all
x=-5:0.01:5;
if x<=0 
    y=(x+sqrt(pi))./(exp(2));
else
    y=1/2*(log(x+sqrt(1+x.^2)));
end
plot(x,y);

  

这样的话就是数组和0比较,会只画出第二个图像

正解:

x=-5:0.01:5;
y=((x+sqrt(pi))/exp(2)).*(x<=0)+((1/2)*log(x+sqrt(1+x.^2))).*(x>0);
plot(x,y,'b','linewidth',2);

 

猜你喜欢

转载自www.cnblogs.com/lbq-home/p/9836424.html