How to make piece-wise function accept single variables and vectors
Show older comments
I have a piece-wise function that can take a single variable and produce my desired output. However, the problem I am having is when there are vectors instead of single variables. For example, it will not accept vectors (0:1:10) or (1 2 3 4 5), where the code produces no output at all. I have put the x. so that it would do the operations for every variable in a given vector, but it has no effect.
function y = piecewise(x)
if (x>0) & (x<=120) % for 0 < x <= 120
y = ((800*x.^3)-(13.68*(10^6)*x)-(2.5*x.^4)) % Use this equation
elseif (x>120) & (x<=240) % for 120 < x <= 240
y = ((800*x.^3)-(13.68*(10^6)*x)-(2.5*x.^4)+2.5*(x-120)^4) % Use this equation
elseif (x>240) & (x<=360) % for 240 < x <= 360
y = ((800*x.^3)-(13.68*(10^6)*x)-(2.5*x.^4)+2.5*(x-120)^4+600*(x-240)^3) % Use this equation
end
Accepted Answer
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!