1.37 Write a user-defined MATLAB function that adds two integers in binary form according to the algo-
rithm of Problem 1.30. Name the function aplusb = addbin(a,b), where the input arguments a and b
are the numbers to be added in binary form (vectors with 1s and 0s that represent the binary numbers), and
the output argument aplusb is the result in binary form (a vector with 1s and 0s that represent the binary
number). The largest numbers that could be added with the function should be binary numbers with 15 1s.
Use the function in the Command Window to add 100111011 and 1100110.
Solution
function aplusb=addbin(a,b)
n=length(a); m=length(b);
maxi=max(m,n);
if m>n
end
if bb(i)==1
flag_b=1;
When the function is executed in the command window, the following is obtained:
>> a=[1 0 0 1 1 1 0 1 1];
>> b=[1 1 0 0 1 1 0];
>> aplusb=addbin(a,b)
aa =
1.38 Write a user-defined MATLAB function that adds two integers in decimal form. Name the function
c = adddec(a,b), where the input arguments a and b are the numbers to be added in decimal form, and
the output argument c is the result in decimal form. The addition is done in the following way: First, the
numbers a and b are converted to binary form by using the user-defined function intTObina written in
Problem 1.35. Then, the converted two numbers are added using the user-defined function addbin writ-
ten in Problem 1.37. Finally, the result is converted to decimal form by using the user-defined function
binaTOint written in Problem 1.34. The three user-defined functions intTObina, addbin, and
binaTOint can be written as subfunctions inside adddec. The numbers that are being added with the
function cannot be larger than 65,535 each. If a larger number is entered, an error message is displayed.
Use the function in the Command Window to add 60,000 and 12,087.
Solution
If the functions addbin and binaTOint are used as they are in the previous problems, the following
program will still work, but the error messages can be eliminated by replacing the criteria with:
%**************************************************************************
end
end
if (flag == 1)
break
end
end
for i=1:length(pint)
b(20-pint(i))=1;
end
b(20-pint(i))=1;
end
end
%**************************************************************************
function aplusb=addbin(a,b)
end
flag_a=0; flag_b=0; aplusb(1:maxi+1)=0; answer(1:maxi+1)=0;
carry=0;
aa(1:maxi)=0; bb(1:maxi)=0;
if large==n
aa(1:maxi)=a(1:n); bb(maxi-m+1:maxi)=b(1:m);
else
end
aplusb=ans;
%**************************************************************************
function d=binaTOint(b)
sum=0;
n=length(b);
When the function is executed in the command window, the following is obtained:
>> c=adddec(60000,12087)
c =
72087
1.39 Write a user-defined MATLAB function that implements the IDDD-643 standard of problem 1.13.
Name the function n = iddd643(num), where the input argument num is any real number, and the out-
put argument n is a 16-element-long vector with 1s and 0s. Use chopping if necessary. Use iddd643 for
determining n for the following numbers:
(a) 81. (b) 256.1875.
Solution
function n = iddd643(num)
IDDD643(1:16)=0;
base10 = num;
if base10 > 0.0
sign = 0;
else
%**********************************************************************
function b=intTObina(d)
large=0; b(1:20)=0;
for i=1:20
large=large+(2^(i-1));
end
if(d>large) disp(‘ERROR: The number entered is too large for this function to
handle’)
return
%**************************************************************************
function b=deciTObina(d)
large=0; b(1:30)=0;
if d > 0.0
sign = 1;
else
sign = 0;
end
for i=1:15
large=large+(2^(i-1));
end
%Convert digits to the left of the decimal point:
if(integer~=0)
flag = 0; icount = 1;
for j = 15:-1:1
temp = integer/(2^j);
if (temp >= 1)
pint(icount) = j; icount = icount + 1; integer = integer -(2^j);
if (integer == 1)
flag = 1; pint0 = 0;
end
for i=1:length(pint)
b(15-pint(i))=1;
end
end
When the function is executed in the command window, the following is obtained:
>> n = iddd643(81)
n =
1.40 Write a user-defined MATLAB function that determines the binary floating point representation of a
number written in decimal form. Name the function b = decTObinfloat(num), where the input argu-
ment num is a real number, and the output argument b is a two-element vector in which the first element is
the mantissa and the second element is the value of the exponent. Use the function to determine the binary
floating point representation of 55.6, 2143.75, and 0.00843.
Solution
function b = decTObinfloat(num)
format long
if num > 0.0
temp = dec*(2^j);
if (temp >= 1)
break
end
end
Max_expo = -j;
sci_bin = dec/(2^Max_expo);
mantissa = sci_bin – 1;
b(1)=mantissa; b(2)=Max_expo;
end
When the function is executed in the command window, the following is obtained:
>> b = decTObinfloat(55.6)
1.41 The value of can be calculated with the series:
(1.1)
Write a MATLAB program in a script file that calculates the value of by using n terms of the series and
calculates the corresponding true relative error. (For the true value of , use the predefined MATLAB vari-
able pi.) Use the program to calculate and the true relative error for:
(a) . (b) . (c) .
Solution
clear; clc;
n=input(‘Please enter the number of terms of the series desired:\n’);
total=0;
When the function is executed in the command window, the following is obtained:
(a)
Please enter the number of terms of the series desired:
10
For n= 10, the calculated value of pi is 3.04184
π
π41()
n11
2n1
————–
n1=
41 1
3
1
5
1
7
1
9
1
11
—–
++ +


==
π
π
π
n10=
n20=
n40=
For n= 20, the calculated value of pi is 3.09162
The true relative error is 1.59056e-02 or 1.591 percent
>>
As can be seen, the convergence of this series is extremely slow, and many more terms are required before
accuracy can be obtained to three decimal places:
Please enter the number of terms of the series desired:
2000
1.42 The Taylors series expansion for is:
where x is in radians. Write a user-defined function that determines using Taylors series expansion.
For function name and arguments, use y=sinTaylor(x), where the input argument x is the angle in
degrees and the output argument y is the value for . Inside the user-defined function, use a loop for
adding the terms of the Taylors series. If is the nth term in the series, then the sum of the n terms is
. In each pass, calculate the estimated error E given by . Stop adding terms
when . Use sinTaylor for calculating:
(a) . (b) .
Compare the values calculated using sinTaylor with the values obtained by using MATLAB’s built-in
sind function.
Solution
function y=sinTaylor(x)
format long
xsin
xsin xx3
3!
—-
x5
5!
—-x7
7!
—-
++ 1()
n
2n1+()!
—————–—-x2n1+()
n0=
==
xsin
xsin
Sn
SnSn1an
+=
ESnSn1
Sn1
—————–—–
=
E0.000001
65°sin
195°sin
When the function is executed in the command window, the following is obtained:
(a)
>> y=sinTaylor(65)
y =
0.906307786213760