Docsity

Lecture Notes on Functions and Control Structures | CGN 3421, Study notes of Civil Engineering

Toggle sidebar
bg1
  
Mathcad Basics
23


   
!      "# 
#$
 %   &    
'(%)  *  &  !
! & &+ 
&!#&!!
,- !&  !   
('(%)
*.
FunctionName InputList( ) "function statements"
"including controls (if, for, while) if needed"
"this separate line separates the for loop statements"
"from the rest of the function"
i 1 10..∈for
"output variable or vector"
:=
/,0-"&&! &$  &! ! 
   ,-    &# & 
   ,12# !&!& /-# 
!&  &"/-#/-$&
 &, , 3*4&"5$678#
 5  &        & &
& &! 
  &  &   !
5&
9&
: ! && !    ;!# 
  &  1  &
&! &
$<==1=,4+>++,41='%,49>;9%'
7?  &       & *
 &     !    & & 
     * &&  
&&
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Lecture Notes on Functions and Control Structures | CGN 3421 and more Study notes Civil Engineering in PDF only on Docsity!

FunctionName InputList( ) "function statements"

"including controls (if, for, while) if needed"

"this separate line separates the for loop statements"

"from the rest of the function"

for i ∈ 1 .. 10

"output variable or vector"

$ )> 4> ;,A= :>+ 9  *+ 4 (,> 4   1 = .%'= 4%'= %. A% 9 ,%B 0 =. , 4  :>+ 9  '%, 4 

 9 >; 9 %'> 9 , 4  1 =+ 4 (,> 4 )>4>+.= 1 =4%'=>% 4 =C,., 4 ;*+ 4 (,> 4 

 &&/- D'(%)

  /- 1   , E!   F  #       31  8  

!DD!   &  -  G 

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

 ! K 6 K   !&#K 67 K  

 ' !! D 5  

, D 5 L!   #!L 

$) "D& $

$("D$

$(" D$

,.& D) "$

   M  

Venn Diagram of branching

If Condition I (^) If Condition II

If Condition III

Otherwise we are in this space

Example #1: Assigning grades

If grade >= 90 `A'

If 80 > grade >= 70 `C'

If 90 > grade >= 80

`B'

If 70 > grade >= 60 `D'

If grade < 60 `F' 

  '(%)  5        D&    &   D& DD      

= 5 &2 64>= 6 < ! ,*"N$

grade x( ) out ← "you get an A" if( x ≥ 90 ) out ← "you get a B" if( x ≥ 80 ) ∧(x < 90 ) out ← "you get a C" if( x ≥ 70 ) ∧(x < 80 ) out ← "you get a D" if( x ≥ 60 ) ∧( x < 70 ) out ← "you fail" if( x < 60 ) out

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

grade 93( ) ="you get an A"



=   !

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

 G  D& 

grade x( ) out ← "you get a D" ifx ≥ 60 out ← "you get a C" if x ≥ 70 out ← "you get a B" if x ≥ 80 out ← "you get a A" ifx ≥ 90 out ← "you fail" ifx < 60 out

grade 50( ) ="you fail" grade 61( ) ="you get a D" grade 75( ) ="you get a C"

grade 88( ) ="you get a B"

grade 93( ) = "you get a A" 

= 5 &2 64 D& 

4    4=. D&  & , #  5 

 D &   !             ,  !#  -

 & 0 - A &"  5  $

   R  

     #    &       1     F

 B &    D

 for^ ∈ "*& & #, ??&&??3 8   %  " # &!& #  & #       $ '(%)  &     &&  "$ * 5 &# !  3 8   S #T" E     & !&  $#    6

for i ∈ 1 .. 5

vector i

←i

for i ∈ 1 .. 5

 #    D      6  " $  &   D " $

% '(%)  +D

"G&

)  U #

- &F

x s ← 0

s ←s +i

for i ∈ 0 .. 4

x = (^10) 

i := 0 .. 4

x

i

:= i

x = 10 

s := 0

x s ←s +i

:=for i ∈ 0 .. 4

x = (^4) 

= 5 &2 6)   &   " E

sum n( ) s ← 0

s ←s +i

for i ∈ 0 ..n

s

sum 4( ) = (^10) 

sum begin end( , ) s ← 0

s ←s +i

for i ∈begin ..end

s

sum 0 4( , ) = 10 sum 1 10( , ) = (^55) 

 = 5 & 2 6 0 - &        3,D&  8   3D 8         & 

(!      F

sum begin end( , ) s ← 0

s ← s + i ifi < 0 s ← s +i otherwise

for i ∈begin ..end

s

sum (− 4 , 4 ) = (^20) 

sum begin end( , ) s ← 0

s ← s + i ifi < 0 continue otherwise

for i ∈begin ..end

s

sum (− 4 , 4 ) =   4   -    5 &       &               &!      &!#       = 5 &2@ 6 % & 3,D&  8 3D  8  !  , 6 , 6       6  &     &  

 &   D 6 $ 5D 

x

P

2 3 (3 ) 6

P Deflection Lx x EI

− = −

         

I := 600 E := 29000 P := 20 L := 20 NP := 10

BeamDefl L NP( , , E, I,P) inc

L

( NP − 1 )

xi ← 0 +(i − 1 )inc

defli

− P

6 E⋅ ⋅ I

3 L⋅ (^) ( xi) 2 ⋅ (^) (x (^) i) 3 ← ⋅ −

for i ∈ 1 ..NP

x defl

  

  

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

results

  

  

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 1 := defl results 2

0 10 20

0

defl

xaxis (^) 

K*K   !  5 ! &!&  ! 

KK   !  5   

!L  5 !   

 #KK   !  5  

!L  5 !   ,!  5 !

 &!! # KK

 38W

%(    !   K K

function v t( ,) j ← 1

j

B( '  && 

(( K K) G!   K K 6 

function v t( ,) j ← 1 while

j

),     K K#! B 5 ( 

K%0 K &&   

function v t( ,) j ← 1

j ←j + 1

while v (^) j ≤t

j

 1  W

   M  

x

           

:=

  3 58 & /@-W3" 5 #@$73#      F

    E3 8F

function02 v t( ,) j ← 1

j ← j + 1 v i if ≤t

break otherwise

for i ∈ 1 ..length v( )

j

function02 x 4( , ) = (^3) 

@ -    FDD+KK&     !

 '(%) KK&  !3 8 3 8

B  & 5      5  &

 #  E -& " 3  8 W  

   &  & #  E&

   O  

   & # !  P  

Find_Negative vec( ) stop ← 1 location ← 1

out_location ←location stop ← 0

if veclocation < 0

location ←location + 1

while stop 1

out_location

list

               

result :=Find_Negative list( )

result = 5 

!  6     &   /- E  &  , #  5     /-W &  5/-   E# & -&"B> $    !(7  /-   5  ,   #    &  #  #  &    E !& &    &        #  /-            #  /X-#  &   &  D/-#      5 "! 7 $ &     #      #  5 # -             

5 "D  $ 6 ,#4##=

M 

    &  M 

   R  

 ,  M #      &

!Y    &!&   !

   5 M 

  & M # !Y

4      5 M# 

. 5D 5     

x

P

2 3

P

Deflection Lx x

EI

   & & 5 && 7M F