필터 지우기
필터 지우기

How to calculate number of function evaluation used?

조회 수: 16 (최근 30일)
NUR ZAFIRAH MOHD SIDEK
NUR ZAFIRAH MOHD SIDEK 2017년 6월 20일
편집: Tamas Kis 2022년 12월 23일
Hi guys, can some one help me to solve my problem?
i've be facing difficulty to find the number of function evaluation. let's say that i have f(x)=x^3+x^2-2 with initial value of x(0)=1.5
i want to calculate x(n+1)=x(n)-(f(x(n)/fprime(x(n))) until n=5
thus, i want to calculate how many time of f(x(n)) have been use until n=5. but if the first iteration has use f(x(1)) then the second iteration also use f(x(1)), it is considered as we only use f(x(1)) one time only.
can someone help me
  댓글 수: 2
KSSV
KSSV 2017년 6월 20일
What is fprime ?
Jan
Jan 2017년 6월 20일
The obvious answer is, that f(x) is called n=5 times.

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

채택된 답변

Steven Lord
Steven Lord 2017년 6월 20일
If you're using release R2017a or later you could use memoizedFunction. Once you've used memoizedFunction to solve the problem, call stats on it to determine how many times and with which inputs it was called.
  댓글 수: 3
Steven Lord
Steven Lord 2017년 6월 21일
Yes, that's how I would do it. The first time you call the function with a new set of inputs, that increments TotalMisses. Each subsequent time you call the function with inputs that are already in the cache, that increments TotalHits.
Tamas Kis
Tamas Kis 2022년 12월 23일
편집: Tamas Kis 2022년 12월 23일
Found a way to do this using a handle class: https://www.mathworks.com/matlabcentral/fileexchange/122422-counted-function-countedfunction. In an example I included it is about 5 times faster than using a memoized function.
If you have a function handle f, you can simply do
[f,count] = count_function_calls(f)
and keep using f in the same way as before. When you want to know the number of function calls, you just use
count()

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

추가 답변 (2개)

Torsten
Torsten 2017년 6월 20일
When n=5, f and fprime have been called 5 times each, namely to evaluate f(x1),f(x2),...,f(x5), fprime(x1),fprime(x2),...,fprime(x5).
Best wishes
Torsten.
  댓글 수: 4
Torsten
Torsten 2017년 6월 20일
편집: Torsten 2017년 6월 20일
f=@(x)x^3+x^2-2;
fprime=@(x)3*x^2+2*x;
x=4;
for n=1:5
x=x-f(x)/fprime(x);
end
Best wishes
Torsten.
NUR ZAFIRAH MOHD SIDEK
NUR ZAFIRAH MOHD SIDEK 2017년 6월 20일

@Jan Simon: ops..sorry..because this is my first time.

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


Jan
Jan 2017년 6월 20일
function fx = f(x)
persistent Count
if isempty(Count)
Count = 0;
end
if nargin == 0 % Reply counter and reset it
fx = Count;
Count = 0;
return;
end
fx = x^3+x^2-2;
Count = Count + 1;
end
Now:
f();
x = f(1) + f(2) + f(3);
Count = f()
  댓글 수: 2
NUR ZAFIRAH MOHD SIDEK
NUR ZAFIRAH MOHD SIDEK 2017년 6월 21일
and is it the same process if i want to find more than one function at the same time?
Jan
Jan 2017년 6월 21일
I do not understand you question. This example shows how to insert and request a counter ina function. Of course you can do exactly the same with other functions also.

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

카테고리

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

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by