Mathcad Variable Names A string of characters (including numbers and some “special” characters...

31
Mathcad Variable Names A string of characters (including numbers and some “special” characters (e.g. #, %, _, and a few more) Cannot start with a number Capitalization & spelling count!!! A1 is different from a1 Be careful not to conflict with predefined name (e.g. g = acceleration due to gravity, m = meter, etc. -- which we will discuss later)

Transcript of Mathcad Variable Names A string of characters (including numbers and some “special” characters...

Mathcad Variable Names

A string of characters (including numbers and some“special” characters (e.g. #, %, _, and a few more)

Cannot start with a number

Capitalization & spelling count!!! A1 is different from a1

Be careful not to conflict with predefined name(e.g. g = acceleration due to gravity, m = meter,etc. -- which we will discuss later)

Be careful that variables and functions have different names

Subscripts

The “.” (period) starts a text subscript, which really is part of the variable name A1 is different from A1

The “[“ indicates an array subscript. Thus A1 indicates an element in the array A (it is the second element, as array subscripts start with 0 by default in Mathcad)

Functions

Similar to Excel function

Have a name with argument list in ( )

Return a value ( or set of values)

Common math functions implemented asfunctions or predefined symbols (from the calculator tool pad:

Square Root: Or \

Absolute Value: | | (put expression in edit box, hit | key)

Powers of “ e “ : e

Most available as function with a standard math name:

loglnsincosexpabs

Other somewhat useful functions:

If ( test expression, true result, false result)

Health (Temp) = if ( Temp>98, “Fever”, “No Fever”)

Health(100) = “Fever”

String functions --- to find length, take substrings, search, concatenate + others.

And, as you might expect, we can write our own.

We will look at writing simple functions that can be expressed as a single mathematical expression

Example:

Function to compute volume of an ideal gas as a function of the gas constant (R) , temperature (T), pressure ( P), and moles (n)

First, define the function as you would assign a value to a variable ( use the := sign )

Vol(P,n,R,T) := n*R*T/P

Use it like any other function:

Volume := Vol(100,1,0.08206,273.15)

Functions can take and use units:

Vol ( 1 atm, 2 mol, 0.80206 L*atm/(mol*K), 500K) =

41.03 L

But as with all use of units in Mathcad, must be consistent. If evaluation results in incorrect unit operations, will get an error message ( the variable or whatever in red )

Functions

Functions “know about” variable values defined to the left and above its definition and those values will be used when the function is used.

A:= 3Y(x) = A*3Y(2)=6A:=10Y(2) = 6

Functions

Functions can have other functions in them, providing they have been previously defined.

Y(x) := x2

A(y, x):= y2+Y(x)

A(2, 3) = 13Y13

Arrays, Vectors, & Matrices

Range Variables

Brief Introduction to Plots

ArraysOrdered set of numbers

VectorSingle Row Or Column

MatrixTwo or More

Rows and / or Columns

Arrays

Access elements with array subscript “ [ “

Maximum of two dimensions

Define with ctrl-m or matrix tool button

First element has index of [0] or [0,0](can change with the ORIGIN keyword)

Basic Operations

Scalar add & multiply -- affects Each element in the array

Determinant & Inverse -- square matrix only -- short cut key strokes ( | | & ^ -1 )

Add, subtract, multiply with usual matrix rules on size & shape -- standard math operators (+, - , *)

Resize Operations

Add / Delete RowsAdd / Delete ColumnsAdd / Delete Rows & Columns

Put array (or element) in edit box, type ctrl-m

Vectorize Operation

This is a non-standard matrix operation in which the indicated operation is performed element by element

2

4

3

5

4

6

5

7

8

24

15

35

2

4

3

5

4

6

5

7

8

24

15

35

Vectorized

2

4

3

5

4

6

5

7

26

46

31

55

Standard Multiply

Useful, but be careful!

Range Variable

A special type of Mathcad variable

Takes on the indicated range of values when found in the worksheet

Range Variable

Var := First, Second; Upper Limit

( the ; shows as … )

J := 1,3;10

J takes on values 1, 3, 5, 7 & 9 each timeit is in found in the worksheet (following usual worksheet evaluation – To right or anywhere below)

Define and fill a matrix in 4 steps:

i 0 1 3

j 0 1 3

Mi j 2 i j 1

M

1

1

3

5

0

2

4

6

1

3

5

7

2

4

6

8

Arrays can have units, but each element must have the same dimensions

Range variable not limited to integers:

Temp 273.15 293.15 350

Temp273.15

293.15

313.15

333.15

Range variable can have units:

Plots

As in Excel, many type of plots.

We will look at the x-y plot

From the menu:

Insert / Graph / X-Y plot

Add X vector in the box on the x axis

Add Y vector in the box on the y axis

Can add multiple x – y vector pairs.

Works in internal units – no units on plot

Insert / Graph / X-Y Plot

Click on plot, then right click to get format menu

Plots

If would like specific units for a plot, need to convert the data before or during plotting.

Say wanted previous plot in ft & ft^2

0 5 10 15 200

500

1 103

1.5 103

A R( )

ft2

R

ft

As with Excel, plot not complete until has title and axis labels

0 5 10 15 200

500

1 103

1.5 103Area of Circle

Radius, ft

Are

a,ft

^2

To add axis labels & titles, select plot then right click.

Choose “Labels” and add as desired

To get rid of Mathcad’s variables,choose “Trace” and check “Hide Arguments”