6-Term Taylor Series Expansion
Show older comments
Hi,
I am trying to solve a 6-term Taylor Series Expansion from x=-1 to x=1. The equation is attached as a jpg.
a2=0;
N=5;
x=-1:0.01:1;
TSeriesByTerm=zeros(length(x),N);
fx=@(u)1./(u.^2);
for ind=1:N
n=ind-1;
TSeriesByTerm(:,ind)=factorial(n+1)/factorial(n).*(x-a2.^n;
end
TSeriesExpansionN=cumsum(TSeriesByTerm,2);
hf=figure;
hold on
plot(x,fx(x),'-k','linewidth',2)
plot(x,TSeriesExpansionN)
line([a2,a2],[min(TSeriesExpansionN(:)),max(TSeriesExpansionN(:))],'color','r','linestyle','--')
ylabel('f(x)')
xlabel('x')
Once I plug this code in MATLAB I get an error, but I'm not sure why.
Thank you so much for your help in advance!
3 Comments
John D'Errico
on 14 Jan 2017
Edited: John D'Errico
on 14 Jan 2017
Not sure what you mean by "solve" it. Unless you are thinking of this as how to solve your homework problem. Solve has an explicit meaning in mathematics. But you did show what you did anyway, and it was a credible try so I'll provide a solution, something we avoid if you make no effort.
You say that you got an error. Usually you will get a better response if you tell us what error you got! Provide the COMPLETE test of the error message. Otherwise, you force us to execute your code just to know what you did wrong.
newbie
on 14 Jan 2017
Image Analyst
on 14 Jan 2017
Look down below. Scroll down to the Answers part of the page.
Answers (1)
John D'Errico
on 14 Jan 2017
Edited: John D'Errico
on 14 Jan 2017
TSE = @(x,n) sum((-3).^(0:n).*(x(:).^(2*(0:n)+4))./factorial(0:n),2);
Note that a 6 term Taylor series has TSE go only to 5, since the zero'th term is the first term. That is, 0:5 is SIX terms.
The above expression will work as long as you are using the current MATLAB release. Older releases will show an error, and want to use bsxfun in there.
So all you need do is a few lines, most of which are just to make a pretty plot.
TSE = @(x,n) sum((-3).^(0:n).*(x(:).^(2*(0:n)+4))./factorial(0:n),2);
x = linspace(-1,1,100);
plot(x,TSE(x,5))
grid on
xlabel X
title 'Six term TSE'
hold on
plot(x,x.^4.*exp(-3*x.^2),'--g')
legend('6 term TSE','x^4*exp(-3*x.^2)')

Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!