PH15010 Laboratory Techniques An Introduction to MATHCAD Introduction • • • • Review of last week Vector and Matrix Maths Statistics Solving simultaneous equations with matrices Review of Last Week • Processing experimental data with arrays • Adding Error bars to graphs • Pre-Processing Data – Convert l to f – Logarithms for power law • Extract slope & intercept from data Functions Reviewed • Take one or more inputs (parameters) and return a value or values • From f(x) toolbar if you don’t know the name • Afunction(parameters)= • Avariable:=Afunction(p1,p2,p3…) • Use the help system/f(x) dialog Matrix & Vector Maths • Used for: – – – – – – Processing experimental data Statistics Resolving forces etc in 2D, 3D Co-ordinate transformations Simultaneous Equations Applying transforms to data Creating Arrays in MathCAD • 2 ways of entering arrays – Input Table (already encountered) • Spreadsheet type entry • Expand to fit data available – Insert Matrix Dialog box • Array of placeholders • Good if you know how many elements in matrix/vector Creating an array with Insert|Matrix… • Same for defining ordinary variable • Follow : with – [:::] from toolbar – <ctrl-m> – Insert|Matrix • Dialog box for rows & columns Creating arrays with Insert|Matrix… Empty 3 – element vector MyVector 1 MyVector 2 Placeholders to fill in 3 1 MyVector 2 mm 3 Can use units Matrix Maths #1 VA 1 VA 3 2 MA 1 3 3 MA 3 6 VA VB 11 MA MB 19 22 MA VA 5 2 3 4 5 3 6 9 12 Addition of constant Multiplication by scalar Dot product of 2 vectors Dot product of 2 matrices 43 50 11 Dot product of matrix and vector Matrix Maths #2 (Symbolically) a b c d y a b a d c d a b c d a x c x x K • Matrix Multiplication b y d y b c • Determinant a K b K c K d K • Addition of constant Matrix & Vector Maths • Add, Multiply & Divide matrices/vectors • Must have correct numbers of rows & columns Addressing Arrays with Indices • Index addressing allows access to individual elements of array • Examine & Set elements • Indices start at 0 • Indices use [ key • 2D matrices use two indices for row, column selection Indices #1 Vector Example VA 12 666 VA0 12 VA0 VA 42 42 666 VA1 666 • Simple 2 element vector • Examine elements • Set element 0 • Result of setting Indices #2 Matrix Example 1 MA 2 3 999 42 666 MA0 0 1 MA0 1 2 MA0 3 MA1 0 MA 27 1 2 3 27 42 666 MA0 2 3 • Sample matrix • Examine elements • Subscript too big • Set element • Result Accessing Parts Of Arrays • Index addressing – single element • Column Extract operator M<> • Submatrix() function • Transposing matrices The submatrix() function 1 2 MyMatrix 3 4 1 1 4 8 27 9 Define a matrix 16 64 Matrix to operate on 1 1 submatrixMyMatrix0 ( 2 1 2) 4 8 9 27 Rows Columns Use submatrix() to extract part of the array Transpose Rows & Columns 1 2 MyMatrix 3 4 1 1 4 8 9 27 16 64 1 2 3 4 T MyMatrix 1 4 9 16 1 8 27 64 • Uses MT operator • From matrix toolbar Other matrix functions 1. Information • Return information about: – Size of matrix • rows(M), cols(M), last(V), length(V) – Contents of matrix • min(A), max(A) • V Other matrix functions 2. Manipulation • Create new arrays – Put 2 arrays together • stack(A1,A2) • augment(A1,A2) – Sorting • sort(V), reverse(A) • rsort(M,r), csort(M,c) Statistical Functions on Vectors #1 • From f(x) dialog box under “Statistics” • Averaging – mean() ‘Average value’ – median() ‘Half way item’ – mode() ‘Most common value’ Statistical Functions on Vectors #2 • Variance & Standard Deviation • 2 forms of each • Analysis of sample – Stdev(), Var() • Analysis of whole population – stdev(), var() • Note capitalisation Vectorise Operator #1 f (x) • From matrix toolbar • Forces evaluation on element by element basis • Overrides normal matrix maths f (x) – ‘dot’ product – Determinant |M| – Powers Mx Vectorise Operator #2 1 • Dot product 4 2 5 32 3 1 4 6 2 5 3 6 32 4 4 2 5 10 1 3 6 18 • With vectorise operator Range Variables • Take on a series of values • Define using semicolon [;] i 0 10 Start i 5 Finis h 15 Start Finis h Range Variables #2 • Restrictions on where used • ‘illegal context’ error message • Useful for filling arrays Filling Arrays using RVs • Define RV • Use RV in array index & expression i 0 3 timei time i 10 min 0 0 600 10 3 1.2 10 1.8 10 3 s change the units to give: time 20 30 min Processing Arrays using RVs • Use RV in expressions to create output array from input array Range Variables with different step sizes • Can create RVs with different step sizes. • Enter first 2 values separated by comma [,] before typing [;] RV First Second Last Summation #1 • Summation operator on matrix palette • Sum of all elements in a vector V Summation #2 • Operators on Calculus palette • Do summation on any expression • 2 forms – Plain => defines local RV – Range Variable => uses existing RV m Expression n l Expression n Summation #3 5 i 15 • Sum of first 5 integers Countsi 9 • Sum of elements 20-30 of vector Counts i= 1 30 i = 20 Summation #4 Reciprocals of factorials • 2 terms 1 1 i 2 i= 0 • 4 terms 3 1 i 2.667 i= 0 • 9 terms 8 1 i i= 0 2.718 Data Files • Used Input table to put data into MathCAD by hand. • Can use Import on input table to get from a file (makes a copy) – Select table & right-click • Also can link to data file with File Read Component (preferred) Reading Data Files #1 • File Read Component • Reads from datafile array • Text files – Numbers separated by comma or tab • Other formats supported Reading Data Files #2 • Insert|Component…|File Read or Write • Wizard: – (Select file type) – Browse for file – Finish • Give name for array Reading Data Files #3 • Example This weeks worksheet • • • • • Arrays & Indices Columns (Revision) Submatrix() function Other array functions Reading data from file