Question regarding function inputs
1 view (last 30 days)
Show older comments
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)"
0 Comments
Accepted Answer
Walter Roberson
on 4 Mar 2011
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.
More Answers (1)
Marco Wu
on 4 Mar 2011
"function y=lagrange(x,pointx,pointy)"
is the standard form to CREATE a function in matlab. This time the function is called lagrange and have 3 inputs (x, pointx and pointy)
When you USE the SAME function, you will type
y=lagrange(x,pointx,pointy)
Please vote me if you think it is clear
2 Comments
See Also
Categories
Find more on Cell Arrays 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!