The Taylor expansion for log_e(1 + x) is given by log_e(1 + x) = x - x^2/2 + x^3
ID: 3933754 • Letter: T
Question
The Taylor expansion for log_e(1 + x) is given by log_e(1 + x) = x - x^2/2 + x^3/3 - .... + (-1)^k + 1 x^k/k + ... Write a MATLAB function that takes as input a value for x, and a value for tol. The function must sum the series while the value of the current term is greater than or equal to to. When executed, the function should display the value of x, the tolerance, the value of log_e(1+x) obtained by using your function, the exact value of log_e(1 + x) obtained using the MATLAB function log, and the absolute error: x=0.4 tol=0.01 myval=### exact=### error=### Run the function four times using values of x = 0.4 and 0.83 and values of tol = 0.01 and 0.001.Explanation / Answer
(i) FOR X=0.4
MY FUNCTION:-
Nmax=1000; %maximum number of terms
num_app=1; %number of values approximated
approx=zeros(2,1);
approx(1)=0.4;
%approx(2)=0.83;
solution=zeros(Nmax,2,num_app);
%
for k=1:num_app %error loop
true=log(approx(k)+1); %calculates true value
disp(true);
partial_sum=0; %resets partial sum variable
for i=1:Nmax %term loop
x=approx(k); %assigns local value
partial_sum=partial_sum+(-1)^(i-1)*x^(i)/i; %runs taylor approx
disp(partial_sum);
error=abs((true-partial_sum)/true); %calculates relative error
disp(error);
solution(i,1,k)=i;
solution(i,2,k)=error;
end
end
MATLAB FUNCTION
syms x
taylor(log(1+x), x, 'ExpansionPoint', 0.4)
OUTPUT:
myval= >> TaylorSeries = 0.3365
Exact Value= Taylor series = 0.253
ERROR : 0.0833
(ii) X= 0.83
OUTPUT:
my val= >> TaylorSeries = 0.6043
Exact val = 0.26245
error=0.34
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.