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 endend
Best Answer