DOC PREVIEW
MATLAB Guide

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Math 151-2 INTRODUCTION TO MATLAB L. J. Gross - August 1995This is a very basic introduction to the elements of MATLAB that will be used in the early part of this course. A much more complete description is available for purchase (The Student Edition of MATLAB for MS-DOS or the version for Windows), however as part of this course you may install either of these versions on a computer you use, as long as you sign an agreement that you will delete it at the end of the course. To get a copy, bring an appropriate number of high density DOS-formatted diskettes (one for DOS version, 4 for the Windows version) to the Math Lab and the assistants there will aid you in getting a copy. You must take along a signed agreement form, obtained from your instructor, to get this. Note that we cannot supply a Macintosh version of MATLAB. There is also extensive documentation on MATLAB available through the UTK MathArchives site athttp://archives.math.utk.edu/matlab.html/ or see the tutorial located athttp://www.math.utah.edu/lab/ms/matlab/matlab.html/ MATLAB is a mathematics package that allows you to easily solve many of the quantitative problems that arise in the life sciences. This document briefly describes some of the key elements in usingMATLAB to (i) do descriptive statistics; (ii) matrix algebra; (iii) probability, and (iv) discrete difference equations. These are all topics that will be covered in detail in the course and this document is designed to just aid you in solving problems in these areas using MATLAB. Note that the files referred to here as .m files are all available in the Mathematics Lab in directory W:\MATH\MATH151\MFILESA brief description of these files is given at the end of this document.DESCRIPTIVE STATISTICS, REGRESSION, and CURVE FITTING:Handling lists of data:In many laboratory and field experiments, you collect lists of data associated with measurements of experimental outcomes. Typically, the ordering of the data matters in that it makes a difference asto what measurement is made first, second, etc. For example, if you are measuring leaf lengths and widths, you would want to maintain the order in which you collected the data so that you know what length to associate with what width. We do this mathematically by putting the data in a list in which the order matters - we call such a list a "vector". Suppose we have the following data: Student number: 1 2 3 4 5 6 grade : 60 80 100 90 70 10In MATLAB you type: SNUMBER=[1 2 3 4 5 6]and MATLAB will then print out:SNUMBER = 1 2 3 4 5 6then you type: SGRADE=[60 80 100 90 70 10]and MATLAB will print out:SGRADE = 60 80 100 90 70 10Then the vector SNUMBER contains the number of each student and the vector SGRADE contains the grade of the corresponding student. We can then use a variety of MATLAB commands to makecalculations from these data. For example, if we want to find the arithmetic mean value of the above grades then all we have to do is type: m=mean(SGRADE) and MATLAB will print out:m = 68.3333If we want to find the standard deviation of the grades then we type: sdev=std(SGRADE)and MATLAB will print out:sdev = 31.8852NOTE: MATLAB is case sensitive (i.e small or capital letters make a difference - so be careful). Note as well that certain names in MATLAB are reserved for its use - so you would not want to namea variable "mean" or "std" - the program will not allow this.In both the above cases, MATLAB has now assigned the above numerical values to the variables m and sdev, and you can use MATLAB to make any standard calculations using these - for example to square m and subtract 3 times sdev type: m^2 - 3*sdevand MATLAB will print out:ans = 4.5738e+03You can use all the standard mathematical operators: + - * / ^We have learned so far how to set up our data in vectors on MATLAB and how to do some simple statistics commands. Now we are going to see how we can do a linear regression using our data.Another example: Here are some data of length and width of Acer saccharum leaves Length: 57 95 61 110 85 80 78 112 87 112 Width : 70 105 78 120 99 89 99 125 105 123 let us store our data in vectors : L=[57 95 61 110 85 80 78 112 87 112] W=[70 105 78 120 99 89 99 125 105 123]If we want the histogram of the length we type in: hist(L) and hit the return key and you will see the following histogram.To plot these points, with length on the horizontal axis and width on the vertical axis, using a + symbol for each of the data points we type in: plot(L,W,’+’) then hit enter and you will see the following graph:50 60 70 80 90 100 110 12070809010011012013050 60 70 80 90 100 110 12000.511.522.53Let us do a linear regression for the above data. We first calculate the regression coefficients: C=polyfit(L,W,1) hit the return key and you will seeC = 0.9162 20.9460This means that MATLAB has created a vector C=[0.9162 20.9460] with the first number in C being the slope and the second number in C being the y-intercept of the regression line. To graph the above line we need to find at least two values for the polynomial P(L)=0.9162*L+20.9460 corresponding to any two values of L. Here P(L) is the equation of the line with slope and y-intercept given by C. Let us for example choose the following two values of L: 57 112 and let us store them in a vector in MATLAB: LTEMP=[57 112]Then to find the values of P(L) that correspond to those numbers we use the following command: Y=polyval(C,LTEMP)then type in: plot(LTEMP,Y,L,W,’+’) hit enter and you will see the following graph.If we need to fit a higher degree polynomial to our data we change the number 1 to the degree of the polynomial we need - in general the command is polyfit(L,W,n) where n is the degree of the polynomial. For example, to fit a quadratic equation to a set of points, you would use polyfit(L,W,2).Saving data in MATLAB:Before quiting MATLAB make sure that you save the data you need for future use. For example 50 60 70 80 90 100 110 120708090100110120130suppose we want to save the data L and W. Then place a floppy in the drive a: and type in thefollowing: save a:math151.dat L W then hit enter. This will save our data in a file called math151.dat on a floppy, then to quit MATLAB type in: quitIn the


MATLAB Guide

Download MATLAB Guide
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view MATLAB Guide and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view MATLAB Guide 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?