You are on page 1of 43

Introduction to MATLAB

for Mechanical Engineers

Cankaya University
Faculty of Engineering
Mechanical Engineering Department

ME 102 Mechanical Engineering Orientation


Class Notes
by
Prof.Dr.Nevzat Onur
1-2
Entering Commands and Expressions

• MATLAB retains your previous keystrokes.


• Use the up-arrow key to scroll back through the
commands.
• Press the key once to see the previous entry, and
so on.
• Use the down-arrow key to scroll forward.
• Edit a line using the left- and right-arrow keys
the Backspace key, and the Delete key.
• Use the down-arrow key to scroll forward
• Press the Enter key to execute the command.
Scalar arithmetic operations
1-6
1-7
Order of precedence
% designates comment .Matlab does not execute it

1-9
 keskin

 Keskin

 KESKIN

 Keskin_1

 Keskin_2
>> r=8;
>> h=15;
>> V=(pi*r^2*h)/3

V=

1.0053e+03
Numeric
display
formats.
Some commonly used mathematical functions
1-20
abs(z) The absolute value of
z=x+iy
angle(z) the angle of z. This is exa
calculated by atan2(y,x)

Example
Given z=3-5*i
>> real(z)
ans =
3
>> imag(z)
ans =
-5
>> abs(z)
ans =
5.8310
>> angle(z)
ans =
-1.0304
21
25
>> a=30;
>> b=20;
>> A=sqrt(b^2+16*a^2)/2;
>> B=(b^2/( 8*a ))*log((4*a+sqrt(b^2+16*a^2))/b);
>> s=A+B
>>s =
64.9806
COMMENTS
% -the comment symbol
The comment symbol may be put anywhere in the
line. MATLAB ignores everything to the right of the
% symbol. For example,

>>% This is a comment.


>>x = 2+3 % So is this.
x =
5

Note that the portion of the line before the % sign is


executed to compute x.
Some Input/output commands
disp can be used to display the value of a variable.

>> x=3.0;
>> y=3^x;
>> disp(y)
27
Example

>> disp(' And now for something completely different' )

And now for something completely different

Example
>> disp('----------------------------------')
----------------------------------
29
You can perform operations in MATLAB in two ways:

1. In the interactive mode: all commands are entered


directly in the Command window,

2. In the script file mode: By running a MATLAB program


stored in script file. This type of file contains MATLAB
commands, so running it is equivalent to typing all the
commands—one at a time—at the Command window
prompt. You can run the file by typing its name at the
Command window prompt.

30
Example:
Write a script file for the following
problem and save it as T5
>> T5
Part a

Lp =

92.0412
Part b

m=

7.9057

>> sphere
Enter radius=5
Surface area of sphere
314.1593
Keep in mind when using script files:

1. The name of a script file must begin with a letter,


and may include digits and the underscore
character, up to 63 characters.
2. Do not give a script file the same name as a
variable.
3. Do not give a script file the same name as a
MATLAB command or function. You can check to
see if a command, function or file name already
exists by using the exist command.

39
40
diary
Diary command saves your input to MATLAB and most
of the output to disk. This command toggles diary on and
off. (If no file is given, it is saved to the file diary in the
current directory.)
diary on turns the diary on.
diary off turns the diary off.

41
1)

Write script file.


2)

Write a script file for this problem


Write a script file

You might also like