Problem including tolerance and finding value of a certain term
Show older comments
Currently doing revision for a MATLAB module next year. Was attempting a question and was hoping for some help it.
I am asked to consider this Taylor series expansion:
S(x,n)=(x-1) - 1/2(x-1)^2 + 1/3(x-1)^3 - 1/4(x-1)^4 +...+ ((-1)^(n-1))*(1/n)*(x-1)^n
From this write a matlab function that, given the values of x and a tolerance e, returns the value of k such that the absolute value of the k-th term of the taylor series expansion is less or equal than e.
The issue is with the script i have made:
function S = taylor()
clear all
clc
tol=1e-5 x = input('Select value of x = '); S = 0; m = input('Select value of n = ');
for n = 1:m S = S + ((-1) ^ (n-1)) .* (((x - 1) .^ n)./n); end
disp(' ') fprintf('S(x,n) = %8.4f\n' ,S)
end
As I am a beginner to MATLAB I have no idea how to include a tolerance in the calculation. I am also wondering how I can find the kth term of the series with an absolute value of less or equal e(tolerance).
Just to inform you I the value of e is 10e-5
Thank you in advance for your time
Accepted Answer
More Answers (1)
Iain
on 6 Aug 2013
tol = 0.001;
x = 5;
n = -1;
current_term = inf;
while abs(current_term) > tol
n = n + 1;
current_term = ((-1) ^ (n-1)) .* (((x - 1) .^ n)./n);
end
disp(['The answer is the ' num2str(n) 'th term'])
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!