필터 지우기
필터 지우기

anonymous function issue

조회 수: 1 (최근 30일)
Abdulaziz
Abdulaziz 2012년 4월 19일
Hi guys I need help with this issue
I am using this calculated formula
(((h(n)/3)*(f(c)+Ie+Io+f(d))))
which equals to
(x*exp(1/2 - x))/24 + (x*exp(1/4 - x))/12
where, f=@(y) (x*y*exp(y-x)),
then i wrote it as
g = @(x)(((h(n)/3)*(f(c)+Ie+Io+f(d))))
then i tried which is in my code
>> g(2)
ans =
(x*exp(1/2 - x))/24 + (x*exp(1/4 - x))/12
I am wondering why it gives me the same formula without substituting x as 2

채택된 답변

Walter Roberson
Walter Roberson 2012년 4월 19일
You use x as the dummy parameter for g, but you do not use x within the formula.
In order for your code not to crash you would have to have defined x as symbolic or given x a value before you defined f, and it would be that symbol or that value that would be "captured" in the definition of f.
For example,
x = 5;
f = @(y) 3 + x + y;
x = 19;
f(7)
then the value "x = 5" gets "captured" when f is defined so even though you changed x after you define f, the value 5 will be used not the current x.
  댓글 수: 1
Abdulaziz
Abdulaziz 2012년 4월 20일
thanks your notes drives me to my mistake

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by