MATLAB: Taylor function gives wrong answer for symbolic expression expansion

taylortaylor wrong

Hello, I'm having a problem using the taylor function.
I have a symbolic expression
phi
that yields:
phi
phi =
(4506344770847535*pi*((0.40794*lambda^2)/(lambda^2 - 0.0135117376) + (0.89748*lambda^2)/(lambda^2 - 97.93477444) + (0.69617*lambda^2)/(lambda^2 - 0.004679107216) + 1)^(1/2))/(2251799813685248*lambda)
Now I have calculated the first 3 terms of the taylor series, meaning zero order, 1st and 2nd order terms of the expansion, and I wanted to compare the series expansion that I made with the one that the taylor function would calculate.
In the code I wrote to calculate the terms, nDerivatives is a cell containing a structure with the different derivatives of refraction_index{1}, for example nDerivatives{1}.derivative1st it's the first derivative of refraction_index{1}. Here is the code with the calculations that I made:
lambda = symvar(refractive_index{1});
phi0 = 2.*pi./lambda0.*L(1).*subs(refractive_index{1},lambda,lambda0);%Zero order term.
phi1 = L(1).*2.*pi./lambda.*(lambda0-lambda)./lambda0.*(subs(refractive_index{1},lambda,lambda0)...
-lambda0.*subs(nDerivatives{1}.derivative1st,lambda,lambda0));%1st order term.
phi2 = L(1).*pi.*lambda0./lambda.^2.*(lambda0-lambda).^2.*...
subs(nDerivatives{1}.derivative2nd,lambda,lambda0);%2nd order term.
%Comparison of the second order expansion should be enough to tell if the
%taylor matlab function does a good job or not.
phi_paper = phi0 + phi1 + phi2;
Now I calculated the expansion also with the taylor function:
phi_taylor = taylor(phi(1),symvar(phi(1)),'Order',2,'ExpansionPoint',lambda0);
%The taylor expansion up to the second order gave the result
%33.641452458295232847332851430229 - 30.824184817508774864415075915539*lambda
That cannot be possible because the second order expansion MUST have the term (lambda0-lambda).^2.
Does anyone have an idea if I used wrong the taylor function or is something else is the problem?

Best Answer

taylor with a given order, N, will have the independent variable to at most the power (N-1) . If you want squared terms then you need to use at least order 3.