MATLAB: Write a three_max function

function

* not a homework question. This is a practice question for an upcoming exam
Write a function, three_max, which takes three numbers, x1, x2, x3 and outputs the largest number. Do this without using the variety of built-in functions associated with max and sort. Test it.
I have created a function but when ever I try to test it I get a "not enough input arguments" for line 2: if (x1>x2 && x1>x3). I do not know what I am doing wrong
function three_max(x1,x2,x3)
if (x1>x2 && x1>x3)
max=x1;
elseif (x2>x1 && x2>x3)
max=x2;
elseif (x3>x1 && x3>x1)
max=x3;
end
end

Best Answer

There are several issues with your code.
  1. It does not return an output.
  2. It overloads the max() function, which can lead to confusing issues.
  3. Your third conditional (which isn't necessary at all) compares against x1 twice.
Finally, I do not get the stated error when I run this code. It must be how you are calling it. Perhaps you have done:
three_max(x1,x2)
The function requires three inputs.
Related Question