How to plot Taylors approximation using pre-calculated generalized summation formula

Hi All,
I have came accross the function taylor() exampl. T = taylor(log(x), x, 'ExpansionPoint', 2); by using it I get perfect result
but I'd like to plot results of my own pre-calculated Taylors aproximation of x Order. I started with f(x)=ln(x) for 0<=x<5 when approximation is around x=1 (therefor a=1) at n points [0,2,4,6]
This is what I got.
BTW: I am total newbie, and any hint more then appriciated
clear
clc
close all
%taylors series of ln(x)
x=0:5;
a=1;
SumN=0; % initialize SumN
sign=-1; % variable that assigns a sign to a term
timepoints = 0:0.1:6;
y= zeros(1,length(timepoints));
%adding bells and whistles
fig = figure();
set(fig,'color','white')
grid on
xlabel('x')
ylabel('y')
for n=1:length(y)
SumN= @(x) ((sign).^(n+1)*((x-a).^n))/n;
y(n) = SumN(timepoints(n));
end
plot(timepoints,y,'r-','LineWidth',2);
legend('Taylor series')
Thank you

7 comentarios

SumN= @(x) ((sign).^(n+1)*((x-a).^n))/factorial(n);
Try using the factorial in the series expansion in place of n, since n involves 0 which makes the expansion undeterminable
Hi VBBV,
That is a good point, you highlighted. I am trying to implement sum function which I calculated on paper. the result shown in picture. In the case of ln(1) the simplified result doesn't involve factorial of n. Also, notice my 'n' starts at 1.
i have a feeling, that I am missing know-how, how to manipulate variables in Matlab. i am very new in coding in here. to be honest, i have put someones else concept with the hint of my formula, but this is the closest way of my coding to get at least the plot result. Othervise i had multiple errors :(
in the meantime, I completed Onramp and Fundamentals Matlab courses, great learning curve
Thanks again
@Stefan, In your code, the difference i noticed is you were evaluating the function at each timepoint instead of x and not summing the terms, By summing all the timepoints and evaluate the function at each value of x, you would still arrive at the same result as shown below
clear
clc
close all
%taylors series of ln(x)
x=0:0.1:5;
a=1;
SumN=0; % initialize SumN
sign=-1; % variable that assigns a sign to a term
timepoints = 1:6; % Note the zero is excluded !!! in his function too
y= zeros(1,length(timepoints));
%adding bells and whistles
figure
plot(x,log(x))
hold on
for n=1:length(x) % evaluate the function at every x
y = (sign).^(timepoints+1).*((x(n)-a).^timepoints)./timepoints;
ySum(n) = sum(y);
end
ySum
ySum = 1x51
-2.4500 -1.9187 -1.5023 -1.1726 -0.9077 -0.6911 -0.5105 -0.3566 -0.2231 -0.1054 0 0.0953 0.1823 0.2623 0.3363 0.4047 0.4674 0.5233 0.5701 0.6035 0.6167 0.5995 0.5376 0.4114 0.1950 -0.1453 -0.6521 -1.3786 -2.3900 -3.7655
plot(x,ySum,'r-','LineWidth',1);ylim([-2 2])
legend('ln(1)','Taylor series'); grid
Thank you for hints, and making the code work, much appriciate
I was messing around and Here is what I got
I wanted to implement the section of code below, but when I assigne my plot to variable "p", set function has an issue to accep "p" element
p = plot(rand(4));
NameArray = {'LineStyle'};
ValueArray = transpose({'-','--',':','-.'});
set(p,NameArray,ValueArray)
Anyhow, I learned a lot with your help, research and , trial and error experiments, Thanks
clear
clc
close all
%%taylors series of ln(1)
%variables
x=0:0.1:5;
a=1;
ySum = 0; % initialize ySum
sign =-1; % variable that assigns a sign to a term
y= zeros(1,length(6)); %initialize y variable
%adding bells and whistles
figure;
ylim([-2 2])
grid on
xlabel('x')
ylabel('y')
legend({"f(x)=ln(1)","Taylor Deg0","Taylor Deg2","Taylor Deg4","Taylor Deg6"}, ...
'location','northwest')
hold on;
plot(x,log(x),'LineWidth',1.5);
for i=0:2:6
timepoints = 1:i;
for n=1:length(x)
y = (sign).^(timepoints+1).*((x(n)-a).^timepoints)./timepoints;
ySum(n) =sum(y);
end
hold on
plot(x,ySum,'LineWidth',1.5);
end
p = plot(rand(4)); % plot returns 4 x 1 line array
NameArray1 = {'LineStyle'}; %
NameArray2 = {'LineWidth'};
ValueArray1 = {'-','--',':','-.'};
ValueArray2 = [1.5, 1.5 2 2];
for k = 1:numel(p) % use a loop to set the individual line styles
set(p(k),NameArray1{1},ValueArray1{k},NameArray2{1},ValueArray2(k)); %
end
Hi VBBV,
Thanks for contribution to this project, this is what I ended up with
%% Paylors series of ln(1)
%% initialize workspace
clear
clc
close all
%% declare variables
x=0:0.1:5;
a=1; %constant variable
sign =-1; % constant variable
y= zeros(1,length(6)); %initialize y array
ySum = zeros(1,length(6)); % initialize ySum dynamic array
%% Plot bells and whistles
markers = {'o','hexagram','*','diamond','d','v','*','h'};
colors = {'#e81416','#79c314','#ffa500','#FFA500','#70369d','#487de7','#79c314'};
linestyle = {':','--','-.',':','-.','--'};
getFirst = @(v)v{1};
getprop = @(options, idx)getFirst(circshift(options,-idx+2));
%% Plot of ln(1)
figure()
plot(x,log(x),'Marker',getprop(markers,5),...
'Color',getprop(colors,3),...
'linestyle',getprop(linestyle,6),...
'DisplayName', ['Line ', num2str(6)],...
'MarkerIndices',1:10:length(log(x)),...
'LineWidth',1.5);
hold on
%% Plot Taylors aproximation of ln(1) in 0,2,4,6 order
for i=0:2:6
timepoints = 1:i;
for n=1:length(x)
y = (sign).^(timepoints+1).*((x(n)-a).^timepoints)./timepoints;
ySum(n) =sum(y);
end
plot(x,ySum ,'Marker',getprop(markers,i),...
'Color',getprop(colors,i),...
'linestyle',getprop(linestyle,i),...
'DisplayName', ['Line ', num2str(i)],...
'LineWidth',1.5,....
'MarkerIndices',1:5:length(ySum));
hold on
end
%% Figure set up
ylim([-2 2])
grid on
xlabel('x')
ylabel('y')
title('Tayylors series of ln(1)');
set(gca,'fontSize',10);
legend({" f(x) = ln(1)","Taylor Deg0",...
"Taylor Deg2","Taylor Deg4",...
"Taylor Deg6"},'location','northwest')
%% End
Might be inspiration for others, or for further development.
All the best
Stef

Iniciar sesión para comentar.

Respuestas (0)

Etiquetas

Preguntada:

el 3 de Mzo. de 2024

Comentada:

el 11 de Mzo. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by