필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
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
VBBV 2024년 3월 9일
편집: VBBV 2024년 3월 9일
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
Stefan 2024년 3월 11일
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

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by