MATLAB FUNCTION The Maclaurin series expansion for the sine function is: where x
ID: 2291752 • Letter: M
Question
MATLAB FUNCTION
The Maclaurin series expansion for the sine function is:
where x is in radians. This function can be used to approximate the sine of x with increasing accuracy as terms are added to the summation. Write a function that accepts two scalar inputs (in order):
A value for x (in radians).
The number of series sums, N , to use in the series approximation of sin(x).
Your function should generate the following three outputs (in order):
A column vector of the first N series summations. Consider the first summation to be x-x^3/3!.
A column vector of the magnitude (i.e. absolute value) of the approximate relative error values associated with the first N series summations. Note the "previous approximation" for the first value in this vector will be x.
A column vector of the true relative error values associated with the first N series summations. Use MATLAB's built-in sine function to compute the true value for this error calculation.
Explanation / Answer
function[sum_N, error_relative_N, error_true_N ] = sine(x,N)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The function sine computes the approximate value of sine of x using
% the Maclaurin series expansion for the sine function given by
%
% sin(x) = x - x^3/3! + x^5/5! - x^7/7!....
%
% with increasing accuracy as terms are added to the summation.
%
% The function sine accepts two scalar inputs (in order):
% (1) A value for x (in radians).
% (2) The number of series sums, N , to use in the series approximation of sin(x).
%
% and generates the following three outputs (in order):
%
% (1) A column vector of the first N series summations, the first summation being
% x-x^3/3!.
% (2) A column vector of the magnitude (i.e. absolute value) of the approximate
% relative error values associated with the first N series summations. The
% "previous approximation" for the first value in this vector will be x.
% (3) A column vector of the true relative error values associated with the first
% N series summations compared to the in-built sin(x) function of MATLAB.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Declaring empty column vectors for required outputs
t = zeros(N+1,1); % column vector to store first N+1 terms of the series
sum_N = zeros(N,1); % column vector to store first N sums of the series. since the question
% defines sum_N(1)=t(1)+t(2), no. of sums is 1 less than no. of terms.
error_relative_N = zeros(N,1); % column vector to store relative error terms
error_true_N = zeros(N,1); % column vector to store absolute error terms
% Initializing variables and array values
t(1) = x;
t(2) = -t(1)*(x*x)/factorial(3);
sum_N(1) = t(1)+t(2); % as mentioned in question
error_relative_N(1) = x;
error_true_N(1) = sin(x) - sum_N(1);
fact =3; %fact is initialized with fact=3 because x^5/5! is the first iteratively computer term in this function
%computing reuired values
for i=3:N+1
t(i) = -t(i-1)*(factorial(fact)/factorial(fact+2))*x*x; %computing i-th term of series w.r.t (i-1)th term
sum_N(i-1) = sum_N(i-2)+t(i); %i-th series summation, from sum_N(2) to sum_N(N)
fact = fact+2; %incrementing argument of factorial function for computing next term in the next iteration
end
for j=2:N
error_relative_N(j) = abs(sum_N(j) - sum_N(j-1)); %j-th Relative error magnitude = |sum_N(j+1)-sum_N(j)|
error_true_N(j) = abs(sin(x) - sum_N(j));%j-th true error w.r.t in built sin function of MATLAB
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.