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 件のコメント
VBBV
2024 年 3 月 4 日
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
Stefan
2024 年 3 月 4 日
Stefan
2024 年 3 月 8 日
@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
plot(x,ySum,'r-','LineWidth',1);ylim([-2 2])
legend('ln(1)','Taylor series'); grid
Stefan
2024 年 3 月 8 日
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
Stefan
2024 年 3 月 11 日
回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Time-Domain Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





