How to create a separate function file for a piecewise function?

2 ビュー (過去 30 日間)
Grigorios Chatziandreou
Grigorios Chatziandreou 2022 年 3 月 31 日
コメント済み: Stephen23 2022 年 3 月 31 日
How do I create a function with the function:
f(x) = -ax+b for 0<x<b/a and f(x)=0 for x>b/a?
I want it in a seprate file.
How do I then use it in my main code? (call it)
  1 件のコメント
Stephen23
Stephen23 2022 年 3 月 31 日
What should be the value for x<=0 ?

サインインしてコメントする。

採用された回答

Torsten
Torsten 2022 年 3 月 31 日
a = 3;
b = 12;
x = 0:0.01:12;
plot(x,f(a,b,x))
function pw = f(a,b,x)
pw = NaN(size(x));
idx = x>0 & x<b/a;
jdx = x>=b/a;
pw(idx) = -a*x(idx)+b;
pw(jdx) = 0;
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSimulink Functions についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by