One option is to use the built-in taylor function in symbolic math toolbox.

If you want to use your strategy, there are some issues that need to be accounted for

- when you use diff for the evaluation of the derivative, the output vector is one element shorter than the input. For this reason, every time you evaluate the nth order derivative, you must rebuild the x vector. I update the vector xd in the loop, having as coordinates the midpoints of the vector at the precedent iteration.
- the evaluation of the derivative at the point a is not "exact" if you dont use analytical functions but only numerical vectors. So I used an approximate evaluation looking for the first xd coordinate larger than or equal to a

clear all, close all

x = 0:2000;

y = nthroot(x,3);

a = 1728;

N = 5;

fig=figure();

set(fig,'color','white')

plot(x,y,'LineWidth',2);

grid on

xlabel('x')

ylabel('y')

dy = y;

xd = x;

yEst = zeros(size(x));

for n = 0:N

idx = find(xd >= a,1,'first');

yEst = yEst+dy(idx)*(x-a).^n/factorial(n);

dy = diff(dy)./diff(xd);

xd = (xd(2:end)+xd(1:end-1))/2;

end

figure(fig), hold on

plot(x,yEst,'r-','LineWidth',2)

legend('Actual Function','Taylor Series Expansion')

set(gca,'yLim',[min(y) max(y)]);

## 2 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/546398-plotting-a-taylor-series-function-of-x-1-3-centred-at-x-1728#comment_893195

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/546398-plotting-a-taylor-series-function-of-x-1-3-centred-at-x-1728#comment_893195

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/546398-plotting-a-taylor-series-function-of-x-1-3-centred-at-x-1728#comment_893234

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/546398-plotting-a-taylor-series-function-of-x-1-3-centred-at-x-1728#comment_893234

Sign in to comment.