Solving a taylor serie with matlab. Homework question

3 次查看(过去 30 天)
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?

采纳的回答

Alan Stevens
Alan Stevens 2020-12-3
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)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Signal Analysis 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by