Question regarding function inputs
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2011-3-4
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.
更多回答(1 个)
Marco Wu
2011-3-4
"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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!