Lab 18

Complex and Integer Data Types

CS211 Lab Policy:

Instructions:

You will create one MATLAB program file named Lab18.m for this lab.  For each step below, add appropriate MATLAB code and label each step with appropriate comments.  Make sure you test your code for each step before proceeding to the next step.

  1. The MATLAB function roots() will solve for the roots a polynomial equation. The input to the function must be a vector containing the coefficients of the polynomial. For example, to solve for the roots of the equation, 3x2 + 12x + 4.3 = 0, call roots([3  12  4.3]). The output of the roots() function is a vector of values. The number of values in the output vector will be equal to the degree of the polynomial.

    Using the roots() function, solve for the roots of the following polynomials.

    3x2 + 12x + 4.3 = 0

    3x2 + 2x + 4.3 = 0

    x2 + 1 = 0

    5x8 + 7x5 - 2x4 + 12x + 7 = 0 
    (Be careful -- your input vector should contain 9 numbers.)

    Use disp() to display your root values.

    For each equation, use a loop to insert the root values back into the original equation to verify that the equation does produce zero for that value. For example, a loop for the first equation might look like:

        for j = 1:2
            disp( 3*Root(j)^2 + 12*Root(j) + 4.3 );
        end

    Notice that your answers will be close to zero in some cases but not exactly zero. Why do you think this might happen?

    Notice that you did not have to do anything special to calculate, display, or perform arithmetic on imaginary numbers. MATLAB understands imaginary numbers! Imaginary numbers are completely integrated into all MATLAB calculations! Isn't that cool?

     

  2. For this part of your program you will investigate the integer data type. Please download the file 49.png and save it to your MATLAB folder. This file contains a medical x-ray image, where each pixel value is an intensity in the range 0 to 65,534. The pixel values represent the amount of radiation (x-rays) that passed through the patient's body.

    For each row in the table below, add the MATLAB statements to your Lab18() code, run your code, examine the results, read the comments on the right side of the table below, and then continue to the next set of statements. If you have any questions "along the way," please ask your instructor to explain what is happening.

    MATLAB statement to add Explanation and Comments
    My_image = imread('49.png');
    whos My_image
     
    Read the data in an image file into an array called My_image. Then use the whos() function to examine the type of data stored in the My_image array. Notice that the element values are all uint16 (unsigned integers in the range 0 to 65,535).
     
    image(My_image);
    colorbar();
    Display the image in a figure window. Notice that the image does not look anything like a medical image. This is because the current color map does not contain good color values to view a medical image. The colorbar() function has displayed the current color map on the right-hand-side of the figure window.
     
    colormap( bone() );
    colorbar();
     
    Change the color map and display it in the figure window. Notice that the new color map contains 64 values that are basically shades of gray (with a tint of blue).
     
    Max_value = max( max( My_image) )
     
    Find the maximum intensity value in the image array. This will allow us to re-scale the image values to new intensities that are within the 64 values of the color map. (Why do we need to call max() twice?)
     
    New_image = My_image * (64 / Max_value);

    image( New_image );
     
    This attempts to scale every element in the image by a fraction. The attempt is to change the image pixel values to a range of values in the color map; that is, from (0, 65,535) to (0,64). Notice that your output image is still not a medical image. This is because this assignment statement is a mixed mode expression. The 64 is a double value, Max_value is a uint16 value, and My_image is a uint16 array. This mixed mode expression is producing a scale factor of zero. Why is 64 / 65535 zero? Review the lesson notes if you do not know why. Remember, never write a mixed mode expression in MATLAB.
     
    New_image = double(My_image) * (64 / double(Max_value) );

    image( New_image );
     
    This scales every element in the image by a fraction, but now the mixed mode expression has been changed into an expression where all the values are of the same data type --i.e., double. We need all the values in the expression to be double because the scale factor we need is a small fraction. Remember that integer values cannot represent fractions.

    You should now see an medical x-ray image of a slice of a human skull!
     

    Things to remember:

  3. Please spend the remainder of your lab time working on the PEX2 assignment.
     

Turn-in:

Submit your Lab18.m file.