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

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)

 채택된 답변

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개)

카테고리

도움말 센터File Exchange에서 Bessel functions에 대해 자세히 알아보기

태그

질문:

2022년 3월 31일

댓글:

2022년 3월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by