the piecewise function runs out a weird figure
1 回表示 (過去 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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/208052/image.png)
1 件のコメント
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
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Specifying Target for Graphics Output についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!