Sie sind auf Seite 1von 18

F.E.A.

Lab Session – 2
Getting Started
(MATLAB basics)
MATLAB
2007-08 Deaprtment of Mechanical Engg UET - Lahore 2
MATLAB 7.0 (R-14)

• System requirements
– IntelR Pentium- 4 CPU
– 512 MB memory (1.0 GB recommended)
– 64MB VGA buffer (128 MB recommended)
– 1024x768 / 256 - color 17” Display
– Minimum 2.0 GB HDD installation space
– Win XP Pro SP - 2 / Win 2k SP - 3 / Win Vista

2007-08 Deaprtment of Mechanical Engg UET - Lahore 3


MATLAB Environment

2007-08 Deaprtment of Mechanical Engg UET - Lahore 4


MATLAB System
• Development Environment
– Command Window
– Command History
– Editor / Debugger
– Browser
• Mathematical Function Library
• MATLAB Language – a high level matrix/array
language; supports control flow and functions
better than C language
• Graphics – full support for displaying
matrices/vectors as graphs, 2D and 3D data
visualization

2007-08 Deaprtment of Mechanical Engg UET - Lahore 5


EXAMPLE:

Typing at the command prompt in the MATLAB


command window:
>>logo

2007-08 Deaprtment of Mechanical Engg UET - Lahore 6


Running the MATLAB commands
• Command mode
Directly entering commands at the command
prompt (>>)
For example:
>>5 * 2.5
• Batch mode
Writing set of MATLAB commands (a batch of
commands) as function/script m-file and then
calling the function
>>myfunction
2007-08 Deaprtment of Mechanical Engg UET - Lahore 7
Command Line Editing

• Any typing error can be corrected before the [Enter] key is pressed.
Use → or ← keys to make changes / correct
spelling mistakes etc.
When the line is correct, just press [Enter] to send the command to
MATLAB.

• Previous commands can be corrected and reused to save typing.


Use ↑or ↓ arrow - keys to browse through
previously entered commands
Make the necessary corrections and press [Enter] to run the corrected
command.

2007-08 Deaprtment of Mechanical Engg UET - Lahore 8


SUPPRESSING OUTPUT

>>pi
3.142857143

>>pi;
Use semicolon (;) to suppress immediate
display of the result.

2007-08 Deaprtment of Mechanical Engg UET - Lahore 9


NUMBERS & FORMATS
All computations in MATLAB are done in double
precision which means about 15 significant figures.
>>format long
0.333333333333333
>>format or format short
0.33333
>>format long e
3.333333333333333e-001
>>format short e
3.3333e-001
>>format bank
0.33
>>format loose
>>format compact
2007-08 Deaprtment of Mechanical Engg UET - Lahore 10
MATLAB help

The easiest way to find out more about specific


MATLAB commands. For example:

>>help logo

* This will display enough information about log command.

2007-08 Deaprtment of Mechanical Engg UET - Lahore 11


Simple Calculations in MATLAB
>> 2 + 3 / 4 * 5
ans =
5.7500

MATLAB works according to the priorities:

1. quantities in brackets
2. powers 2 + 3^2 = 2 + 9 = 11
3. * /, working left to right (3*4/5=12/5)
4. + -, working left to right (3+4-5=7-5)

2007-08 Deaprtment of Mechanical Engg UET - Lahore 12


VARIABLES
THE “ans” VARIBALE:

>> 3 – 2 ^ 4
ans =
- 13
>> ans * 5
ans =
- 65

2007-08 Deaprtment of Mechanical Engg UET - Lahore 13


USER-DEFINED VARIABLES:
>> x = 3 – 2 ^ 4
x=
- 13
>> y = x * 5
y=
- 65

NOTE: These are examples of assignment statements: values are assigned to


variables. Each variable must be assigned a value before it may be used on the right
of an assignment statement.

2007-08 Deaprtment of Mechanical Engg UET - Lahore 14


NAMING THE VARIBALES

• Any combination of letters and digits


• Must be starting with a letter
• Case-sensitive

These are allowable:


NetCost, Left2Pay, x3, X3, z25c5
These are not allowable:
Net-Cost, 2pay, %x, @sign

2007-08 Deaprtment of Mechanical Engg UET - Lahore 15


• MATLAB Comments
>>p = 2+3 ; % The semicolons suppress
>>q = 3+5 ; % unwanted output.
>>ratio = p/q
• Placing a number of commands on one line
[separated by a comma (,) or a semicolon (;). (Use a semicolon, unless you want the
answer displayed.)]

>>p = 2+3 ; q2 = x + 4 , ratio2 = p/q2


• Parentheses can be used to make expressions
clearer.
>>ratio = (2+3)/(x+4)

2007-08 Deaprtment of Mechanical Engg UET - Lahore 16


KEEPING A RECORD
>>diary mysession saves editable text copy
>>diary off terminates the command
>>save thissession saves to file; cannot edit
>>load thissession resumes work from saved
file
>>who
>>clear
>>clear variable_name
>>clc
LEAVING MATLAB
>> exit
or
>>quit

2007-08 Deaprtment of Mechanical Engg UET - Lahore 17


• MATLAB Exercises

2007-08 Deaprtment of Mechanical Engg UET - Lahore 18

Das könnte Ihnen auch gefallen