Taylor series of log(x) with a = 1.

14 次查看(过去 30 天)
How to Write a Matlab function that sums up a specified number of terms from the Taylor series of log(x) with a = 1.
%I need my own sript.
%I tried this:
function s = etaylor_log(x, n)
syms x
s = taylor(log(x), 'ExpansionPoint', 1, 'Order', n+1)
end
% It does not give values.
% The second :
function s = etaylor_log(x, n)
for i=1:n;
y(i) = (x-1).^(i-1)*(x-1).^i/i;
end
s=sum(y);
% it does not show for n=0.
  3 个评论
Ibrahima Diallo
Ibrahima Diallo 2019-9-3
%I need my own sript.
%I tried this:
function s = etaylor_log(x, n)
syms x
s = taylor(log(x), 'ExpansionPoint', 1, 'Order', n+1)
end
% It does not give values.
% The second :
function s = etaylor_log(x, n)
for i=1:n;
y(i) = (x-1).^(i-1)*(x-1).^i/i;
end
s=sum(y);
% it does not show for n=0.
Walter Roberson
Walter Roberson 2019-9-4
When n is 0 then for i=1:n will not execute at all, so you will not create the variable y

请先登录,再进行评论。

回答(1 个)

Kritika Bansal
Kritika Bansal 2019-9-13
Hi,
Taking the analogy from the expression returned by the following command:
taylor(log(x), x, 'ExpansionPoint', 1, 'Order', n);
You can design your script as follows:
function s = etaylor_log(x, n)
if n==0
error("The value of order can only be a positive integer i.e. n>0");
end
if n == 1
s = 0;
return;
end
for i=1:n-1
y(i) = ((x-1).^(i)*(-1).^(i-1))/i;
end
s=sum(y);
end
Hope this helps!

类别

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