%% Problem 1 The sequence of statements doesn't seem to work. Turn on long exponential formatting. This will show all of the digits. See if this reveals anything >> format long e >> a = 1.1; >> b = 2.2; >> c = 3.3 c = 3.300000000000000e+00 >> a+b ans = 3.300000000000000e+00 It appears that c = a+b is true. IMPORTANT: Numbers are stored in and calculations are performed in binary (i.e., the only digits are 1 and 0). MATLAB will let you see exactly how the numbers are stored in their binary format by turning on hex formatting >> format hex >> c = 3.3 c = 400a666666666666 % last digit is 6 (= 0110 in binary) >> a+b ans = 400a666666666667 % last digit is 7 (= 0111 in binary) When we look at how the numbers are stored, c and a+b are not the same. They differ by 1 binary digit in the last place. In fact, a+b > c. This happens because of errors in the calculation process. In a binary representation, most numbers with digits after the decimal place are going to have an infinite repeating sequence of digits (similar to how 1/3 = 0.333...). All the numbers here (1.1, 2.2, 3.3) have infinite repeating binary representations. See http://www.binaryconvert.com/convert_float.html These numbers need to be truncated to fit into memory. This leads to a storage error. When a and b are added, this can further introduce a rounding error in the calculation. Another important observation is that even though the internal representations of c and a+b are different, they displayed the same when converted to their decimal versions. This is because 3.3 is the closest decimal number to these 2 different numbers. There are 3 important conclusions from this exercise: 1) It is never safe to test numbers that have digits after the decimal place for equality. If you need to test this, you need to test whether the relative difference between the expressions is less than some tolerance (we will come back to this later). 2) It is safe to test integer values (numbers that don't have digits after the decimal place) for equality. 3) Even if numbers display the same when converted to decimal, this does not mean that they are the same number that is stored on the computer. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Problem 2 % There are many ways to do this problem % Shortest way a = input('input a '); b = input('input b '); c = input('input c '); s = (a+b+c)/2; d = s*(s-a)*(s-b)*(s-c); if(d > 0) area = sqrt(d) else disp('Cant make a triangle') end % Longest way if(a+b > c) if(b+c > a) if(a+c > b) s = (a+b+c)/2; area = sqrt(s*(s-a)*(s-b)*(s-c)) else disp('Cant make a triangle'); end else disp('Cant make a triangle'); end else disp('Cant make a triangle') end % Other ways if( a+b > c & a+c > b & c+b > a) s = (a+b+c)/2; area = sqrt(s*(s-a)*(s-b)*(s-c)) else disp('Cant make a triangle') end % Other ways if( a+b < c | a+c < b | c+b < a) disp('Cant make a triangle') else s = (a+b+c)/2; area = sqrt(s*(s-a)*(s-b)*(s-c)) end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Problem 4 %% Key step: Solve for theta_2 %% %% theta_2 = asin (n1*sin(theta_1)/n2) %% %% The domain of the asin function is [-1,1]. In order to %% obtain a meaningful result, n1*sin(theta_1)/n2 must be less than 1 in %% absolute value. Also, all calculations need to be in radians. clear theta_1 = input('Input theta_1 '); n1 = input('Input n1 '); n2 = input('Input n2 '); % Convert theta_1 to radians. theta_1 = pi*theta_1/180; q = n1*sin(theta_1)/n2; if(abs(q) <= 1) then theta_2 = asin(q); % theta_2 is in radians here theta_2 = 180*theta_2/pi % Convert theta_2 to degrees else disp('No value possible') end