Not getting the right output for my if statement? [piecewise function]
Show older comments
(Ajay deleted this so I (MF) am restoring it).
% code
function y = FcnEval(x)
if x < -2
y= -1./ ((x.^2) + 1)
elseif -2 <= x < -1
y= sin(x)
elseif -1 <= x < 1
y= x.^2
elseif 1 <= x < 2
y=2
else x >= 2
y=1./(x+1)
end
but when I run the test case: x1 = 3; x2 = -2:2;
y1 = FcnEval(x1)
y2 = FcnEval(x2)
I'm supposed to get y1 = 0.2500
y2= -0.9093 1.0000 0 2.0000 0.3333
what am i doing wrong?
3 Comments
Walter Roberson
on 8 Oct 2012
Meaningful part of the question has been edited out of existence by the original poster :(
Walter Roberson
on 8 Oct 2012
There is no such thing as an "if loop", just if statements.
Matt Fig
on 9 Oct 2012
Saved from google cache:
% code
function y = FcnEval(x)
if x < -2
y= -1./ ((x.^2) + 1)
elseif -2 <= x < -1
y= sin(x)
elseif -1 <= x < 1
y= x.^2
elseif 1 <= x < 2
y=2
else x >= 2
y=1./(x+1)
end
but when I run the test case: x1 = 3; x2 = -2:2;
y1 = FcnEval(x1)
y2 = FcnEval(x2)
I'm supposed to get y1 = 0.2500
y2= -0.9093 1.0000 0 2.0000 0.3333
what am i doing wrong?
Accepted Answer
More Answers (0)
Categories
Find more on Performance and Memory 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!