MATLAB AND DATA SIMULATION PART A

advertisement

MATLAB AND DATA SIMULATION PART A

Getting familiar with MATLAB

There will be a folder titled Chem426 on the D drive of each computer. In this folder there is a folder called Students. Create a personal folder in this folder to store data you create today. You can do as you wish with the contents of this folder but you may be using other computers to access Matlab so be sure you also copy any data, Matlab Work

Space information , or figure files you wish to work on onto your own flash drive.

Periodically the computer Desktop will be cleaned off if it is tending to get cluttered, so do not store anything you wish to keep on the Desktop. Always store it in your personal folder on the D drive Chem426 folder..

In the Chem426 folder there is another folder called Files for Transfer. Copy this folder onto your own storage device so you have all of the files it contains, which you will be using during the course. Any time you use Matlab the path must be set to access these files. See Setting the Matlab Path below.

Instructions at the end of a line such as enter or return mean press the enter or return key not type out the word enter or return.

During this lab session you will be introduced to Matlab and be given the opportunity to apply this software to the analysis of data similar to data you will be collecting in the future This lesson is designed to teach you by doing. If you can’t figure out what all of the command language means just follow the instructions to see how Matlab responds and learn through experience.

With computers using windows software click the mouse on the Matlab icon to invoke

Matlab. If the Matlab icon isn’t visible on the desktop go to the Start menu on the bottom left of the screen to find the Matlab program. The command window to Matlab will open.

Instructions or commands for Matlab to execute are typed into this window. When the enter key is pushed on the keyboard, Matlab will execute the commands typed in the most recent command line. A command history window may also open next to the command window. If it doesn’t, click on the view option and select command history.

This window keeps a record of commands. If you make a mistake and get an error message when pressing the enter key you can either use the up arrow to retrieve a command line and then retype it or cut and paste the command from the command history window into the command window.

There is help available in Matlab on all of the operations provided. These can be seen by typing help and pressing the enter key. Explanations on each operation can be found by typing help followed by the operation you want help with. Matlab has many function programs that will do calculations on sets of variables. The variables you want the calculations performed on are to be included in parentheses( ) following the function. In the help menu is a list of these functions and you can obtained an explanation of most of the functions by typing help followed by the function name.

It is possible to write individualized programs in Matlab, which this exercise covers later.

Some programs were written specifically for this course and are stored separately from the general Matlab programs. Your TA will tell you where to locate these programs.

You must then tell Matlab where these programs are. This is done by setting a path in

Matlab. Do this now using the following steps.

Setting the Matlab Path:

After opening Matlab

Click on the following buttons in order:

File

Set Path

Add with subfolders – a browser will open.

Browse for the folder containing program files for this class

Click on that folder to highlight it only but not to open it to show subfolders

OK

Save

Close

Numbers in Matlab are presented in a short format consisting of four digits after the decimal point by default. By typing: format long , 14 digits will follow the decimal point.

MATLAB can operate only on a rectangular, numerical matrix. However a single number such as 7 is a 1-by -1 numerical matrix called a scalar. Lists of numbers in a row

such as 5, 7, 9, 11, 15, 19, 39, 84 or in a column:

2

4

6

8

10 are single row or single column matrices called vectors. MATLAB is used to perform calculations on either single results or on a set of data.

It is important to be aware that MATLAB uses the symbol log(X) to mean lnX and log10(X) to mean logX. sqrt(X) represents the square root of X.

The easiest way of entering data is to use an explicit list. The elements in the explicit list are separated by spaces or commas, and are surrounded by brackets []. A semicolon, ; , indicates the end of the rows of a matrix. For example in your program file type:

A=[1 2 3; 4 5 6; 7 8 9] then press return.

You should see the following:

A =

1 2 3

4 5 6

7 8 9

The matrix A is saved in memory for later use.

Now try a few simple exercises to get the feel of how MATLAB works. Be aware that

MATLAB is case sensitive by default such that the letters capital A and small a can each have different values.

Type: x=10 and press return.

Type: x and press return.

Type: x=5+2+3 and press return.

Type: x=(2+3)*2 and press return.

Type: y=x*x and press return.

Type: z=sqrt(y) and press return.

Type: a=z-x and press return.

Type: a and press return.

Type: z=y^(1/2) and press return.

Type: 10^(-4) and press return.

Type: 1e-4 and press return.

Take a little time to try a few of your own calculations, then try operating on a data set in the form of a row vector as follows.

Type: A=[1 2 3 7 5 6] and press return.

The fourth element in the A vector can be changed to make the vector a linear sequence of numbers. Any single element of a matrix can be viewed or altered by typing the matrix name {A in this instance} followed by parentheses containing the row and column numbers designating the matrix element of interest, separated by a comma.

Type: A(1,4) and press return.

Type: A(1,4) = 4 and press return.

Type: A and press return.

Type: A(1,1:4) and press return.

What does the colon do?

In this notation, the "1" before the comma specifies row 1 of the matrix A (Note: Because

A is a row vector it has only 1 row. If you try to specify any other row you will get an error message.) The 1:4 after the comma specifies columns 1 through 4 of matrix A.

Here are some other useful ways to build matrices that you may need to be aware of :

Type: A(1,[1,2,4]) and press return.

This command demonstrates using a vector, [1,2,4] to specify the desired columns from matrix A.

If you do not want to see the answer for each entry, type a semicolon before pressing return. For example:

Type: A1=[1 2 3 4 5 6]; and press return.

Type: A2=[7 8 9 10 11 12]; and press return.

Type: A=[A1 A2(1,1:4)] and press return.

Do you understand what happened and why? If not, ask for assistance.

And finally type: B = A’ and press return.

A useful command function is linspace x = linspace(0,10,6)

.

*10^(-8) ( type help linspace in the command window for information on how this function works).

Matlab will return x =

1.0e-007 *

0 0.2000 0.4000 0.6000 0.8000 1.0000

A vector of 6 evenly spaced values between 0 and 10 has been created. Then the values in that vector have been multiplied by 10

-8

.

Programming with Matlab .m files.

Commands can be saved in files and executed as a unit. Files that will do this have a

.

m extension and are created by going to the file option and selecting new, M-file . When this is done an editor screen will open with Untitled#.m at the top. If several commands have been typed into the command window and have been found to work successfully they can be copy and pasted either from the command window or command history window into an M file. The commands in an M file must start with a letter so when copying don’t copy extraneous characters like the >> marks. Commands are executed from M files by selecting the Debug, Run option – or by naming the M file with the save as option, saving it and typing its name in the command window. M file names must not contain spaces or Matlab will not recognize the file name as a command to run the file. When the

M file program finishes, results from the file appear in the command window.

Caution: when saving files save them back to the original directory they came from. The Matlab editor has a default save setting to a Matlab\work folder and will create a second file with the same name making it difficult to know which file is running when it is invoked from the command window.

Also Matlab program files will not run if they are given the same name as a variable name in the command space. Matlab will simply display the variable contents rather than running the program if the program has the same name as a variable.

Open a new M file. and use it to save and execute commands for the remainder of this assignment. Errors are much easier to edit from M files since the whole command line doesn’t have to be retyped to make corrections.

Part B Using MATLAB to Plot and Analyze Data

{Material to be turned in is in bold print preceded by a number. There are a total of five items to turn in, however item 1 has four parts and item 3 has two parts.}

Calibration of scientific instruments is important in order to ultimately quantify an analyte with well-defined precision and accuracy.

Simulated calibration data for an analyte is shown below in a table. The absorbance signal A was measured as a function of the analyte’s concentration, C (mol/L), following

Beer’s law:

A=  b C

where b is the path length ( 1.0 cm for this experiment ) and   is the analyte molar absorptivity (L mol

-1

cm

-1

). As you will see, the measured absorbance has experimental uncertainty ( i.e., noise) , at each concentration.

The first requirement for data analysis is that the data must be imported into Matlab.

The file Calibration Data 1 resides on the desktop of this computer. The Matlab Import

Wizzard function will bring this data into the Matlab command wincow.

Importing Spectral Data into Matlab

Under File in Matlab select Import Data.

From the Import Data Window that appears browse for and select the data file that is to be imported from the flash drive. In this first exercise

The file CalibrationData1.txt is to be imported from the

D:\Chem426\Example Data folder. After selecting the file select Open form the Import Data Window.

From the Import Wizard window that appears verify that comma is selected for separating columns. In this case commas do separate the columns of data. In other files tabs or other spacing indicators may.

There may also be any number of columns of data from 1 on up. Not all columns will need to be imported Into Matlab. Also verify that the

Number of text header lines Is correct. The Calibration Data 1.txt file has one line of header information; other files may have more than one header line or none.

The table on the right of the window should contain 2 columns of data when the data tab is chosen. The textdata tab should have two headings:

'Standard Concentratin' 'Absorbance '

Or

[1X22 char] 'Absorbance '

(If the text of the header is too long to print the size of the text will be indicated.)

Select Next.

Then select: Create vectors from each column using column names.

Right click on the Absorbance . . . name. Left click on the text box that says Rename Variable. Shorten the name of this variable to Abs to make it easier to deal with in Matlab. Follow the same procedure to rename the standard concentration variable to remove any spaces from the variable name. In this case conc would be a good choice. The variable name

cannot start with a number or contain spaces.

Select Finish and the variables will be imported into Matlab as single column vectors.

Calculations and programming using MATLAB (to be handed in)

Polyfit is a Matlab function that will output the coefficients of a polynomial as determined by the method of least squares. In this example it will be used to determine the slope, m, and intercept, b, of a first order polynomial fitted to the x and Abs vectors created above, i.e. the best fit line. You may type help polyfit in

the command window to find out more about this function. type: p = polyfit(conc,Abs,1); m = p(1); b = p(2);

Use these m and b values to calculate the best fit absorbance values, yfit, for each sample concentration by x by typing: yfit = m*x + b;

An analogy can be drawn between the standard deviation of a set of values from their mean and the deviations of y values for each x from the value of the fitted line

(yfit)for each x. If we call the deviations from the line the standard error, S line

, for the best fit line we can propose an equation for S line

as:

N

 i  1

 y data

 y fit

2 s line

N  2 where N is the total number of concentrations analyzed and y stands for absorbance. N-2 are the degrees of freedom limited by the fact that both the slope an intercept values are determine.

The Matlab expression for this is:

>> sline = sqrt(sum((Abs-yfit) .^2)/(length(Abs)-2)); however -

Abs – yfit is the difference between each data point and the fitted or predicted value. The standard deviation of these differences has the same value as s line

. An equivalent way of typing the calculations for s line is to take the standard deviation of [Abs-yfit] and correct for the degrees of freedom.. The Matlab command for this is:

>>sline = std([Abs - yfit])*sqrt(length(Abs)-1)/sqrt(length(Abs)-2);

The Abs values contain random errors and are estimates of the true values for y.

Hence a line plotted from Abs is a estimate of a true line that would be plotted from the true values for y. Just as the true values for y can not be known so the true line for y can not be known. However, in the absence of systematic errors, limits can be set within which the true line can be expected to fit with a given degree of probability based on the number of data points measured. The limits obtained are called the confidence limits or confidence interval. An approximate equation for the confidence interval, CI, is:

CI  y fit

 ts line

N

The value for t can be found in the t table in the Appendix. N is the number of data points taken. A minimum of two data points are required for a straight line to fix the slope and intercept, so the degrees of freedom for a line are the number of data points minus two. For six data points the degrees of freedom are four.

For a confidence limit of 95% (that is the true line lies with 95% probability within the CI range plotted), t is 2.78. The value of the 95% confidence interval

(the distance above and below the best fit line) is calculated as:

CI 95 

2 .

78 s line

N

The 95% confidence interval calculated will determine two parallel straight lines above and below the calibration line.

Below are example commands for an M file that could be written to calculate the best fit line and the magnitude of the 95% confidence interval. Semicolons suppress displays in the command window so all of the data doesn’t print out and clutter up the window. Semicolons also indicate the start of a new command, so several commands can be typed on a single line.

To create an m file with these commands below, from the Matlab command window go to File\New\Blank M File

An Editor –Untitled will appear.

Enter the text below just as it appears. The ‘%’ symbol is a comment symbol that prevents the line of text from being recognized and executed. The semicolons, ;, at the end of each line prevent the variables and their values from being displayed as the commands are executed.

%CI_mfile.m t = 2.776 % Found in student t table for 4 degrees of freedom. Six data

%points – two degrees of freedom one for the slope and one for the intercept of

%the line. p=polyfit(x,Abs,1); m=p(1); b=p(2); yfit=m*x+b; sline=std([Abs-yfit])* sqrt(length(Abs)-1)/sqrt(length(Abs)-2) ;

CI95 = t*sline/sqrt(length(conc));

After entering the commands go to File \ Save As

Create a folder under the Chem_426_Students folder using your name for the newly created folder name and save the M file to that folder.

Go to File \ Set Path

Select Add With Subfolder

Scroll through the Browse For Folder window for the folder you created and select it.

Click OK in the Browse For Folder window.

Click Save in the Set Path window.

The type of M file just created is called a script M file and it requires no additional input to run. Simply typing the name of the file in the Matlab Command Window and the m file automatically creates the variables defined in the file. Typing who or whos in the

Command Window after running the file will cause those variables to be displayed.

Type who after running the script file and observe the variables created.

Now type plot(conc,Abs,’o’) and a figure will appear showing a plot of the raw data, absorbance vs. concentration using symbol ‘o’ for each data point.

Type hold on and press ENTER.

On the same graph plot the best fit line as a solid line by typing the command: plot(conc,yfit).

Also, on this same graph plot two additional fitted lines at plus CI95 and minus CI95 relative to the best fit line.

The plot command for these two lines would be: plot(conc,yfit+CI95,conc,yfit-CI95)

The above calculation of the confidence interval is an approximation of the calculation of confidence intervals for which there is a 95% probability that the next measurement of y for a given x will fall within the range defined by the confidence interval. However that exercise should give you a feel for the meaning of confidence intervals as they apply to linear plots, which will apply to most of the calibration data in this course.

In least squares fitting, the calibration line is best determined near the mean of the x and y values. There are uncertainties in both the slope and intercept of the best fit line that cause the confidence intervals to increase as the distance from the mean x and y values increases. The actual confidence interval for reading the next data point in the y direction as a function of the lines position in the x direction is more properly defined as:

CI ( x )  ts line

1 

1

N

( x  x )

2

 ( x i

 x )

2

Or in Matlab for the above case

:

CI95 = 2.78*sline*sqrt((1+ 1/length(x)+((x-mean(x)) .^2) ./sum((x-mean(x)) .^2 ) ));

If several readings of a sample are to be averaged together then the confidence interval is defined as:

CI ( x )  ts line

1 p

1

N

( x  x )

2

 ( x i

 x )

2

, where p is the number of sample readings that are averaged.

The confidence intervals along the x axis as a function of y CI( y ) is given by the formula :

( x  x )( t

2 s

2

/( m

2  ( x i

 x )

2

) )  ( ts / m )[( x 

1  ( t 2 s 2 x )  ( x i

/( m 2  ( x i

 x )  ( 1  ( t

2 s

2

 x ) 2 ) )

/( m

2  ( x i

 x )

2

) ))( 1 p

An explanation of why this interpretation of the confidence interval is preferable is given in the Appendix. However a program called plotdata .m has been written for this class that will calculate these more precise confidence intervals

 1

N

)]

1 / 2

Further programming:

If other data were to be plotted the values for conc and Abs can be edited and the script

M file can be rerun to do the new calculations. However Matlab uses a more practical approach called function files. Function files are just script files that have a first line containing the word function and a statement of what the input to the file must be and output could be.

Go to the file, open option and browse for the file plotdata.m in the Chem 426 folder.

This is a shorthand name for a data plot function file created for this class. Select this file and it will open this file in the editor.

The top line of the program says:

[yfit,Xo,CI95XoL,CI95XoR,CI95Yo] = plotdata(x,y,Yo)

;

First look at the information to the right of the ‘=” sign. For this function file to work the values in the parentheses (the input variables) after the plotdata term must exist in the workspace or be typed directly into the parentheses as vectors. When executing a function, Matlab will only look at the location of variables in the parentheses, not their names. So in this example for the x in parentheses the user would enter conc and Abs for y . Assume that a test sample was analyzed that gave an absorbance reading of 0.7. Type

0.7 in the position of the Yo in parentheses. Use commas to separate the enteries.

Now look to the right of the ‘=’ sign. This file has five outputs. yfit ,t he vector of y values for the best fit line, Xo , the unknown concentration, as determined from the best fit line and the test sample absorbance, Yo,

CI95XoL , the 95% confidence interval to the left of the calculated unknown x value, CI95XoR , the 95% confidence interval to the right of the calculated unknown x value, and

CI95Yo

, the 95% confidence interval along the absorbance or y axis.

To get all of the output variables to appear the command must be typed into the command window with names that the output variables are to assume appearing in the brackets preceding the equal sign in front of the function name like so:

>> [yfit,Xo,CI95XoL,CI95XoR,CI95Yo] = plotdata(x,y,Yo)

Comment lines indicated with the “%” as the first character in the line that follow the first line of the plotdata.m

file also shows what the input and output values represent. . The

“%”, comment marks allow the program writer to comment on the commands written and explain what they are intended to do. Typing help and the file name will cause the only the first series of comment lines to appear in the Command Window Comment lines can

be used throughout the file, but only the first series will become visible with the help command.

Type the plotdata command into the Command Window supplying output variable names in the brackets and the input variables described in the parentheses. As stated above, use

0.7 for the Yo input variable..

On pressing return values will be calculated and assigned to the variable names appearing in brackets before the ‘=’ sign.

Along with creating the variables described above, this program also plots the data along with extensions of the confidence intervals to the axes.

An unknown sample gave an absorbance reading of 7.0X10

-1

.

(1a ) Use this value as the unknown absorbance (Yo) in the plotdata function and run the plotdata function to determine the concentration of the analyte in the unknown

.

(1b) Put titles, x and y axes labels on the plot, including your initials in the title. Print and turn in this plot.

(1c)Turn in the left and right uncertainties in this concentration value, i.e. the value for

CI95XoL and CI95XoR

in the function file. (1d)

From the command line in the function file that calculates Yo explain how this uncertainty is calculated.

1.

% Relative Standard Error is calculated as follows:

RSE %  s line  100 % y

, where y is the y value of the unknown.

(2) Type in the command for S

line

above. Given that the y value for the unknown is 0.7, what is the relative standard error for the unknown based on the S

line

?

In addition to analyzing the calibration standards the 0.0 concentration standard, the blank, was analyzed a total of 4 times. Load the blank data from the file Blank Data 1.txt Deselect the column containing the concentrations so it won’t be imported. Name the remaining variable to be imported into

Matlab: blank.

The instrument limit of detection is three standard deviation units above the average of the blank reading. The standard deviation of the blank is determined by the following equation:

N

 i  1

 y blank

 y blank

 2 s 

N  1 and can be calculated with the Matlab standard deviation function by typing: std(blank).

(3a) What is the instrument detection limit in this case?

(3b) What is the concentration limit of detection for this analyte on this instrument?

The concentration limit of detection is three standard deviation units of the blank divided by the slope of the calibration line.

Part C, Using Matlab to Perform Multicomponent Analysis by Classical

Least Squares analysis

Instruments capable of analyzing a sample at more than one wavelength can be used to analyze for more than one component in each sample. In order to do this the following conditions must hold true:

A. The sample must be analyzed at least as many wavelengths as there are analyte components to be determined.

B. Each component must have a reasonably different absorption at each wavelength from the rest of the components

.

Importing Spectral Data into Matlab

Under File in Matlab select Import Data.

From the Import Data Window that appears browse for and select the

Red data file import from the Example Data folder in the Chem426 directory on the D drive.

From the Import Wizard window that appears verify that Tab is selected for separating columns. Also verify that the Number of text header lines is correct, there is only one text header line. The path and file name will appear above the window displaying the data. The table on the right of the window should contain 2 columns of data when the data tab is chosen. Selecting the text data tab should show two headings:

'Wavelength' 'Absorbance '

Select Next.

Then at the top select: Create vectors from each column using column names.

Right click on the Absorbance . . . name. Left click on the text box that says Rename Variable. Rename this variable red. The variable name

cannot start with a number or contain spaces.

Right click on the Wavelength . . . variable and shorten its name to a convenient term such as wavln.

Select Finish and the variables will be imported into Matlab as single

4X1 [rows x columns] vectors. The wavln vector will contain the wavelength values 250, 300, 350, 400 nm and the analyte red vector will contain the absorbance values that correspond to those wavelengths.

Also import the yellow, green, blue, and brown files. However for these files, the Wavelength variables can be deselected altogether by ensuring that a check mark does not appear in the box before the variable name.

The brown file contains test sample absorbance values for the red, yellow, green, and blue analytes. .All of the files except the brown will be used for calibration absorbance data in order to determine the concentration of those components in the brown sample.

Part I.

Create a concentration scalar value of 10

-4

M by typing:

C = 10^(-4)

Create a plot as Figure 2 by typing: figure(2) plot(wavln,red,wavln,yellow,wavln,green,wavln,blue)

Check that each analyte has maximum and minimum absorbances at different wavelengths.

Before printing the above plot, label the axes and give it a title, include your initials so you can locate it in the printer..

(4) This plot is to be turned in.

Next, determine the absorptivity coefficients of each analyte.

The absorptivity coefficient appears in the Beer’s Law equation for absorbance, A = B*C where A(a column vector) is the absorbances, B(a column vector) is the absorptivity coefficients, and C(a scalar) is the sample concentration.

Bred is found typing:

Bred = inv(C)*red

The output will be:

>>Bred = 10000

5000

1000

500

Perform this operation for remaining absorbance vectors (green, yellow, and blue)..

Verify that they are column vectors by typing each B variable, Bgreen, Byellow, etc. and verifying that the results line up as a column.

Create a B matrix for the four analytes combined by typing:

B = [ Bred Byellow Bgreen Bblue].

Note: there is a space between each entry in the brackets. Don’t suppress the output with a semicolon at the end of the line so you can see the resulting matrix.

Make sure you obtain this matrix:

10000 5000 500 500

5000 10000 1000 1000

1000 1000 10000 5000

500 500 5000 10000

When you get to the UV-VIS experiment you will be given a sample of unknown concentration in four analytes and measure an absorbance vector for that sample. In this exercise the sample’s absorbance vector is brown

The essential step of the UV-VIS experiment is to calculate the concentration vector of the unknown, Conc_brown. The UV-VIS section of the lab manual derives the formula for determining Conc_brown. Below is that final formula:

>> Conc_brown = inv(B’*B)*B’*brown

(5.) Use the B matrix and the brown vector in this formula to obtain the Conc_brown vector and turn your result in as the final part of this report.

26

27

28

29

30

22

23

24

25

40

60

120

Inf

15

16

17

18

19

20

21

11

12

13

14

7

8

9

10 t Table degrees of freedom Probability Alpha (  ) level

0.05 0.02 0.01

1

2

12.706

4.303

31.821

6.965

63.657

9.925

3

4

5

6

3.182

2.776

2.571

2.447

4.541

3.747

3.365

3.143

5.841

4.604

4.032

3.707

2.365

2.306

2.262

2.228

2.201

2.998

2.896

2.821

2.764

2.718

3.499

3.355

3.25

3.169

3.106

2.179

2.16

2.145

2.131

2.12

2.11

2.101

2.093

2.681

2.65

2.624

2.602

2.583

2.567

2.552

2.539

3.055

3.012

2.977

2.947

2.921

2.898

2.878

2.861

2.086

2.08

2.074

2.069

2.064

2.06

2.056

2.052

2.048

2.528

2.518

2.508

2.5

2.492

2.485

2.479

2.473

2.467

2.845

2.831

2.819

2.807

2.797

2.787

2.779

2.771

2.763

2.045

2.042

2.021

2

1.98

1.96

2.462

2.457

2.423

2.39

2.358

2.326

2.756

2.75

2.704

2.66

2.617

2.576

Appendix

Finding the b and m that gives the minimum difference in the squared values of the fitted line to the actual y data.

Q  n i

 1

[ y i

 ( b  mx i

)]

2

Take the partial derivatives of with respect to b and m,

Seteach partial derivative equal to zero, and

Solve the resulting system of two equations with two unknowns yields the following estimators for the parameters:

 Q

 m

 0 m  n i

 1

( x i

 )( i

 y ) n i

 1

( x i

 x )

2

It can be shown that x and ˉ both fall on the best fit line making the solution for b simply b  y  mx

An analogy can be drawn between the standard deviation of a set of values from their mean and the deviations of y values for each x from the value of the fitted line

(yfit)for each x. If we call the deviations from the line the standard error, SE line

, for the best fit line we can propose an equation for SE line

as: n i

 1

[ y i

 ( b  mx i

)]

2

SE line

N  2 where N is the total number of concentrations analyzed and y stands for absorbance. N-2 are the degrees of freedom limited by the fact that both the slope an intercept values are fixed once the line is determined..

Given a set of data points plotted as below of absorbance measurements at various concentration levels.

A best fit line can be determined using least squares analysis.

If the absorbance of a sample is measured, from this line its corresponding concentration can be calculated.

From the SE line

formula above 95% level confidence intervals can be estimated:

CI

95

2.78

 SE line

N t = 2.78 for four degrees of freedom N=6 – 2.

From the 95% confidence interval uncertainties in both the absorbance and concentration results can be estimated.

However the uncertainty is greater than this calculation suggests. Uncertainty in the line involves uncertainty in both the slope and intercept and formulas to calculate these uncertainties have been derived [they are not included here].

A graphic representation of what this implies can be seen when a line involving the slope minus its 95% confidence interval value combined with an intercept plusits 95% confidence interval value is compared to a line with a slope plus the 95% confidence interval and an intercept minus the 95% confidence interval. Both lines rotate about the x and y values.

If each data point was determined with only a single analysis the following confidence intervals would result. In this case the uncertainty on the concentration axis is quite marked.

If each data point represents the mean of multiple readings that are assumed to have resulted in them representing the true mean value of absorbance readings at each concentration level then the confidence intervals become much smaller. Instruments often take multiple readings and average them or integrate their signal over a sufficiently long time range to make this assumption reasonable.

If then the x axis is extended by a factor of three in each direction it becomes clear that the confidence intervals converge with the lines with the lines with slopes containing the addition and subtraction of the 95% confidence interval.

Additional Information on Matlab for the curious -

If I equals the vector 1+ i, 2 + 2i, 3+3i, where i equals

 1

, then this vector is entered into Matlab using the following command:

I = [1+ 1*i, 2+2*i,3+3*i];

Type I' then enter. MATLAB will return the transpose of the complex conjugate of I: ans =

1.0000 – 1.0000i

2.0000 – 2.0000i

3.0000 – 3.0000i

Type I.' enter, MATLAB will return the transpose of I: ans =

1.0000 + 1.0000i

2.0000 + 2.0000i

3.0000 + 3.0000i

If you are working in real numbers only then either ' or .' will return the same values; for R = [1 2 3];

Type [R' R.'] enter.

Does it return: ans =

1 1

2 2

3 3 ?

Do you see why?

Typing whos will return the names and sizes of all of the vectors you have entered so far.

Type: size(R) enter:

MATLAB will return the row and column lengths of the vector R.

There are two convenient ways to create row or column vectors. One is to use the linspace command. By typing LS = linspace(0,10,25) and pressing return you will create a row vector called LS that begins with the number 0, ends with the number 10, and has

25 equally spaced elements in it. To make this a column vector simply type LS = LS’.

Another way to create a row or column vector is to type RV = [0:2:10] and pressing return. This instruction creates a row vector called RV that begins with the number 0, ends with the number 10, and has all of its elements separated by a value of 2. Typing

CV = [0:2:10]’, creates a column vector with the same elements as the row vector, RV.

Matricies can be defined or operated on using for or while loops. This can be useful in programming with Matlab when you want the program count the elements in a matrix in order to perform a calculation on each element in that matrix. The “ for n=nfirst:nlast ” command instructs Matlab to create a set of variables from n1 to n2 one at a time if they do not already exist, and perform the commands that follow the “ for ” while applying the current value for n until an “ end ” statement is reached. The value for n is then increased by one and the cycle of commands repeated from the for to the end statement. This process repeats until the commands have been executed for all values of n from the first value for n to the last value for n. Such statements are called a for loop.

Type :

A = linspace(1,10,10); enter This loop creates a new vector

B = linspace(1,10,20); enter containing 10 values that equal

[m,n]=size(A) enter each value of A added to every for i = 1: n; enter other value of B.

C(i) = A(i) +B(2*i) enter end enter

After you type the final enter you will see the actual for loop executed by MATLAB, as it performs calculations involving selected elements of both of the defined matrices to create a third matrix. Review each of the above command lines until you clearly understand them.

The following while loop will accomplish the same result. i=1; while i < length(A)

C(i) = A(i) + B(2*i) enter i = i+1; enter end enter

The indentation is a convenience to make it easier to tell where the loop begins and ends. A “%” symbol prevents Matlab from reading any text written after it. So one could type: i=1; while i < length(A)

C(i) = A(i) + B(2*i) enter i = i+1; end%while to indicate that the “end” command applies to the while loop. This may be useful if one loop is nested inside another.

The use of loops is particularly useful in modeling and analyzing theoretical data for comparison with empherical sampling. Most data contains random noise. Matlab has a random number generating function called “ rand ”. Typing rand generates a random number from a uniform population distribution. However most noise or random error seen in experimental data has a normal distribution about a particular mean value. For this type of random number generation the command randn is used. randn generates random numbers with a normal distribution around a mean of 0 having a standard deviation of 1.

Suppose an experimental model predicts that for each given magnitude of input (say the presence of a 1 mM concentration of analyte), an instrument output of 1.5 volt is expected. The model also assumes that with no analyte present the instrument output is

0.1 volt. The relationship between input (analyte concentration) and output (instrument voltage) is output = 1.5 volt/mM x input + 0.1 volt.

A for or while loop would not be necessary to show this relationship in Matlab, but it is used here to demonstrate how to generate some theoretical data points and then establish a sampling of data with random error. The input will be mM for millimoles. The output will be V for volts. There will be ten data points ranging from 0.1 to 100 millimolar solutions. mM = linspace(0.1,100,10);

A common source of frustration with Matlab comes from mismatching the sizes of matrices. The following few exercises will help in figuring out this dilemma most of the time. When assembling data into matrices, it is important to understand the use of the comma, semicolon, and colon. Compare the outputs from the following commands:

A1= [1 2 3 4 5 6]; A2=[7 8 9 10 11 12]; B1=A1'; B2=A2'; enter

[A1 A2(1,1:4)] enter

[A1,A2] enter

[A1;A2] enter

[A1' A2'] enter

The space in the first example was treated like a comma. The following commands produce errors.

[A1 B1] enter

[A1;A2(1,1:4)] enter

Now try some calculations on the above A and B matrices that were created.

In order to operate on each individual element in A, a space followed by a period must precede the operation.

Type: B=A .^2 and press return

To emphasize distinction between operations on a matrix as a whole and on the individual elements of each matrix let Matlab generate a preprogrammed 5X5 matrix by typing:

M=magic(5) and press return

Now Type: M*M and press return.

Compare this to M .*M. Also compare M^2 and M .^2 or M^3 and M .^3.

Section 9 on page 13 of ;the MATLAB User's Guide tutorial discusses creating graphs.

To make a graph of the relationship between two matrices type: plot(A,B) and press return.

A plot with A on the abscissa and B on the ordinate will appear on the graphics screen.

Typing return will remove the plot and return to the command screen. The plot will be

saved as Figure 1 accessible from the bottom task bar.

Now type: title('B vs. A') and press return. note the semi quotes inside the parentheses.

Type: xlabel ('A') and press return.

Type: ylabel('B') and press return

Type: click on [Figure 1] on the bottom of the screen.

(Unless you type plot followed by parentheses indicating what to plot, the previous graph will always appear.)

Press return. Give the graph a different appearance by changing the line type.

Type: plot(A,B,'x') and press return

To understand line type options type:

>> help plot in Matlab.

Try this trick. Type:

plot (A,B,A,B,'ro') and press return

The blue line is the default type, red is the color and the o is the type you specified .

MATLAB can plot more than one function per graph. For example try:

Type C= A .^3 and press return.

Type: plot(A,B) and press return

Type: hold on and press return {this allows you to plot many functions on the same graph.}

Type: plot(A,C) and press return

Type: hold off to remove the hold on the plot.

Typing: plot(A,B,A,C) will have the same effect has the previous command using the hold on option.

You may add informative text anywhere to your plot using the gtext command .

Type: gtext([‘The maximum value is : ' num2str(max(C))]) and press return

The current plot will appear with either a mouse arrow or cross hairs which respond to the mouse or arrow keys on the keyboard. Press either the mouse key or return and the gtext message will be pasted onto the plot.

Text can also be added to a plot by specifying the coordinates where the text is to appear using the text command.

Type : text(1.5,15,['The maximum value is:' num2str(max(C))]) enter

Type help text for more information on the text command.

NOTE: MATLAB automatically scales plots so that the maximum and minimum abscissa and ordinate values will fit on the plot. If the maximum and minimum values are not whole numbers Matlab will extend the axes to the next nearest whole number.

In order to keep a record of your work you may log all of your commands into a file

Matlab labels a script file with an .m extension. This file can serve as your own personal program, which, when run step by step, activates the commands that were typed into it.

To create a script file select File at the top of the command screen. Then select New, then m-file. The Matlab Editor/Debugger screen will appear with a screen for writing the

Untitled 1 file. You may immediately go to save as to save this file with any name you choose for later recall. As you follow the directions below type the specified commands into this program file

Reset the cursor at the beginning of the line and select Debug from the top of the window.

Press the F12 key to set the break point at the beginning of the line. A red ball will appear at the beginning of the line. This selection might not be available unless you have used the save as command to save the file under a name other than Untitled 1. Next go to

Tools and select run. Go back to Debug and press the F110 key to run the program a single step at a time. (Note you can use just the F10 and F12 keys without selecting the

Debug option in the future. The command screen should then show:

By keeping both the command window large and the editor/debug window small you can switch from one window to the other, typing, editing and executing commands as you choose. You can clear and reset the break point to start the program at any point you wish.

Type a few commands in the editor/debugger then execute them single step (F10).

Download