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

6 ビュー (過去 30 日間)
Ibrahima Diallo
Ibrahima Diallo 2019 年 9 月 3 日
回答済み: Kritika Bansal 2019 年 9 月 13 日
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 Center および File ExchangeCalculus についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by