Solving a taylor serie with matlab. Homework question

1 view (last 30 days)
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?

Accepted Answer

Alan Stevens
Alan Stevens on 3 Dec 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)

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!