Matlab question The function e x can be approximated by its

Matlab question?

The function e x can be approximated by its McLaurin series expansion as follows (note the alternating + and -): e^-x 1 - x + x^2/2! - x^3/3! + ... plusminus x^n/n! Alternatively, note that e x = Thus, e^-x can also be approximated by 1 over the McLaurin series expansion of ex. That is, e^-x 1/1 + x + x^22! + x^33! + ... + x^n/n! Approximate e^-2 using both approaches above for n = 1, 2, ..., 6 and compare each approximation to the true value of e^-2 = 0.135335..., using the true relative error. What conclusions can you make about the two approaches?

Solution

% matlab code

actualValue = e^(-2);

x = 2;
sum = 1;
for n=1:6
sum = sum + (x)^n/factorial(n);
approxValue = 1/sum;
error = actualValue - approxValue;
fprintf(\"N = %d , Actual Value: %f, Approx value: %f, Relative error: %f\ \",n,actualValue,approxValue,error);
end

%{
output:
N = 1 , Actual Value: 0.135335, Approx value: 0.333333, Relative error: -0.197998
N = 2 , Actual Value: 0.135335, Approx value: 0.200000, Relative error: -0.064665
N = 3 , Actual Value: 0.135335, Approx value: 0.157895, Relative error: -0.022559
N = 4 , Actual Value: 0.135335, Approx value: 0.142857, Relative error: -0.007522
N = 5 , Actual Value: 0.135335, Approx value: 0.137615, Relative error: -0.002279
N = 6 , Actual Value: 0.135335, Approx value: 0.135952, Relative error: -0.000616

We can observe that the relative error decreases for higher value of N and can assume that the values will converge at some point near infinity
%}

Matlab question? The function e x can be approximated by its McLaurin series expansion as follows (note the alternating + and -): e^-x 1 - x + x^2/2! - x^3/3! +

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site