# Value of Sin(x) using infinite power series method, Evaluation of Binomial expansion

In this example,  the answer to two questions, viz, value of Sin(x) using infinite power series method, and evaluation of binomial expansion has been demonstrated using Matlab function. In the first question expert demonstrates to calculate value of Sinx for three different values of x . Sum is calculated using  an infinite power series method. A Matlab function is written to accomplish the same. In  second question the expert has revealed the  evaluation of one binomial expansion. Here also, the tutor has written a Matlab function for this evaluation.

SOLUTION : –

Question 1

To solve this question, the following series is used as an approximation to calculate the sine of any value:

This is an approximation defined by a sum of infinite terms. However, to calculate the sine value, it cannot be calculated as a sum of infinite terms. Instead, a finite number of terms is used. The number of terms depends of a termination condition, which is established as a minimum error accepted. The error is calculated as the difference between real value of and the approximated value calculated at term . For this paper, the minimum error is established at:

The code used to calculate the (Appendix A) can calculate the values for small values of x. The results are presented below:

Discussion for Question 1

The series used as an approximation to the sine value turned out to be a fairly accurate approximation, since it allows to calculate the sine value of a number with an error less than or equal to 10 ^ -10. However, from the computational point of view, this approach has a weakness and it is at the time of calculating the denominator of each term. The denominator of each term is defined as the factorial of twice the position of the term minus 1. For a few terms, a computer calculates these values easily. However, when you want to calculate the sine of a larger number, a larger number of terms (for example, 50) is required and a computer is not able to calculate the factorial of that number because its value is excessively large.

One way to solve this problem is to set a maximum limit for the number of terms. In the case of MATLAB the maximum factorial that can be calculated is 171!. Therefore, this limit could be set using an if statement.

Question 2

To convert a floating-point binary number to a decimal number, the following method is applied:

First of all, the first bit in the mantissa is considered the sign of the number:

11110010 11000101 01101010

If the first number is zero, the resulting decimal number is positive. If the first bit is 1, the number is negative. So, for this example, the number should be negative.

The exponent is defined as an 8-bit binary number:

01011000

So, the floating-point binary number can be written as:

1 01011000 1110010 11000101 01101010

The exponent is calculated as:

clc, clear all, close all

%% Question 1: Calculate the sine of a value x using the series approximation

x = 0.5                  % the value used to calculate the sin

real_val = sin(x)        % variable to store the real value of sin(x)

e = 1e-10;               % epsilon (min error)

calculated_val = 0;      % variable to store the calculated value using the approximation

error = 1e50;             % Initial error used for n = 1

s = 1;                   % Variable to store the sign of the term. (don’t touch) 1 for positive, -1 for negative

i = 1;                   % Counter

while(error > e)

k = 2*i-1;           % exponent and factorial of term

term = x^k /factorial(k);

calculated_val = calculated_val + s*term; % Current calculated value of sin(x)

s = -1*s;            % for each term, change the sign

error = abs(real_val – calculated_val); % Calculate current error

i = i + 1;           % Increase counter

end

% Print results

fprintf(‘The values calculated using the sine approximation are:\n’);

fprintf(‘     sin(%s): %s\n’, num2str(x), num2str(real_val));

fprintf(‘     approximation: %s\n’, num2str(calculated_val));

• Matlab function that converts a floating-binary to decimal

clc, clear all, close all

%% Question 1: Calculate the sine of a value x using the series approximation

x = 0.5                  % the value used to calculate the sin

real_val = sin(x)        % variable to store the real value of sin(x)

function ret = FloatingBinaryToDec(mantissa_str, exponent_str)

%% Question 2: Convert a floating-point binary to decimal

% Sizes of mantissa an exponent

mantissa_size = 24;

exponent_size = 8;

% Define the mantissa and exponent as strings

%     mantissa_str = ‘11110010 11000101 01101010’

%     exponent_str = ‘01011000’

% Remove spaces if exists

mantissa_str = mantissa_str(~isspace(mantissa_str));

exponent_str = exponent_str(~isspace(exponent_str));

m_sign = str2double(mantissa_str(1));

mantissa_sign = (-1)^m_sign; % Get the sign of the mantissa

exponent_val = 0;

for i = 1:exponent_size

val = 2^(i-1); % The value of the current term: 2^(i-1)

if str2double(exponent_str(exponent_size – i + 1)) == 1 % If the current bit is equal to 1, consider the term

exponent_val = exponent_val + val;

end

end

E = exponent_val-127; % Finally, the exponent is the calculated value – 127

% Add the hidden bit, implied to be 1. Also, we delete the first bit

% because that bit defines the sign of the mantissa

mantissa_str = mantissa_str(2:end); % we delete the first number because that’s the sign

mantissa_str = strcat(‘1’, mantissa_str); % hidden bit

M = 0;                      % The value of the mantissa M

for i = 1:mantissa_size

k = -(i-1);

if str2double(mantissa_str(i)) == 1

M = M + 2^k;

end

end

dec_val = mantissa_sign*M *2^E;          % Finally, the result

ret = dec_val;

end

• Matlab code to call the function

clc, clear all, close all

%% Question 2: Convert a floating-point binary to decimal

% Sizes of mantissa an exponent

mantissa_size = 24;

exponent_size = 8;

% Define the mantissa and exponent as strings

mantissa_str = ‘11110010 11000101 01101010’;

exponent_str = ‘01011000’;

m_sign = str2double(mantissa_str(1));

fprintf(‘******* QUESTION 2 PART a) *******\n’);

dec_val = FloatingBinaryToDec(mantissa_str, exponent_str);

mantissa_str_print = mantissa_str(2:end); % Get the original mantissa without the implied 1 and the sign bit

fprintf(‘The floating-point binary: %s %s %s is %.50f\n’, num2str(m_sign), mantissa_str_print, exponent_str, dec_val);

fprintf(‘\n\n******* QUESTION 2 PART b) *******\n’);

largest_number = FloatingBinaryToDec(‘011111111111111111111111’, ‘11111111’);

fprintf(‘The largest number (dec) that can be represented is: %s\n’, largest_number);

fprintf(‘\n\n******* QUESTION 2 PART c) *******\n’);

smallest_number = FloatingBinaryToDec(‘000000000000000000000001’, ‘00000000’);

fprintf(‘The smallest positive number (dec) that can be represented is: %s\n’, smallest_number);

fprintf(‘\n\n******* QUESTION 2 PART d) *******\n’);

fprintf(‘The difference between these numbers is: %s\n’, largest_number – smallest_number);

fprintf(‘\n\n******* QUESTION 2 PART e) *******\n’);

total_size = 2^(mantissa_size – 1);

fprintf(‘The total number of significant base-10 numbers that can be calculated is: %s\n’, total_size);