Examples

  1. What is Matlab's value for ""

    Matlab has a pre-defined variable named pi that contains a high-precision approximation of the value of "".

    Type the following at the prompt in the command window and press enter to see this value.

    >> pi

    You will see the following. However, the internal value is actually has a much higher precision than the digits shown by default.

        >> pi
        
        ans =
        
            3.1416
        
        >> 
  2. Assign the value 1 to the variable named z by typing the following at a command prompt.
    >> z = 1
       >> z = 1
    
       z =
    
            1
    
       >>
    Use the Workspace window to confirm that the variable z has been assigned the value 1.
  3. Type
    z = 2 ;

    in the command window and press enter. Will this cause an error?

      >> z = 2 ;
      >> 
      

    No, this does not cause any type of error, it simply puts a different value into the variable named z. The original value 1 is lost. The semi-colon ( ; ) suppresses (hides) the output from this command. View the Workspace window to see that the value has been changed.

  4. Time Conversion

    Convert twenty-two days, four hours, thirteen minutes, and twenty-five seconds to seconds. Start by entering all of the information into variables. This allows us to use the variable names instead of having to remember what each numeric value represents. This will make our calculations more readable and easier to debug (troubleshoot).

    At the Command Window prompt type and Enter each of these assignment statements:

        >> days = 22;
        >> hours = 4;
        >> mins = 13;
        >> secs = 25;
        >> 

    View the workspace if you're not sure if your values have been assigned correctly. You should see something resembling the following:

    The workspace window shows the values of variables assigned.
  5. We could have typed all this information into an array, however the above format makes it easier for us to remember what each number represents. Assigning values to variable names in this way helps to document the information that we are using in our problems. There are several ways to complete this problem. We'll take an approach that involves combining several variables at once.

    Type the following into the Command Window to find the total hours:

    total_hours = days * 24 + hours

    Next, calculate the total number of minutes:

    total_mins = total_hours * 60 + mins 

    Finally, type the following to find the total seconds:

    total_secs = total_mins * 60 + secs 
        >> total_hours = days * 24 + hours
    
        total_hours =
    
           532
    
        >> total_mins = total_hours * 60 + mins
    
        total_mins =
    
               31933
    
        >> total_secs = total_mins * 60 + secs
    
        total_secs =
    
             1916005
    
        >>
    

    These conversions could have been completed on a single line of code. But, that does not make your work easy to follow or fix if there are errors.

    To learn even more about the types of variables that exist in Matlab and how to use them, open the helpbrowser and search for variables. There are many helpful articles to help explain how variables can be used in Matlab.

  6. Enter the following at the Command Window and press Enter.
    >> y = x^2

    This error is generated and displayed:

        ??? Undefined function or variable 'x'.

    The code statement y = x^2 tells Matlab to square the current value of x and assign it to a new or existing the variable named y. However, since we did not previously assign a value to the variable x, Matlab is unable to square its value and thus displays an error.

    Assign a value to x and use the Command History window to execute the statement again.

  7. Choose a descriptive variable name and assign the value $261.25 as the price per ton of steel. Do not suppress the output of your assignment statement.
        >> steelPrice = 261.25
    
        steelPrice = 
    
          261.2500
          
        >>

    Although, you can choose any valid name, you should also choose something descriptive and easy to remember. Not too short, or too long for the best names.

    Did you include the semi-colon? Do not include the semi-colon if you want to see the results of your command.

  8. Choose a descriptive variable name and assign the value $2,929.00 as the price per acre of development land. Suppress the output of your assignment statement.
    	>> pricePerAcre = 2929 ;
    	>>

    Include the semi-colon to suppress the output. Don't type commas (,) when entering values that are greater than 999. View the Workspace window to be sure that your variable has been assigned.

  9. The acceleration of gravity, the length of a pendulum, the initial angle of release need to be used in several functions that you will need later.

    Make each of these variables global and assign values to each variable. L is a 5 meter length, theta is an angle pi/8 in radians, and g is the acceleration due to gravity, 9.8 m/sec2.

        global g, L, theta ;
        g = 9.8 ;
        L = 5 ;
        theta = pi/8 ;
    	

    Include the semi-colon to suppress the output. To use a global variable in a new scope, include the statement:

        global g;