How to perform a Taylor Expansion on discrete data
33 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello, I would like to find the taylor coefficients of data given by some vector:
x = -5:0.01:5;
data = 2.^-(x.^2);
plot(x,data)
This 'data' clearly must have some taylor expansion, but the only functions/processes I can find are for symbolic functions.
How can we taylor expand a set of discrete data points?
(I know in this specific case I could've defined the gaussian symbolically, but this is just 'sample data'. I plan to use this on actual measurements)
2 Commenti
Ameer Hamza
il 8 Ott 2020
Can you explain what do you mean by Taylor expansion of discrete data points? Taylor series is defined for functions.
Risposte (3)
Ameer Hamza
il 9 Ott 2020
You can first use lsqcurvefit(): https://www.mathworks.com/help/optim/ug/lsqcurvefit.html or fit(): https://www.mathworks.com/help/curvefit/fit.html to first estimate the Gaussian function from data points. Then use taylor(): https://www.mathworks.com/help/symbolic/sym.taylor.html to compute the taylor series.
0 Commenti
Addy
il 15 Feb 2024
Same dude same. Been searching for while now. I came across a numerical implementation of the Taylor expansion from this website for cos(x). There was couple of issues with the function. I corrected it. But Still, it works only with cos(x). Need to figure it out for discrete data.
clear;clf
c = pi/2; % center point for Taylor series expansion
x = (-4:0.1:6)'; % x values for the function
y = cos(x); % y values for a
figure(1);clf;
plot(x, y, 'g')
hold on;grid on
plot(x, taylor_cosine(c, x, 4), 'ro');
plot(x, taylor_cosine(c, x, 6), 'b-.');
plot(x, taylor_cosine(c, x, 10),'k');
title({'Study of Taylor series',...
['cos(x) expansion at x = π/2 (' num2str(c) ')']})
xlabel('x')
ylabel('cos(x) with different number of terms')
axis([-4 6 -3 3])
grid on
legend('cos(x)','4 term s','6 terms','10 terms','Location','north')
function output = taylor_cosine(c, x, n)
% https://matrixlab-examples.com/taylor-expansion.html
% c = center of the function
% x = a vector with values around c
% n = number of terms in the series
tay = zeros(n,length(x)); % Start the series with 0
deriv = [0 -1 0 1]'; % possible derivatives
for i = 0:n-1 % Iterate n times (from 0 to n-1)
tay(i+1, :) = deriv(1) * (x-c).^(i) / factorial(i); % Taylor expansion
deriv = circshift(deriv, -1); % Find derivative for the next term
end
output = sum(tay); % summation of terms
end
0 Commenti
Addy
il 15 Feb 2024
Modificato: Addy
il 15 Feb 2024
For maybe later I will try to use loops for taylor expansion instead of symbolic toolbox.
But for now, it works.
Here is the solution:
clear;clc
syms xx % a variable symbolic function
c = pi/2; % center point for Taylor series expansion
x = (-4:0.1:6)'; % x values for the function
y = cos(x); % y values for a
n = 25; % order to calculate polynomials
%% Polynomial part
p = polyfit(x,y,n); % Polynomialcurve fitting (dont worry about the warning)
f_poly = polyval(p,x); % reconstructing curve to compare with original curve
%% Now that we obtained the polynomials, we construct the equation
temp = vectorize(poly2sym(p)); % since we use symbolic, we preserve precision to greater degree
temp = replace(temp,'x','xx');
f_sym = eval(temp); % for taylor series
f_equ = str2func(['@(xx)',temp]); % for plotting again and checking
%% plotting original, curve from polyfit, curve from equation
figure(1);clf;
plot(x,y)
hold on;box on;grid on
plot(x,f_poly)
plot(x,f_equ(x))
axis tight
legend('Original','Polyfit','Polyfit equ','location','south')
%% plotting error between original vs curve from polyfit and equation
% numerical errors happen
e_poly = y-f_poly;
e_equ = y-f_equ(x);
figure(2);clf;hold on
plot(x,e_poly);
yyaxis right
axis tight
plot(x,e_equ);
legend('error polyfit','error equation')
%% Plotting the taylor part
f_tay1 = addy_taylor(f_sym,xx,c,4,x);ylim([-1.5 1.5])
f_tay2 = addy_taylor(f_sym,xx,c,6,x);ylim([-1.5 1.5])
f_tay3 = addy_taylor(f_sym,xx,c,10,x);ylim([-1.5 1.5])
figure(4);clf;
plot(x, y, 'g')
hold on;grid on
plot(x, f_tay1, 'ro')
plot(x, f_tay2, 'b-.')
plot(x, f_tay3, 'k')
axis([-4 6 -3 3])
legend('cos(x)','4 term s','6 terms','10 terms','Location','north')
%% function for taylor expansion and vectorizing
function y = addy_taylor(f_sym,xx,c,n,x)
y = vectorize(taylor(f_sym,xx,c,'Order',n+1));
xx = x;
y = eval(y);
end
0 Commenti
Vedere anche
Categorie
Scopri di più su Particle & Nuclear Physics in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!