matlab分段函数画图,哪里有问题
x=-800:0.1:800;
if x<-500
y=-sqrt(300^2-(x+500).^2)
else if (-500<=x && x<=500)
y=300*sin(pi/1000*x)
else
y=sqrt(300^2-(x-500)^2)
end
end
plot(x,y) 展开
%%%这样就可以了
clear;clc
x=-800:0.1:800;
for ii=1:length(x)
if x(ii)<-500
y(ii)=-sqrt(300^2-(x(ii)+500)^2);
elseif (-500<=x(ii) & x(ii)<=500)
y(ii)=300*sin(pi/1000*x(ii));
else
y(ii)=sqrt(300^2-(x(ii)-500)^2);
end
end
plot(x,y)
**************************************
%%%或者
clear;clc
for x=-800:0.1:800
if x<-500
y=-sqrt(300^2-(x+500)^2);
elseif (-500<=x && x<=500)
y=300*sin(pi/1000*x);
else
y=sqrt(300^2-(x-500)^2);
end
plot(x,y)
hold on
end
------------------------