Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Panel
Wiki Markup
{panel}

Author:

Rajesh

Bhaskaran,

Cornell

University

[

Problem

Specification|MATLAB Intro Problem Specification] [Step

Specification
Step 1:

Find

Reactions

R

{~}A~

A,

R{~}B~|MATLAB Intro 1 Find Reactions] [Step 2: Calculate _σ{_}{_}{~}x{~}_ for _r{_}{_}{~}i{~}_ = 1 cm|MATLAB Intro 2 Calculations] [Step 3: Plot _σ{_}{_}{~}x{~}_ vs. _r{_}{_}{~}i{~}_|MATLAB Intro 3 First plot] [Step 4: Plot _σ{_}{_}{~}x{~}_ vs. _r{_}{_}{~}i{~}_: Take 2| MATLAB Intro 4 Second plot] [Step 5: Plot _σ{_}{_}{~}x{~}_ vs. _r{_}{_}{~}i{~}_, Take 3: File Input/Output|MATLAB Intro 5 Third plot] {color:#ff0000}{*}Step 6: Plot{*}{color} {color:#ff0000}{*}{_}σ{_}{*}{color}{color:#ff0000}{*}{_}{~}x{~}{_}{*}{color} {color:#ff0000}{*}vs.*{color} {color:#ff0000}{*}{_}r{_}{*}{color}{color:#ff0000}{*}{_}{~}i{~}{_}{*}{color}{color:#ff0000}*, Take 4: Functions{*}{color} {panel} h1. Step 6: Plot _σ{_}{_}{~}x{~}_ vs. _r{_}{_}{~}i{~}_ , Take 4: Functions Let's create a function to calculate the bending stress that outputs _σ{_}{_}{~}x{~}_ given (_M_, _r{_}{_}{~}i,~_ _r{_}{_}{~}o{~}_ ). Functions are really useful to break down your code into modules and also reuse parts of your code. Let's first pick MATLAB's brain on how to create functions in MATLAB. Bring up the following page in the Help navigator: *Help > Contents > MATLAB > Programming > M-File Programming > M-File Scripts and Functions > M-File Functions.* This page has a simple example: a function called _average_ that calculates the average of the elements in a vector. Take a couple of minutes to peruse this example. MATLAB has extensive documentation on the use of functions; however, one has to poke around a bit before finding the most useful information. I personally go for the examples first. The correct syntax for creating a function is: \\ {{{color:#0000ff}function{color} return_value =

RB
Step 2: Calculate σx for ri = 1 cm
Step 3: Plot σx vs. ri
Step 4: Plot σx vs. ri: Take 2
Step 5: Plot σx vs. ri, Take 3: File Input/Output
Step 6: Plot σx vs. ri, Take 4: Functions

Step 6: Plot σx vs. ri , Take 4: Functions

Let's create a function to calculate the bending stress that outputs σx given (M, ri, ro ). Functions are really useful to break down your code into modules and also reuse parts of your code.

Let's first pick MATLAB's brain on how to create functions in MATLAB. Bring up the following page in the Help navigator:

Help > Contents > MATLAB > Programming > M-File Programming > M-File Scripts and Functions > M-File Functions.

This page has a simple example: a function called average that calculates the average of the elements in a vector. Take a couple of minutes to peruse this example. MATLAB has extensive documentation on the use of functions; however, one has to poke around a bit before finding the most useful information. I personally go for the examples first.

The correct syntax for creating a function is:

function return_value = function_name(parameter_1,

...

parameter_2,...)

...

%function description - MUST  be in a comment

code...

...

return_value

...

=

...

value

A few noteworthy points to ponder:

  1. return_value

...

  1. is

...

  1. the

...

  1. only

...

  1. data

...

  1. that

...

  1. gets

...

  1. passed

...

  1. back

...

  1. to

...

  1. the

...

  1. main

...

  1. code.

...

  1. You

...

  1. do

...

  1. not

...

  1. need

...

  1. to

...

  1. have

...

  1. a

...

  1. function

...

  1. description

...

  1. but

...

  1. it

...

  1. is

...

  1. good

...

  1. programming

...

  1. practice

...

  1. to

...

  1. add

...

  1. a

...

  1. comment

...

  1. on

...

  1. each

...

  1. function

...

  1. that

...

  1. describes

...

  1. what

...

  1. the

...

  1. function

...

  1. does.

...

  1. Also,

...

  1. if

...

  1. you

...

  1. add

...

  1. a

...

  1. description

...

  1. of

...

  1. the

...

  1. function,

...

  1. MATLAB

...

  1. will

...

  1. be

...

  1. able

...

  1. to

...

  1. index

...

  1. it

...

  1. and

...

  1. return

...

  1. a

...

  1. description

...

  1. of

...

  1. your

...

  1. function

...

  1. if

...

  1. you

...

  1. type

...

  1. help

...

  1. function_name

...

  1. in

...

  1. the

...

  1. Command

...

  1. Window.

...

  1. Once

...

  1. you

...

  1. have

...

  1. created

...

  1. a

...

  1. function

...

  1. you

...

  1. MUST

...

  1. name

...

  1. the

...

  1. .m

...

  1. file

...

  1. with

...

  1. the

...

  1. same

...

  1. name

...

  1. the

...

  1. function

...

  1. has.

...

  1. Otherwise

...

  1. MATLAB

...

  1. will

...

  1. not

...

  1. be

...

  1. able

...

  1. to

...

  1. access

...

  1. your

...

  1. function

...

  1. when

...

  1. you

...

  1. call

...

  1. it.

...


We

...

will

...

start

...

by

...

creating

...

the

...

bending

...

stress

...

function

...

that

...

outputs

...

σ

...

x

...

given

...

(

...

M

...

,

...

r

...

i,

...

r

...

o

...

).

...

Create

...

a

...

new

...

page

...

in

...

the

...

editor.

...

Type

...

in

...

the

...

following

...

statements

...

into

...

the

...

new

...

page:

...


Image Added

You can be lazy like me and copy-and-paste

...

the

...

last

...

two

...

statements

...

from

...

your

...

previous

...

code.

...

Save

...

this

...

file

...

as

...

bending_stress.m

...

,

...

which

...

is

...

the

...

name

...

that

...

MATLAB

...

automatically

...

assigns

...

the

...

file.

...

Thus,

...

the

...

function

...

name

...

and

...

the

...

.m

...

have

...

the

...

same

...

name.

...


Bring

...

up

...

beam3.m

...

in

...

the

...

MATLAB

...

editor.

...

Make

...

a

...

copy

...

of

...

beam3.m

...

using

...

Save

...

As

...

...

...

  and call the new file beam5.m

...

.

...

In

...

this

...

file

...

comment

...

out

...

the

...

lines

...

below

...

since

...

this

...

calculation

...

is

...

now

...

done

...

within

...

the

...

function.

...

Image Added 

We'll

...

replace

...

these

...

statements

...

with

...

a

...

call

...

to

...

the

...

bending_stress

...

function.

...

The

...

following

...

statement

...

does

...

this:

...

Image Added
Add this to beam5.m

...

.

...

Run

...

the

...

file

...

and

...

check

...

the output  You should get the same plot you got with beam3.m

...

.

Subfunctions

Functions can be called within a function. As crazy as this sounds, this sometimes makes very complex code appear much more manageable by sectionalizing the code. To explain how to create a subfunction, let's look to the matlab help for an example:

Image Added

In the above example, there are three functions: the main function is called "newsstats". Within this function, there are two subfunctions; one called "mean" and the other, "median". The subfunction is defined the exact same way as the main function; but the difference is the subfunction can only used when the main function is used - you can not call your subfunctions in the command window. As you can see, the main function calls both of the subfunctions, making the code much shorter and simpler. For longer, more complex code, this can save you a lot of debugging time by breaking down your code into smaller pieces.

That brings us to the end of this tour. Before we part, let's remind ourselves of some important programming guidelines that we have followed in this tour:

  • Develop code incrementally, testing obsessively at each stage. Develop a plan for how you are going to build your code before you sit at the computer.
  • Dig through the MATLAB help diligently to figure out how to use specific functions etc. Usually, the examples are the best place to start. This is a better strategy than desperately hunting for the TA every time you need help with your code.
  • Comment your program liberally.