# I need to generate in a separate window, the absolute difference (error) between the two functions.

1 view (last 30 days)
Kosmas-Damianos Giannakos on 21 Apr 2016
Answered: Arnab Sen on 25 Apr 2016
syms k x
clf;
%%%%%User Input %%%%%%%
a=@(k)-1.^k; % define coefficient sequence
f=@(x)symsum(a(k).*x.^k/k^2,k,1,inf); %define series
p=@(x,n)symsum(a(k).*x.^k/k^2,k,1,n); % define partial sum
n=5; % degree of partial sum
x1=-1;x2=1; % endpoints of plotting interval
%%%%%End User Input %%%%%%%
x=linspace(x1,x2);
subplot(1,2,1);
plot(x,f(x),'-.'); % plot limit function
hold on
plot(x,p(x,n)); % plot partial sum; leave default style
legend('series',['n = ', num2str(n)]);
% num2str converts n into a string
title(['Series \Sigma ',char(a(k)), '*x.^k/k^2 and Partial Sum' ]);
hold off
syms x
tpoly=taylor(f,x,'order',10); subplot(1,2,2);
% defines taylor polynomial
% expression
p=@(x)subs(tpoly);
% taylor polynomialfunction
approx_error= abs(p(pi/2)-f(pi/2)); % absolute difference in approximation
this is my code , but i am having trouble to generate the absolute difference

Arnab Sen on 25 Apr 2016
Hello Giannakos, This script outputs approx._output as NAN becasse f(pi/2) is evaluated to be NAN. The anonymous function 'f' is defined to be as
f(x)= -x -x^2/4-x^3/9-x^4/16-....x^i/i^2-... to infinite
Now, notice that the series is divergent whenever x>1, because the numerator grows exponentially whereas the growth of the denominator is polynomial. So the sum keeps on increasing as we proceed to infinite. Hence, MATLAB evaluates the sum to be NAN. So
f(pi/2)=NAN
Verify the appropriate definition of Taylor series. In Taylor series, ith term is something like x^i/i! and hence converges.