Anonymous functions not acting as they should?

1 回表示 (過去 30 日間)
Cameron
Cameron 2013 年 4 月 9 日
Hi,
In the following code I have two anonymous functions that work fine when the function is worked out by hand, iteration by iteration, but choke up by the fourth iteration as apre depreciates linearly to zero (not supposed to happen) and macexp levels off at 10.667 (not supposed to happen) before going directly to zero (not supposed to happen). In the version of this code which does not include any anonymous functions apre diminishes exponentially towards zero and macexp increases towards 55 (with x=4, se=.01). The code is to model the taylor series for exp(x).
Code:
x = input('Input the value of x to be approximated: ');
se = input('Input the specified error (se): ');
fprintf('\nn\t\tSeries Approx\t\tapre\n');
macexp=zeros(1,100);
approx=zeros(1,100);
apre=100;
fmacexp=@(x,n) macexp(n)+((x^n)/factorial(n));
fapre=@(approx,n) (100*((approx(n+1)-approx(n))/approx(n+1)));
for n=1:100;
macexp(n+1)=fmacexp(x,n);
apre=fapre(macexp,n);
fprintf('\n%1.0f\t\t%1.5f\t\t\t%1.5f\t',n,macexp(n+1),apre);
if apre<se
break
end
end
  2 件のコメント
Yao Li
Yao Li 2013 年 4 月 9 日
編集済み: Yao Li 2013 年 4 月 9 日
I'm not sure, but maybe it is because of factorial(n).
Since double precision numbers only have about 15 digits, the answer is only accurate for n <= 21. For larger n, the answer will have the right magnitude, and is accurate for the first 15 digits.
Sean de Wolski
Sean de Wolski 2013 年 4 月 9 日
factorial(18)
is the largest factorial that can be evenly represented as an integer in double precision:
find(factorial(1:30)>flintmax,1,'first')-1

サインインしてコメントする。

採用された回答

Jonathan Epperl
Jonathan Epperl 2013 年 4 月 9 日
I think the mistake happens here:
fmacexp=@(x,n) macexp(n)+((x^n)/factorial(n));
The value of macexp is now "hardwired" with its current valued into your function famcexp, i.e. when you call macexp in your loop, macexp executes with a value of macexp=zeros(1,100) every time, not with the updated value.
What Yao commented you should also consider though, those factorials are going to get gigantic very quickly.
  3 件のコメント
Yao Li
Yao Li 2013 年 4 月 9 日
So, do you think it will be better to define the functions as a real function rather than anonymous functions since currently the two anonymous functions are defined for 100 times
Teja Muppirala
Teja Muppirala 2013 年 4 月 9 日
For something this simple, it doesn't really matter, it's just a matter of milliseconds anyways. If efficiency is really an issue, then certainly you don't want to be redefining function handles all the time.
I would have probably written it like this outside the loop just once:
fmacexp=@(x,n) ((x^n)/factorial(n));
And then inside the loop do
macexp(n+1) = macexp(n+1) + fmacexp(x,n)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by