PROBLEM 1: At the end of the semester, the instructors for a Statics course comp
ID: 3824870 • Letter: P
Question
PROBLEM 1:
At the end of the semester, the instructors for a Statics course compile their final grades into a single spreadsheet to determine the general performance of students for their course. These instructors are responsible for ten sections of the course in total.
It is assumed that each section contains 49 students. The attached Excel spreadsheet contains data arranged in 10 rows where each row represents a section. Each column, in a row, represents one of the 49 students in a section. Each number in each cell represents the final grade that a student has earned for the course.
Write a MATLAB script to do the following:
-Import the entire Excel spreadsheet data into MATLAB
-Compute the mean final grade for each section
-Compute the standard deviation of the final grades for each section
-Compute the global mean final grade (that is the mean final grade of all 490 students)
-Compute the global standard deviation of the final grade
Your MATLAB script must also complete the following tasks:
-Display the mean and standard deviation of the final grades for each section in the form of a table
-Display the maximum and minimum final grades for each section on the same table
-Plot a histogram of the data using the histogram function on MATLAB
*please provide the matlab code w/ explination on how to use functions please*
Explanation / Answer
When MATLAB is invoked, the user is presented with an interactive environment. Enter a statement, press the carriage return ("ENTER") and the statement is immediately executed. Given the power that can be packed into one MATLAB statement, this is no small accomplishment. However, for many purposes it is desirable to store a set of MATLAB statements for use when needed.
The simplest form of this approach is the creation of a script file: a set of commands in a file with a name ending in .m (e.g. do_it.m). Once such a file exists and is stored on disk in a directory that MATLAB knows about (i.e. one on the "MATLAB path"), the user can simply type:
at the prompt in interactive mode. The statements will then be executed.
Even more powerful is the function file; this is also a file with an .m extension, but one that stores a function. For example, assume that the file val_port.m, stored in an appropriate directory, contains a function to produce the value of a portfolio, given a vector of holdings and a vector of prices. In interactive mode, one can then simply type:
MATLAB will realize that it doesn't have a built-in function named val_port and search the relevant directories for a file named val_port.m, then use the function contained in it.
Whenever possible, you should try to create "m-files" to do your work, since they can easily be re-used.
Showing Values
If at any time you wish to see the contents of a variable, just type its name. MATLAB will do its best, although the result may take some space if the variable is a large matrix.
MATLAB likes to do this and will tell you what it has produced after an assignment statement unless you request otherwise. Thus if you type:
MATLAB will show you the value of C. This may be a bit daunting if C is, say, a 20 by 30 matrix. To surpress this, put a semicolon at the end of any assignment statement. For example:
Initializing Matrices
If a matrix is small enough, one can provide initial values by simply typing them in. For example:
Here, a is a scalar, b is a {1*3} row vector, c a {3*1} column vector, and d is a {2*3} matrix. Thus, typing "d" produces:
The system for indicating matrix contents is very simple. Values separated by spaces are to be on the same row; those separated by semicolons are on to be on separate rows. All values are enclosed in square brackets.
Making Matrices from Matrices
The general scheme for initializing matrices can be extended to include matrices as components. For example:
gives:
While:
gives:
Matrices can easily be "pasted" together in this manner -- a process that is both simple and easily understood by anyone reading a procedure (including its author). Of course, the sizes of the matrices must be compatible. If they are not, MATLAB will tell you.
Using Portions of Matrices
Frequently one wishes to reference only a portion of a matrix. MATLAB provides simple and powerful ways to do so.
To reference a part of a matrix, give the matrix name followed by parentheses with expressions indicating the portion desired. The simplest case arises when only one element is wanted. For example, using d in the previous section:
In every case the first parenthesized expression indicates the row (or rows), while the second expression indicates the column (or columns). If a matrix is, in fact, a vector, a single expression may be given to indicate the desired element, but it is often wise to give both row and column information explicitly, even in such cases.
MATLAB's real power comes into play when more than a single element of a matrix is wanted. To indicate "all the rows" use a colon for the first expression. To indicate "all the columns", use a colon for the second expression. Thus, with:
In fact, you may use any expression in this manner as long as it evaluates to a vector of valid row or column numbers. For example:
Variables may also be used as "subscripts". Thus:
Particularly useful in this context (and others) is the construct that uses a colon to produce a string of consecutive integers. For example:
Thus:
Text Strings
MATLAB is wonderful with numbers. It deals with text but you can tell that its heart isn't in it.
A variable in MATLAB is one of two types: numeric or string. A string matrix is like any other, except the elements in it are interpreted asASCII numbers. Thus the number 32 represents a space, the number 65 a capital A, etc.. To create a string variable, enclose a string of characters in "single" quotation marks (actually, apostrophes), thus:
Since a string variable is in fact a row vector of numbers, it is possible to create a list of strings by creating a matrix in which each row is a separate string. As with all standard matrices, the rows must be of the same length. Thus:
Matrix and Array Operations
The Mathworks uses the term matrix operation to refer to standard procedures such as matrix multiplication. The term array operation is reserved for element-by-element computations.
Matrix Operations
Matrix transposition is as easy as adding a prime (apostrophe) to the name of the matrix. Thus:
To add two matrices of the same size, use the plus (+) sign. To subtract one matrix from another of the same size, use a minus (-) sign. If a matrix needs to be "turned around" to conform, use its transpose. Thus, if A is {3*4} and B is {4*3}, the statement:
will get you the message:
while:
will get you a new matrix.
There is one case in which addition or subtraction works when the components are of different sizes. If one is a scalar, it is added to or subtracted from all the elements in the other.
Matrix multiplication is indicated by an asterisk (*), commonly regarded in programming languages as a "times sign". With one exception the usual rules apply: the inner dimensions of the two operands must be the same. If they are not, you will be told so. The one allowed exception covers the case in which one of the components is a scalar. In this instance, the scalar value is multiplied by every element in the matrix, resulting in a new matrix of the same size.
MATLAB provides two notations for "matrix division" that provide rapid solutions to simultaneous equation or linear regression problems. They are better discussed in the context of such problems.
Array Operations
To indicate an array (element-by-element) operation, precede a standard operator with a period (dot). Thus:
You may divide all the elements in one matrix by the corresponding elements in another, producing a matrix of the same size, as in:
In each case, one of the operands may be a scalar. This proves handy when you wish to raise all the elements in a matrix to a power. For example:
MATLAB array operations include multiplication (.*), division (./) and exponentiation (.^). Array addition and subtraction are not needed (and in fact are not allowed), since they would simply duplicate the operations of matrix addition and subtraction.
Using Functions
MATLAB has a number of built-in functions -- many of which are very powerful. Some provide one (matrix) answer; others provide two or more.
You may use any function in an expression. If it returns one answer, that answer will be used. The sum function provides an example:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.