introduction to matlab for mechanical engineersme313.cankaya.edu.tr/uploads/files/lecture 1...
TRANSCRIPT
![Page 1: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/1.jpg)
ME 102 Mechanical Engineering Orientation
Class Notes
by
Prof.Dr.Nevzat Onur
Introduction to MATLAB
for Mechanical Engineers
Cankaya University
Faculty of Engineering
Mechanical Engineering Department
![Page 2: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/2.jpg)
1-2
![Page 3: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/3.jpg)
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.
![Page 4: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/4.jpg)
Scalar arithmetic operations
![Page 5: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/5.jpg)
![Page 6: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/6.jpg)
1-6
![Page 7: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/7.jpg)
1-7
![Page 8: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/8.jpg)
Order of precedence
![Page 9: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/9.jpg)
1-9
% designates comment .Matlab does not execute it
![Page 10: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/10.jpg)
![Page 11: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/11.jpg)
keskin
Keskin
KESKIN
Keskin_1
Keskin_2
![Page 12: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/12.jpg)
![Page 13: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/13.jpg)
![Page 14: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/14.jpg)
![Page 15: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/15.jpg)
![Page 16: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/16.jpg)
![Page 17: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/17.jpg)
>> r=8;
>> h=15;
>> V=(pi*r^2*h)/3
V =
1.0053e+03
![Page 18: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/18.jpg)
Numeric
display
formats.
![Page 19: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/19.jpg)
Some commonly used mathematical functions
![Page 20: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/20.jpg)
1-20
![Page 21: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/21.jpg)
21
exa
abs(z) The absolute value of z=x+iy angle(z) the angle of z. This is 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
![Page 22: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/22.jpg)
![Page 23: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/23.jpg)
![Page 24: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/24.jpg)
![Page 25: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/25.jpg)
25
![Page 26: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/26.jpg)
>> 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
![Page 27: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/27.jpg)
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.
![Page 28: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/28.jpg)
Some Input/output commands
![Page 29: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/29.jpg)
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
![Page 30: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/30.jpg)
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
![Page 31: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/31.jpg)
![Page 32: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/32.jpg)
![Page 33: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/33.jpg)
Example:
Write a script file for the following
problem and save it as T5
![Page 34: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/34.jpg)
![Page 35: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/35.jpg)
>> T5
Part a
Lp =
92.0412
Part b
m =
7.9057
![Page 36: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/36.jpg)
![Page 37: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/37.jpg)
![Page 38: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/38.jpg)
>> sphere
Enter radius=5
Surface area of sphere
314.1593
![Page 39: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/39.jpg)
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
![Page 40: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/40.jpg)
40
![Page 41: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/41.jpg)
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
![Page 42: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/42.jpg)
1)
Write script file.
2)
Write a script file for this problem
![Page 43: Introduction to MATLAB for Mechanical Engineersme313.cankaya.edu.tr/uploads/files/LECTURE 1 Introduction.pdf · ME 102 Mechanical Engineering Orientation Class Notes by Prof.Dr.Nevzat](https://reader031.vdocuments.us/reader031/viewer/2022013120/5af35a317f8b9abc789102a4/html5/thumbnails/43.jpg)
Write a script file