0% found this document useful (0 votes)
70 views18 pages

Lecture 03

The document provides an overview of functions in Mathcad including: - Defining a function with inputs and outputs - Using control structures like if/else, for loops, and while loops within functions - Returning multiple outputs from a function using a data structure - Examples of functions that find minimum/maximum values, count values, and locate values within a vector

Uploaded by

burvanov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views18 pages

Lecture 03

The document provides an overview of functions in Mathcad including: - Defining a function with inputs and outputs - Using control structures like if/else, for loops, and while loops within functions - Returning multiple outputs from a function using a data structure - Examples of functions that find minimum/maximum values, count values, and locate values within a vector

Uploaded by

burvanov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

!

"

%
*

' (%)
!

&
&

&

&
&

,-

!&

&

!
(

'

(%)

FunctionName( InputList ) :=

"function statements"
"including controls (if, for, while) if needed"
for i 1 .. 10
"this separate line separates the for loop statements"
"from the rest of the function"
"output variable or vector"

/
,

0 -" &&!

&
$
,, 1 2 #!

!
5

&
&

&
& !

&
&

"
/#/
,,

&

&

&

!
& #
&
/
#
- $
&
3
*
4& "
5$6
78#
&
&

&

&

5&
9

&

! &&
&
&

$<==
7?

;
1
!

1=,
4+

#
&

&

>+ + ,
4 1='%,
4 9>;9%'

&
&

&
&

!
*

&

&

*
&
&

&

Mathcad Basics

23

@
$ )> 4> ;,
A= :>+9 *+4( ,
>4 1= .%'= 4%'= %. A%9,
%B0=. ,
4 :>+9 '%,
4
9>;9%' >9 ,
4 1=*+4( ,
>4 )> 4> +.= 1=4%'=>*%4 =C,
.,
4; *+4( ,
>4
7? <
5&

&
#,

&

&
/
!
!

&

&

&
-1
,E
&
/ 5D

/
!

&
!
/
F
#

*
D

&

' (%)
3
1 8

- G

5&

'

'

&
&

!
!
!
@ !
'

&

&
"
5#
!$
75H I J5!I!H 6

&
K
6
K

! &&
#K
6
7K

!&

5
!

&

,
&

D
! -

&
$)
$(
$(
,.
)
)

&&

!
! & #
"D
&
" D
"
&
"

Mathcad Basics

&
&
$
$
D

!
5

&&

$
"

#! L

$
&&

24

if "condition is true"
"statement 1"
"statement 2"
* D

"

&

&

&

&

for variable start , next .. end


"statement 1"
"statement 2"
D

"

&

&

while "condition is true"


"statement 1"
"statement 2"

Mathcad Basics

25

Venn Diagram of branching


Otherwise we are in this space
If Condition I

If Condition II

If Condition III

Example #1: Assigning grades


If grade >= 90
`A'

If 80 > grade >= 70


`C'
If 70 > grade >= 60
`D'

If 90 > grade >= 80


`B'

'

(%)

5
D

D
=5 &

&

&

&

2 64> =6< !

grade ( x) :=

If grade < 60
`F'

out "you get an A"

,
*"N$
if ( x 90)

out "you get a B"

if ( x 80) ( x < 90)

grade ( 50) = "you fail"

out "you get a C"

if ( x 70) ( x < 80)

grade ( 61) = "you get a D"

out "you get a D"

if ( x 60) ( x < 70)

grade ( 75) = "you get a C"

out "you fail" if ( x < 60)

grade ( 88) = "you get a B"

out

Mathcad Basics

grade ( 93) = "you get an A"

26

Even if one condition is found true, all other conditions are still checke

=5 &

2 6

G
grade ( x) :=

&

out "you get a D"

if x 60

out "you get a C"

if x 70

out "you get a B"

if x 80

out "you get a A"

if x 90

grade ( 50) = "you fail"


grade ( 61) = "you get a D"
grade ( 75) = "you get a C"
grade ( 88) = "you get a B"

out "you fail" if x < 60


out

=5 &

2 64

4=.
D

&
&

Mathcad Basics

grade ( 93) = "you get a A"

D
D
!
0 -

&
&

&
A

&

"

,
,
5

#
!#

5
-

27

If grade >= 60 `D'


If grade >= 70
`C'
If grade >= 80
`B'
If grade >= 90
`A'
If grade < 60
`F'
%

&

6
grade ( x) :=

if x 60

grade ( 50) = "you fail"

out "you get a D"


if x 70

grade ( 61) = "you get a D"

out "you get a C"


if x 80

grade ( 75) = "you get a C"

out "you get a B"


out "you get an A"

grade ( 88) = "you get a B"

if x 90

grade ( 93) = "you get an A"

out "you fail" otherwise


out

=
.

5Q M #

0
&
%

!
&&
&

Mathcad Basics

,
*D

&

#
&&

.
&

&

& "
,

$
&&

28

R
#

&

0 B

&

for

"
* &

&

#,

%
5&

&&

" #
$' (%)
38
$
#

!
&

??

&

&

??

! & #
&
S #T"
6

3 8
&
&&

#
"$ *

for i 1.. 5
5

( &&

&

"$
6
# ##
@#

&

!#

!3

for i 1 .. 5

vector i
i

D
"$

=5 &

" $ &&

6" $

2 6)

'

&

"$

"$

&
(%)

+
"
G

D
&

U
-

#
&F

x :=

s0

i := 0.. 4

for i 0 .. 4
ss+i

x :=

s := 0
x := for i 0 .. 4
ss+i

i
x = 10

Mathcad Basics

x = 10

x= 4

29

=5 &

2 6)
' (%)-

&
&&

sum( n) :=

"E

s0

&

sum( begin , end ) :=

for i 0 .. n

!#

s0
for i begin .. end

ss+i

ss+i

sum( 4) = 10

=5 &

sum( 0 , 4) = 10

2 60 -

sum( 1 , 10) = 55

&

3
,D

&

3 D

&

&

sum( begin , end ) :=

sum( begin , end ) :=

s0

s0

for i begin .. end

for i begin .. end

ss+ i

ss+ i

if i < 0

continue

s s + i otherwise

sum( 4 , 4) =

sum( 4 , 4) = 20

&

=5 &
8

5&

&
#

&

2@6%

&

3
,D

&

3 D

,
,

otherwise

if i < 0

6
6
6

Mathcad Basics

&
&

30

'

)
6

6
?
?
38
&?

M ?
?

&

&

&
&

"
3

$
3 8
?

&
E
&

6
=
.
D

D
)

5
#
#

%
;
&
@L
>

&

!
&"

&

&
3

8
#

&

3 8
#

$
0 -

&

'

(%)

75
84
Grades := 23

class ( x) :=

sum 0

96

pass 0

43

fail 0
for i 1 .. length ( x)
pass pass + 1 if x 60

result := class ( Grades)

fail fail + 1 otherwise

failures := result

sum sum + x

ave

passes := result

sum
length ( x)

average := result

fail
out

pass
ave

failures = 2

"
%

$=5 &

passes = 3

2 6 (

&
%

Mathcad Basics

average = 64.2

&

&

31

&
&# $

6 $

5D
# $+

&

& 4

&
0

P
x
Deflection =

P
(3Lx 2 x3 )
6 EI

Mathcad Basics

32

I := 600

E := 29000

P := 20

BeamDefl( L, NP , E, I , P) :=

L := 20

inc

NP := 10

L
( NP 1)

for i 1 .. NP
x 0 + ( i 1)inc
i

defl
i

P
6 E I

( i)2 (xi)3

3 L x

x
defl
results := BeamDefl( 20, 10, E, I , P)
results =

{10,1}
{10,1}

We've set up the output from the function to contain the two vectors:
x-coordinates and deflections at those coordinates.
They are placed as individual elements in a 2x1 vector.
Note that when we display the contents of 'results',
it tells us that each of the two elements are a 10x1 vector.
Thus 'results' is called a 'data structure'.
The difference is that each element in a data structure can be a vector or matrix,
not just a scalar.
These elements are indexed just like a vector (with a subscript).
In order to display and use the contents of the data structure,
we need to assign a name to each of the elements in the data structure.
xaxis := results

defl := results

defl 0.002

0.004

10

20

xaxis

Mathcad Basics

33

@
.

2
I := 600

E := 29000

P := 20

BeamDefl( L, NP , E, I , P) :=

L := 20

inc

NP := 10

L
( NP 1)

i1
for x 0 , inc .. L
xaxis x
i

defl
i

P
6 E I

3 L x x

ii+ 1
xaxis
defl

results := BeamDefl( 20, 10, E, I , P)


results =

{10,1}
{10,1}

2 F

& F

Mathcad Basics

"

34

%
$

&
5

&

&

&

"

K
* K

&

! &

5
K

!
#K

!
&

,!

K K

%(

function ( v , t ) :=

j 1
j

B(
&&

'

( (

&&

)
function ( v , t ) :=

K
6

j1
while
j

) ,
K
% 0
&

K
K

K
#!

&&
!
function ( v , t) :=

j1
while v t
j

j j+ 1
j

Mathcad Basics

35

1
3

x :=

5
7

0 -

3
58
&
/
@W3

3
58
E

"
5#
@$7 3
#
8

function02( v , t) :=

F
j1
for i 1 .. length ( v )
j j + 1 if v t
i

break otherwise
j
function02( x, 4) = 3

FDD +

&

5
'

(%)

&

3 8

6
%
B

5
& 5

#
&

38 . #
&
6

Mathcad Basics

&
5

!
E

#
&&!

&

&
$
&

3
&
3

8
#
8

&
!

&

&
"
38
W
E

&

&

36

function03( v , t) :=

counter 0
j1
while v t
j

counter counter + 1
j j+ 1
counter
function03( x, 4) = 2

3
F
"

&
&

# !
!8 !F,
&

"
5#
O$7

, & 2M

function04( v , t ) :=

/7OW

3
A

counter 0
j 1
while v t
j

counter counter + 1
break if j

length ( v)

j j+ 1
counter

function04( x, 8) = 4

3
B
&

&

3
,D
3
G
8

&
&

'

(%)

&

&
+

K
&

>

!
'

&
&

&
!

&

!
"$ "

&

=5 &

&

"$

Mathcad Basics

&

37

O
&

9
Find_Negative ( vec ) :=

stop 1
location 1
while stop
if vec

list :=
1

location

12

stop 0

result := Find_Negative ( list)

location location + 1

result = 5

out_location
6

&
&

,
/
#

E
!
/

&
-

&

5
#
&
/

/
"
B

>

#
&
&

/ X
&
"

5
&

#
D
!

&
-

7 $
#

2
"

5
&

&
! &
#

/
#

=5 &

90
3

<0

out_location location

&

5&
& 0 -

&

$
6,
#4 # #=
&0

& 5& &

M
>

!#

Mathcad Basics

& #
&

!
M

38

"
& 5& &$
M

,
!

&

&
5

"
9
(

! &

"
& 5& &

&#

&

&$
&

&

! Y

M #

&
.

5D 5

P
x
Deflection =
&

Mathcad Basics

P
(3Lx 2 x3 )
6 EI

& 5& &

39

kips := 1000lbf

ksi := 1

kips
2

in
E := 29000ksi

P := 20kips

I := 50in

L := 20in

NP := 10

BeamDefl(L, NP , P , E, I) :=

P L

maxdefl

3 E I

while maxdefl < 0.06 in


This "While" control structure is designed
to adjust 'L' (length of the beam) until
'maxdefl' becomes less than 0.06 in.
(Constraint I impose on 'L')

L L 1.05
3

maxdefl

P L

Since maxdefl has a unit of length,


compare 'maxdefl' to other numbers
that have a unit of length.
NOTE: Consistency!!!

3 E I

i1
inc

The "for" control structure uses


the new 'L' value and computes deflection
along the whole beam.

L
( NP 1)

for x 0 , inc .. L
xaxis x
i

How many times?


Well, in this case, 'x' has an increment 'inc' that
contains real number. Therefore, I need to create
an index to locate values of 'x' and 'defl'.
As shown in two lines above, I assigned one to
a variable 'i'. 'i' is being used as the indexing variable
right here.

defl
i

P
6 E I

3 L x x

ii+ 1
xaxis

x_value

defl

0.1

0.002

deflection

deflection0.001

:= BeamDefl( L, NP , P , E, I)

deflection

in

0.2

0.4
x_value

0.6

0.8

0.05

10

20

30

x_value
in

To plot vectors (or 1-D arrays) having units that are


different from default units, you need to divide the vectors
by the unit you want. Otherwise, Mathcad returns a plot
with a default unit (The left plot above)

Mathcad Basics

40

You might also like