Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB question Write a user-defined MATLAB function that converts real numbers

ID: 2085574 • Letter: M

Question

MATLAB question

Write a user-defined MATLAB function that converts real numbers in decimal form to binary form. Name the function b = deciTObina (d), where the input argument d is the number to be converted and the output argument b is a 30-element-long vector with is and Os that represents the number in binary form. The first 15 elements of b store the digits to the left of the decimal point, and the last 15 elements of b store the digits to the right of the decimal point. If more than 15 positions are required in the binary form for the digits to the right of the decimal point, the digits should be chopped. If the number that is entered as d is larger than can be stored in b, the function should display an error message. Use the deciTObina in the Command Window to convert the numbers 85321, 0.00671, and 3006.42.

Explanation / Answer

function [ c ] = deciTobina( x )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
prompt = 'enter the decimal number' ;
x = input(prompt);
b = zeros(1, 30);
a = floor(x);
d = x-a;
i=0;
if ( a>(2^15 - 1))
disp('error input number is too large');
else
while 2<= a
r = rem(a, 2);
a= floor(a/2);
b(15-i) = r;
if(d==0.5)
b(16)=1;
else
r = floor(d*2);
d= d-r;
if (d==0)
b(15+i)=0;
else
b(15+i) = r;
end
end
i = i+1;
end
end
disp(b);

end