the piecewise function runs out a weird figure

2 次查看(过去 30 天)
function codes are as this
function f = myfun(x)
if (x<1);
f=x.^2 ;
else
f=x;
end
end
main program codes are as this
clc;
clear all;
close all;
x=0:0.1:10;
y=myfun(x);
plot(x,y)
a simple figure but a queer figure,why
  1 个评论
Adam Danz
Adam Danz 2019-3-12
Hint from
doc if
"An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric)."

请先登录,再进行评论。

回答(1 个)

Voss
Voss 2022-6-3
x=0:0.1:10;
y=myfun(x);
plot(x,y)
function f = myfun(x)
idx = x < 1;
f = zeros(size(x));
f(idx) = x(idx).^2;
f(~idx) = x(~idx);
end

类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

产品


版本

R2014a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by