Taylor series of log(x) with a = 1.
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Walter Roberson
le 4 Sep 2019
When n is 0 then for i=1:n will not execute at all, so you will not create the variable y
Réponses (1)
Kritika Bansal
le 13 Sep 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!
0 commentaires
Voir également
Catégories
En savoir plus sur Calculus dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!