필터 지우기
필터 지우기

Question regarding function inputs

조회 수: 2 (최근 30일)
buxZED
buxZED 2011년 3월 4일
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)"

채택된 답변

Walter Roberson
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
buxZED
buxZED 2011년 3월 4일
"suspect you are thinking in terms of functions such as f(x) = x^2 + 5 . That is a common use of the word "
now its all clear as sunshine
Thank you

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Marco Wu
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
buxZED
buxZED 2011년 3월 4일
im sorry, but I dont understand
so x pontx and pointy are 3 variables?
where is the actual function?
Walter Roberson
Walter Roberson 2011년 3월 4일
The function is the lines you show, the assignment to n and L.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by