Presentation is loading. Please wait.

Presentation is loading. Please wait.

BAHASA PEMROGRAMAN MATLAB as an Engineering Tool & Programming Language a lecture note for Civil Engineering students of PETRA Christian University Doddy.

Similar presentations


Presentation on theme: "BAHASA PEMROGRAMAN MATLAB as an Engineering Tool & Programming Language a lecture note for Civil Engineering students of PETRA Christian University Doddy."— Presentation transcript:

1 BAHASA PEMROGRAMAN MATLAB as an Engineering Tool & Programming Language a lecture note for Civil Engineering students of PETRA Christian University Doddy Prayogo, Ph.D.

2 Course Outline (cont’d)
Tutorial 4 (2 weeks) Introduction to programming M-File scripts M-File functions Exercises Reading input from a file Writing output to a file

3 Tutorial 4 – M-file scripts
Series of Matlab command can be saved and automatically executed by using M-file scripts. You can start create a script by accessing “File  New  Script” menu, or simply click on “New Script” button. You will open an Editor Window.

4 Tutorial 4 – M-file scripts
Try to type these commands in Editor Window, then save it with a certain name, for example “run”. Matlab will create an m-file “run.m” in the working directory.

5 Tutorial 4 – M-file scripts
Now, back to Command window, and call the script M-file. Type run  enter.

6 Tutorial 4 – M-file scripts
After inputting necessary data, you should see the typical executed script M-file below.

7 Tutorial 4 – M-file scripts
Fprintf is a command to display, originally from C-language. Some of the format can be seen below. ‘\n’ is a special character to enter a new row in C-language.

8 Tutorial 4 – M-file scripts
All variables executed by script M-file will be recorded in the workspace. Sometimes, this is not desirable.

9 Tutorial 4 – M-file function
You can make similar series of commands, and create your own function. Open another Editor window, and type the followings :  then save with the name ‘luas’

10 Tutorial 4 – M-file function
Through command window try the followings :

11 Tutorial 4 – M-file Difference between Script & Function M-File:

12 Tutorial 4 – Exercises Write a script m-file:
Input : Length of simple supported beam, Location of downward concentrated load from left support, magnitude of the load. Output : reactions on both supports, maximum bending moment, sketch of moment diagram. Write a function m-file : Input : a, b, c coefficients of 2nd degree polynomial (ax2+bx+c=0). Output : both roots of the polynomial.

13 Tutorial 4 – Read/Write from/to a text file
To execute process repeatedly on many data, it is un-effective if you run script m-file that many times. Instead, data can be prepared as an input text file. Then we can write a script to read the data, process them, and write an output to another file. For example, we want to calculate area and 2nd moment of inertia of 10 rectangular sections. First prepare an input text file (use notepad or matlab editor) and save it (data.txt) in the working directory.

14 Tutorial 4 – Read/Write from/to a text file
Type this script  save “iotext.m”

15 Tutorial 4 – Read/Write from/to a text file
Execute “iotext.m” from Command window. Enter ‘data.txt’ as input text file, and Enter ‘output.txt’ as output text file. Then open the output >>open output.txt (+enter)

16 Tutorial 4 – Read/Write from/to a text file
The output.txt file should be like below :

17 Tutorial 4 – Exercise Make a script file which read and write from and to text-files. Input text file contains 5 sets of data : Length of simple supported beam (4, 5, 6, 7, 8) Location of downward concentrated load from left (2, 1, 2, 4, 7) Magnitude of concentrated load (1, 2, 3, 4, 5) Out text file contains 5 sets corresponding results: Left support reaction Right support reaction Maximum moment


Download ppt "BAHASA PEMROGRAMAN MATLAB as an Engineering Tool & Programming Language a lecture note for Civil Engineering students of PETRA Christian University Doddy."

Similar presentations


Ads by Google