Sei sulla pagina 1di 5

MAE412/ 512 Machines & Mechanisms II

Recitation Materials #2
Date: 9/5/2006

1 MATLAB Review
In this recitation, we will give you some idea in programming with MATLAB. Beside,
we will set the standard in naming files, and how a program should be written for this
class.

1.1 File Naming Scheme


The two common file names that we will most likely encounter in this class is the .m file
(or the m-file) and possibly the .fig file (for GUI, and figure file). To name this file,
please follow the following scheme:

MAE#12_HW#_Problem#_FirstnameLastname.m

Explain as follow:
ƒ MAE#12 –MAE512 if you are graduate student, MAE412 if you are
undergraduate student.
ƒ HW# - Homework #, write in 00, 01, 02 format.
ƒ Problem# - The problem number, with sub-number sometime, e.g. 1a, 1b, etc.
ƒ FirstnameLastname – Student first name and last name, no space between them,
and capitalize the initials.

Example: MAE512_HW01_Problem1c_LengFengLee.m

1.2 The ‘heading’ of your m-file


When writing a program, not only you write the program to solve the given problem, but
you also need to document your code. You have to let the people who read your code
understand what your code can do, or what other things is needed for your code to work.
Beside, you probably want to remind yourself what your code doing after revisiting your
own code at a later stage! So, properly document you code is very important. Here are
some basic guideline (minimum requirements) used in this course. A “good program” is
not only one that can perform fancy calculation, but also one with good documentation.

An example might worth a thousand words… so let see an example.

Prepared by: Leng-Feng Lee 1


Failed to follow such documentation will result in poor grade….

Note: This way of documentation is based on personal experiences, if you done are a
computer science major and have better idea of doing this. Please let me know as well.

1.3 Documenting the code


While it is not possible to comment every lines of your code, you should comment it
whenever necessary. E.g. when there is a variable that need to change to show different
solution, you should document it; when there is an equation/algorithm use that you need
to explain, etc.

Again, an example is shown:

From the way you document your code, it can help you organizing the algorithm to solve
the problem and also your own thoughts in writing a ‘structure’ program.

Prepared by: Leng-Feng Lee 2


1.4 What is MATLAB
From mathworks.com: “MATLAB is a high-level technical computing language and
interactive environment for algorithm development, data visualization, data analysis, and
numeric computation. Using MATLAB, you can solve technical computing problems
faster than with traditional programming languages, such as C, C++, and Fortran.”

It expanded so much and so wide spread in many fields, that as an engineer/ researcher/
graduate student, you should know it!

1.4.1 How to get started?


The easiest way to learn a MATLAB is by writing code (though it can do more than just
writing code…). See sample codes – there are a lot of them in the web.

Use of “help” command in the command prompt – though you might not fully understand
its descriptions sometime when you are not familiar with MATLAB, you need to try to
understand them. As more you do this, you will eventually get used to it and the “help”
command is really helping you to understand the use of new functions.

This course will only shows you a fraction of what MATLAB can do… but will help you
in your future research/ courses/ job/ etc. We also assume you to have basic programming
knowledge, such as writing structured algorithm, know the use of for-loop, if-else, and
the how data were stored: vector/ matrix.

There is no way we can show you all the use in MATLAB, but here we try to show you
some basic thing you can do with MATLAB. Always remember, there are different ways
to code up a program, and MATLAB have many ‘smart-way’ or ‘short-way’ that a
multiple line command can be replaced by a single line command and gives the same
result – as you get to learn more and more about it, you will learn them.

1.5 One link example


We will use the problem 1(a) given in homework #00 as an example to demonstrate some
basic MATLAB code.

Prepared by: Leng-Feng Lee 3


1.6 Animation
We will use problem 1(b) given in homework #00 as the next example for doing
animation. There are two ways to create an animation, here we shown the simplest way
(Another way of plotting animation is more efficient in terms of CPU processing time –
Interested student should see the MATLAB ‘Graphics’ documentation).

Prepared by: Leng-Feng Lee 4


1.7 Create Movie
Once you have your animation working as what you wanted, creating an avi movie out of
it is just the matter of adding four more instructions.

Note that there are many ‘options’ that you can set for creating an avi movie, and there
are many features that you can control… these will not be shown here so that you can
explore it yourself and actually learn it.

Note:
1. This document only serve as a supplement to the class-notes and textbooks,
the author is not responsible for any mistake that may occur in this document.
2. This document is created for your convenient, and is NOT a substitute for you
to absence from recitation hour. If student abuse its use, there will be no more
material provided in the future.

Prepared by: Leng-Feng Lee 5

Potrebbero piacerti anche