the piecewise function runs out a weird figure

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 Comment

Hint from
doc if
"An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric)."

Sign in to comment.

Answers (1)

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

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Release

R2014a

Asked:

on 12 Mar 2019

Edited:

on 6 Jun 2022

Community Treasure Hunt

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

Start Hunting!