define function in one line
28 ビュー (過去 30 日間)
古いコメントを表示
How can I define vector p without using function statement?
suppose
t = 0:0.1:1
p = sin(t) when t<0.6
p = 0 when t>=0.6
2 件のコメント
採用された回答
Rik
2024 年 2 月 23 日
I don't know if you want to avoid anonymous functions as well, but this should give you a finer plot:
% p = sin(t) when t<0.6
% p = 0 when t>=0.6
p = @(t) (t<0.6).*sin(t);
fplot(p,[0 1])
The problem here is mostly that sin(x) is very close to x for small x, which means you have a fairly straight line. Adding a straight line helps to show there is actually a slight curve:
p = @(t) (t<0.6).*sin(t);
figure
fplot(p,[0 1])
hold on
fplot(@(t) sign(p(t)).*t,[0 1])
legend({'y = sin(t)','y = x'})
2 件のコメント
Rik
2024 年 2 月 23 日
You're welcome. If my answer solved your issue, please consider marking it as accepted.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!