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

14 visualizzazioni (ultimi 30 giorni)
Ibrahima Diallo
Ibrahima Diallo il 3 Set 2019
Risposto: Kritika Bansal il 13 Set 2019
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 Commenti
Ibrahima Diallo
Ibrahima Diallo il 3 Set 2019
%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 il 4 Set 2019
When n is 0 then for i=1:n will not execute at all, so you will not create the variable y

Accedi per commentare.

Risposte (1)

Kritika Bansal
Kritika Bansal il 13 Set 2019
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!

Community Treasure Hunt

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

Start Hunting!

Translated by