Why is my code turning black after I run?
Show older comments
function [y] = f(x)
if (x==3)
disp('y is undefined at x=3')
elseif (x<=1)
y=exp(x-3);
elseif (x>1 && x<=2)
y=2-3.*x;
elseif (x>2 && x~=3)
y=x/(x-3);
end
end
1 Comment
DGM
on 18 Sep 2023
You forgot to describe the problem. What exactly is turning black? The editor window? Is the text in the editor losing highlighting? What version? Which code? Obviously you have to be calling this function from somewhere else, so is it the calling code that's turning black? Are you calling it from the command window?
Answers (1)
Gulshan
on 18 Sep 2023
There are some issue with your code.
Try this code.
function [y] = f(x)
if (x==3)
disp('y is undefined at x=3');
y = NaN; % Assign NaN (Not-a-Number) when x is 3 to represent undefined value
elseif (x<=1)
y=exp(x-3);
elseif (x>1 && x<=2)
y=2-3.*x;
elseif (x>2 && x~=3)
y=x/(x-3);
end
end
Thanks
1 Comment
I would suggest throwing an error instead.
You also missed this case:
f([1.5 2.5])
And the brackets around y are not needed in the first line.
Categories
Find more on Whos 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!