Sie sind auf Seite 1von 16

MONASH

ENGINEERING
ENG1060

GOOD PROGRAMMING PRACTICES


Presented by Tony Vo
Slides by Tony Vo
ENG1060

HOUSEKEEPING

 News forum
– Be aware that there is a news forum for important announcements

 Help desks start this week


– 1st floor building 72 (14 Alliance Lane)
– Wednesday 2pm-3pm
– Thursday 2pm-3pm
– Take advantage of it!!

2
ENG1060

WORKSHOPS

 I think they're working…


– My impressions are you are learning at a faster rate this semester
– You want more questions…

 M-file templates for workshops


– You can now code along side me using the templates
– Found in the lecture folder

 Let me know how workshops can


be improved

3
ENG1060

RECAP: COMMENTS

 Comments are represented as green text in MATLAB

 Code tells you HOW and comments tell you WHY

 For complicated programs, comments can be just as important as the code


– Especially when working within a team
– Or when being graded by others

 You will be graded on good programming practices

4
ENG1060

RECAP: COMMENTS

 Include the following at the top of your m-files


– Your name and student ID number
– The date you created or last modified the m-file
– A short description of what the m-file does
– clear all; close all; clc; (if appropriate)

% Written by: Tony Vo, ID: 12345678


% Last modified: 01/07/2015
% Compares several terms from the sine
% Taylor series against MATLAB's sine
% function

5
ENG1060

RECAP: SECTIONING

 To create a section, use double %% symbols


– Creates and highlights blocks of code

 The ''Run'' button runs through the entire script

 The ''Run Section'' button runs through sections of a script

6
ENG1060

RECAP: RESUABLE CODE

 Computers don't get tired of repeating the same mundane tasks over and
over again

 We write code with a purpose that the same code can be used many times
for different problems
– Being generic means that the code is applicable to many different problems
– Makes you more employable as an engineer/scientist

7
ENG1060

RECAP: MORE BUILT-IN FUNCTIONS

 A = input('text'): Prints out the text string and awaits an input from the user to
store into variable A

 pause: Pauses the m-file until a key is pressed


 pause(N): Pauses for N seconds

 disp(X): Displays the value of variable X


 disp('text'): Displays the text string

 echo on / off: Prints commands for debugging purposes


8
ENG1060

EXAMPLE 1

Use the eye, ones and zeros commands to create the following matrices:
1 0 1 1 0 0
𝐴𝐴 = , 𝐵𝐵 = , 𝐶𝐶 =
0 1 1 1 0 0

Using the variables A, B and C, write a command that creates the following
matrix D
1 0 2 2 0 0
0 1 2 2 0 0
𝐷𝐷 =
0 0 2 2 2 1
0 0 2 2 1 2

9
ENG1060

EXAMPLE 2

Use MATLAB to show that the sum of the infinite series

∞ 9/10 𝑛𝑛
∑𝑛𝑛=1
𝑛𝑛
converges to log 𝑒𝑒 10 .

Do this by computing the sum for n=10, 20, 50 and 100. Plot the sums as blue
diamonds and loge(10) as a red line.

10
ENG1060

EXAMPLE 2

Use MATLAB to show that the sum of the infinite series

∞ 9/10 𝑛𝑛
∑𝑛𝑛=1
𝑛𝑛
converges to log 𝑒𝑒 10 .

11
ENG1060

EXAMPLE 3

The demand for water during a fire is often the most important factor in the
design of distribution storage tanks and pumps. For communities with
populations less than 200,000 the demand Q (in L/min) is given by
𝑄𝑄 = 3.78 × 1020 𝑃𝑃 1 − 0.01 𝑃𝑃

where P is the population in thousands.

Calculate the demand for P between 10 and 200 and plot the results.

12
ENG1060

EXAMPLE 3

The demand for water during a fire is often the most important factor in the
design of distribution storage tanks and pumps. For communities with
populations less than 200,000 the demand Q (in L/min) is given by
𝑄𝑄 = 3.78 × 1020 𝑃𝑃 1 − 0.01 𝑃𝑃

where P is the population in thousands.

On the same plot, plot a Q threshold of 4×104.

13
ENG1060

EXAMPLE 4

The "besselj" function creates Bessel functions of the first kind


– Syntax: besselj(order, number)
– E.g. x = 0:0.1:20
y = besselj(0,x)
plot(x,y)

On a single figure, plot Bessel functions for


x=0:0.1:20 and orders 0,1,2,3,4.

14
ENG1060

EXAMPLE 4

The "besselj" function creates Bessel functions of the first kind


– Syntax: besselj(order, number)
– E.g. x = 0:0.1:20
J0 = besselj(0,x)
plot(x,J0)

On a new figure, use subplots to plot each order Bessel function in separate
panels

15
ENG1060

PART A: MATLAB PROGRAMMING

1. Introduction to ENG1060
2. MATLAB basics
3. Matrices
4. Plotting
5. Good programming practices
6. Functions
7. Input and output
8. IF statements
9. Loops, loops, loops…
10. Debugging MATLAB programs
11. Advanced functions
12. Data types and MATLAB limitations
16

Das könnte Ihnen auch gefallen