24. The natural exponential function can be expressed by e- --. Deter- n! mine e
ID: 2248548 • Letter: 2
Question
24. The natural exponential function can be expressed by e- --. Deter- n! mine e by calculating the sum of the series for: (a) n=5, (b) n= 15, (c) n=25 For each part create a vector n in which the first element is 0, the increment is 1, and the last term is 5, 15, or 25. Then use element-by-element calcula- tions to create a vector in which the elements are . Finally, use the MAT- LAB built-in function sum to add the terms of the series. Compare the values obtained in parts (a), (b), and (e) with the value of e calculated by MATLABExplanation / Answer
If you want to change the precision change the format long statement in the code. for more formats types help format in the command window of the matlab.
a=[5 15 25];
format long
summation=zeros(1,length(a));
difference=zeros(1,length(a));
disp('e^2=');
disp(exp(2));
for i =1:length(a)
n=0:1:a(i);
y=zeros(1,length(n));
% In the given problem x=2 since we have to calculate e^2
for j=1:length(n)
y(j)=2^n(j)/factorial(n(j));%x^n/n!, x=2
end
summation(i)=sum(y);
difference(i)=exp(2)-summation(i);
fprintf('Approximation of e^2 with n=%d ',a(i))
disp(summation(i));
fprintf('Difference between e^2 and approximation of e^2 with n=%d ',n(i))
disp(difference(i));
end
Code for calcualting the factorial of a number
The following lines of copied should be copied into a matlab function file
function [f] = factorial(i)
f=1;
if(i>1)
for j=1:i
f=f*j;
end
end
end
%Note: make sure that the function file and the script file are in the same folder.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.