Why is my code turning black after I run?

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

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?

Sign in to comment.

Answers (1)

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])
Operands to the logical AND (&&) and OR (||) operators must be convertible to logical scalar values. Use the ANY or ALL functions to reduce operands to logical scalar values.

Error in solution>f (line 8)
elseif (x>1 && x<=2)
And the brackets around y are not needed in the first line.

Sign in to comment.

Asked:

on 18 Sep 2023

Commented:

Rik
on 18 Sep 2023

Community Treasure Hunt

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

Start Hunting!