6-Term Taylor Series Expansion

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
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.
Thank you so much for your response! However, I am unable see a solution in your response. I would love to see how you solved it because I have been working really hard on this code for the past few days.
PS: Could you please reopen my other questions and leave it posted for someone else to answer if you are unable to answer me?
Thank you so much! Have a great day!
Look down below. Scroll down to the Answers part of the page.

Sign in to comment.

Answers (1)

John D'Errico
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

Asked:

on 14 Jan 2017

Commented:

on 14 Jan 2017

Community Treasure Hunt

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

Start Hunting!