How to calculate number of function evaluation used?
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (2)
Torsten
le 20 Juin 2017
0 votes
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 commentaires
NUR ZAFIRAH MOHD SIDEK
le 20 Juin 2017
Jan
le 20 Juin 2017
@NUR ZAFIRAH MOHD SIDEK: Accepting an answer means, that the problem is solved.
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
le 20 Juin 2017
Jan
le 20 Juin 2017
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 commentaires
NUR ZAFIRAH MOHD SIDEK
le 21 Juin 2017
Jan
le 21 Juin 2017
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.
Catégories
En savoir plus sur Introduction to Installation and Licensing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!