MATLAB: Question regarding function inputs

functionvariable

function y=lagrange(x,pointx,pointy)
n=size(pointx,2);
L=ones(n,size(x,2));
In many places in the coding (.m) file I found codes like this (this is from the lagrange method file by "Calzino" from this website)
I understand why we type "y=f(x)" or "y=@x f(x)" but I don't understand what's the reasons we do "y=something (something1,something2,something3)"

Best Answer

Define "lagrange" as a subroutine with one output and three inputs. Within the body of the subroutine, the one output is to be known as "y", and the three inputs are to be known as "x", "pointx", and "pointy" respectively.
Assign a value to the variable "n"; the value should be the size of the second dimension of the array "pointx". The size of the second dimension of an array is the number of columns in the array.
Assign a value to the variable "L"; the value should be an array with double precision value 1, and the array should have "n" rows and should have the same number of columns as the number of columns in "x".
The reason to pass in more than one value to a function is that the function needs more piece of information to do its work.
I suspect you are thinking in terms of functions such as f(x) = x^2 + 5 . That is a common use of the word "function" that applies to numbers, but the technical mathematical meaning of "function" does apply to the way it is used in Matlab (provided you do not alter any outside variables or use a random number generator or print anything or do any I/O or do any graphics...) For your purpose now, it would be easier to put aside that meaning and treat Matlab's use of the word "function" as being jargon for "a block of code that takes 0 or more input values and computes something".
The use with 3 arguments is logically similar to creating a mathematical function such as f(x,y,z) = x^2 + 5*x*y + y^2*z + 2 -- you cannot rewrite that in terms of functions that operate only on a single value.
Related Question