Sei sulla pagina 1di 25

4/17/2012 Matlab Functions 1

Components of a Matlab Function


Create the function shown and save it in
the file Area.m
Note the components of the function:
function header which begins
includes:
keyword function
return parameter A
function name Area
parameter list (d) or list of
variables that are passed to the
function
comments
calculations
there must be a line in the
calculations that assigns a value to
the return parameter, in this case,
A
The function must be saved in a separate
file with the same name as the function, in
this case Area.m
4/17/2012 Matlab Functions 2
Command Line Access of a Function
User defined functions may be executed
directly from the command line
The comments contained in the function are
displayed with the help command, just like all
other Matlab built-in functions like cos and
exp
The function is executed or called by typing
the function name (which is the same as the
name of the function file) together with all
necessary parameters
The parameters in the parameter list can be
numeric constants or pre-defined variables
Note that there is no connection between the
variable used in the call to the function and
the variables used in the function itself.
The function executes with its own workspace
for variables used in the function, including the
parameters passed to the function.
4/17/2012 Matlab Functions 3
Using Functions Within Programs
Create the mainline program shown at
left. The mainline program is where the
execution of a program begins.
The user defined function is called like
any other Matlab built-in function
Save the program as main.m
Execute the program from the command
window
4/17/2012 Matlab Functions 4
Using Functions Within Programs
Create the mainline program shown at
left. The mainline program is where the
execution of a program begins.
The user defined function is called like
any other Matlab built-in function
Save the program as main.m
Execute the program from the command
window
4/17/2012 Matlab Functions 5
Functions With Multiple Input Parameters
Create the function shown at left
Note that this function requires two
parameters as indicated by the parameter
list in the function header line
Like the Area function, the Volume
function is saved in a file of the same
name
Note that the Area function can be called
from within the Volume function
Modify the main.m program file to call the
new Volume function
Execute the program from the command
window

4/17/2012 Matlab Functions 6
Functions With Multiple Input Parameters
Create the function shown at left
Note that this function requires two
parameters as indicated by the parameter
list in the function header line
Like the Area function, the Volume
function is saved in a file of the same
name
Note that the Area function can be called
from within the Volume function
Modify the main.m program file to call the
new Volume function
Execute the program from the command
window

4/17/2012 Matlab Functions 7
Functions With Multiple Input Parameters
Create the function shown at left
Note that this function requires two
parameters as indicated by the parameter
list in the function header line
Like the Area function, the Volume
function is saved in a file of the same
name
Note that the Area function can be called
from within the Volume function
Modify the main.m program file to call the
new Volume function
Execute the program from the command
window

4/17/2012 Matlab Functions 8
Functions With Multiple Outputs
Create the function shown at left
Note that this function returns two values,
volume V and surface area S
The multiple values are combined into a
single array which is the actual return
value
Modify the mainline program to call the
new Cylinder function
Execute the program from the command
window
4/17/2012 Matlab Functions 9
Functions With Multiple Outputs
Create the function shown at left
Note that this function returns two values,
volume V and surface area S
The multiple values are combined into a
single array which is the actual return
value
Modify the mainline program to call the
new Cylinder function
Execute the program from the command
window
4/17/2012 Matlab Functions 10
Functions With Multiple Outputs
Create the function shown at left
Note that this function returns two values,
volume V and surface area S
The multiple values are combined into a
single array which is the actual return
value
Modify the mainline program to call the
new Cylinder function
Execute the program from the command
window
4/17/2012 Matlab Functions 11
Beam Bending Moment
The beam shown is simply supported at
its ends
The load F is applied at a distance x from
the left side
The maximum bending moment in the
beam occurs at the point of application of
the force
The magnitude of the support forces at
the ends and the maximum bending
moment are influenced by the point
application of the beam load
We wish to create a program to calculate
the support forces and plot the variation of
maximum bending for various locations of
the force application point.
4/17/2012 Matlab Functions 12
Beam Bending Moment
Sum of the forces is zero:



Sum of the moments about left end is zero:



The two equations are combined in matrix
format and solved for the unknown support
forces:







With the support forces known, the maximum
bending moment is calculated from:
F x f L M
R z
= =

+ = = F f f F
R L y
0
)
`

=
)
`

=
= +
xF
F
f
f
L
F x f L
F f f
R
L
R
R L
0
1 1
L
f x M =
4/17/2012 Matlab Functions 13
mainline routine - beam.m
Create a new folder beam for the files in
this project
Create the mainline routine in beam.m
as shown
Plan the program by dividing it into logical
steps and laying out each step in a
separate function.
BeamParams get user input for the
beam length, applied force, and number
of beam divisions.
Calculations solve the equilibrium
equations for the support forces and
maximum bending moment for each load
application point
PlotMoment plot the beam bending
moment variation with load application
point
Each function will be developed and
tested individually
The perfect performance of each function
must be verified before work on the next
function begins
4/17/2012 Matlab Functions 14
BeamParams
Enter the function BeamParams as shown

Modify the mainline routine in beam.m to
verify that the user input is successfully
achieved

Execute beam.m from the command
window to verify the operation of
BeamParams
4/17/2012 Matlab Functions 15
BeamParams
Enter the function BeamParams as shown

Modify the mainline routine in beam.m to
verify that the user input is successfully
achieved

Execute beam.m from the command
window to verify the operation of
BeamParams
4/17/2012 Matlab Functions 16
BeamParams
Enter the function BeamParams as shown

Modify the mainline routine in beam.m to
verify that the user input is successfully
achieved

Execute beam.m from the command
window to verify the operation of
BeamParams
4/17/2012 Matlab Functions 17
Calculations
Enter the function Calculations as shown
Note that the calculations are repeated for
n+1 loading point locations
The equilibrium equations are entered in
matrix format with A holding the
coefficient matrix, s as the vector of
unknowns, R as the right-hand side vector
The equations are solved with the Matlab
backslash operator \ which is equivalent
to multiplying R by the inverse of A to
determine s.
Modify the mainline routine to test
Calculations.m
Execute the mainline routine and verify
the results shown.
4/17/2012 Matlab Functions 18
Calculations
Enter the function Calculations as shown
Note that the calculations are repeated for
n+1 loading point locations
The equilibrium equations are entered in
matrix format with A holding the
coefficient matrix, s as the vector of
unknowns, R as the right-hand side vector
The equations are solved with the Matlab
backslash operator \ which is equivalent
to multiplying R by the inverse of A to
determine s.
Modify the mainline routine to test
Calculations.m
Execute the mainline routine and verify
the results shown.
4/17/2012 Matlab Functions 19
Calculations
Enter the function Calculations as shown
Note that the calculations are repeated for
n+1 loading point locations
The equilibrium equations are entered in
matrix format with A holding the
coefficient matrix, s as the vector of
unknowns, R as the right-hand side vector
The equations are solved with the Matlab
backslash operator \ which is equivalent
to multiplying R by the inverse of A to
determine s.
Modify the mainline routine to test
Calculations.m
Execute the mainline routine and verify
the results shown.
4/17/2012 Matlab Functions 20
Plot Moment
Enter the function PlotMoment as shown.
Modify the mainline routine to test
PlotMoment
Execute the mainline routine from the
command window
Verify the results shown
4/17/2012 Matlab Functions 21
Plot Moment
Enter the function PlotMoment as shown.
Modify the mainline routine to test
PlotMoment
Execute the mainline routine from the
command window
Verify the results shown
4/17/2012 Matlab Functions 22
Plot Moment
Enter the function PlotMoment as shown.
Modify the mainline routine to test
PlotMoment
Execute the mainline routine from the
command window
Verify the results shown
4/17/2012 Matlab Functions 23
Plot Moment
Enter the function PlotMoment as shown.
Modify the mainline routine to test
PlotMoment
Execute the mainline routine from the
command window
Verify the results shown
4/17/2012 Matlab Functions 24
TabResults
Create a new function TabResults that
can be called from beam.m as shown
Verify the results shown
4/17/2012 Matlab Functions 25
TabResults
Create a new function TabResults that
can be called from beam.m as shown
Verify the results shown

Potrebbero piacerti anche