Solving a taylor serie with matlab. Homework question

1 visualización (últimos 30 días)
Taylor serie of ln x:
ln x = (x − 1) − ((x − 1)^2)/ 2 + ((x − 1)^3)/ 3 − . . .
I need to find an approximately value of ln 3. The question suggested that we should replace x with 1/3 and then take the sum of all terms with absolute value bigger than 1e-8.
The questions suggested solution:
tol=1e-8; s=0; i=0;
term=;
while abs(term) > tol
s=s+term;
i=i+1;
term=...;
end
disp(-s)
What i've tried
tol=1e-8; s=0; i=1;
term = -2./3;
while abs(term) > tol
s = s + term;
i=i+1;
term = ((-2./3).^i)./i;
end
disp(-s)
My code gives the answear 0.51. The answear to ln 3 is 1.098...
How do i solve this?

Respuesta aceptada

Alan Stevens
Alan Stevens el 3 de Dic. de 2020
There's a multiplier of (-1)^(i-1) that you need. Try
tol=1e-8; s=0; i=1;
k = -2/3;
s = k;
err = 1;
while err > tol
sold = s;
i=i+1;
term = (-1)^(i-1)*(k.^i)./i;
s = sold + term;
err = abs(s-sold);
end
disp(-s)

Más respuestas (0)

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by