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

1 次查看(过去 30 天)
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
help please

回答(1 个)

Arnab Sen
Arnab Sen 2016-4-25
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.

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by