In this exercise, we briefly present the use of MATLAB^{®} to perform basic statistics. MATLAB is a powerful software program which can simplify a variety of analyses which are commonly performed in engineering. Some general instructions are provided first. An example of how to apply these instructions can be found towards the end of this page.
When you first open MATLAB, you will see the Desktop. It should look approximately as shown below.
The only portion of the Desktop that we will use in this exercise is the Command Window—it is the large window in the middle of the Desktop, which is blank except for the command prompt, >>. As the name implies, the Command Prompt is where we type commands; generally mathematical expressions and functions which we want MATLAB to evaluate.
To use MATLAB to perform our statistical analysis, we first need to put our resistance data into MATLAB's Workspace. The Workspace is a space in memory which contains all the numbers we will be interested in. Numbers must be in the Workspace before they can be used in calculations^{1}, and the results of any calculations are placed in the Workspace^{2}.
To put our measured resistance data in the Workspace, we will create a variable which contains the resistance numbers. The general syntax to create a variable is^{3}:
“Variable_name” is the name you choose to call your collection of numbers. The numbers themselves are separated by commas, and contained within square brackets.
The MATLAB function to calculate the average or mean of a set of numbers is mean. The syntax is:
Where Variable_name is the variable which contains the set of numbers we want to average. Typing this at the command prompt and pressing the Enter key will cause MATLAB to determine the average and display it in the command window. By default, the result of the calculation will be assigned to a variable called “ans” ^{4}.
If you want to save the result of the calculation to a different variable (definitely a good idea), specify the variable name and use an “=” sign:
In the above, the result of the calculation will now be in the Workspace under the name “Var_Mean”.
The MATLAB function to calculate the median value of a set of numbers is median. The syntax is:
Where Variable_name is the variable which contains the set of numbers for which we want a median value. Typing this at the command prompt and pressing the Enter key will cause MATLAB to determine the median and display it in the command window. By default, the result of the calculation will be assigned to a variable called “ans”.
If you want to save the result of the calculation to a different variable (definitely a good idea^{5}), specify the variable name and use an “=” sign:
In the above, the result of the calculation will now be in the Workspace under the name “Var_med”.
The MATLAB function to calculate the standard deviation of a set of numbers is std. The syntax is:
Where Variable_name is the variable which contains the set of numbers for which we want a standard deviation. Typing this at the command prompt and pressing the Enter key will cause MATLAB to determine the standard deviation and display it in the command window. By default, the result of the calculation will be assigned to a variable called “ans”.
If you want to save the result of the calculation to a different variable (definitely a good idea), specify the variable name and use an “=” sign:
In the above, the result of the calculation will now be in the Workspace under the name “Var_std”.
Suppose you have measured the following values:
To save them as a variable named “data”, and then calculate the mean, median, and standard deviation of the data, just type the following commands at the command prompt:
The result is shown in the figure below:
So the mean is 13.4600, the median is 10, and the standard deviation is 14.2455.