Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Engineering Programming

Introduction
Ahmad Syihan Auzani, PhD

Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 1


Module description
• Understand basic knowledge of computing and programming techniques,
• Be able to create simple computing and programming techniques,
• Be able to solve engineering problems with engineering programming.

Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 2


Contents
• Install MATLAB / Octave/ SciLab / Python
• Basic programming knowledge
• Algorithm
• Exercises
• Assignment 1

Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 3


References
1. Linge, Svein, and Hans Petter Langtangen.
Programming for computations-MATLAB/Octave.
Springer Nature, 2016.
2. Linge, Svein, and Hans Petter Langtangen.
Programming for Computations-Python: A Gentle
Introduction to Numerical Simulations with
Python 3.6. Springer Nature, 2020.
3. YouTube,
4. and other online tutorials

Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 4


How do computers work? Circuits and logic

This Photo by Unknown Author is licensed under CC BY-SA-NC

Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 5


Binary and data

This Photo by Unknown Author is licensed under CC BY-SA

This Photo by Unknown Author is licensed under CC BY-SA


This Photo by Unknown Author is licensed under CC BY

Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 6


Programming language
• Computer only understands binary, programming language converts binary to
understandable code.
• Low-level programming code: Closer to binary. e.g. machine code
• High-level: further from binary. e.g. java, python, MATLAB, C++, etc.
• Why do we learn programming?
1. To solve non-standards problems
2. We know our programs better that the ready-made software
3. Open source / free
4. Mechanical engineers use numerical method to solve problems
• Numerical method: Mathematical tool aimed to solve numerical problems

Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 7


Some programming languages

MATLAB Octave Scilab Python


License price $$$$ Free Free Free
interactive UI yes yes no no
Installer size >5 GB < 400MB <200 MB <200 MB
file extension myfile.m myfile.m myfile.sce myfile.py
Slightly Slightly
different from different from
MATLAB MATLAB

Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 8


Some programming languages

This Photo by Unknown Author is licensed under CC BY-NC-ND

This Photo by Unknown Author is licensed under CC BY-


SA
This Photo by Unknown Author is licensed under CC BY-SA

Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 9


Algorithm
• A method / procedure / process to solve a problem
• Expressed in a flowchart
• Consist of
• Start/end
• process
• input/output
• decision

• Flowchart algorithm is
mandatory in exam

Ahmad This
Syihan Auzani - Eng Programming - DTM FTUI 2021
Photo by Unknown Author is licensed under CC BY-SA
10
Your first program
v0 = 5;
import matplotlib.pyplot as plt
g = 9.81;
import numpy as np
t = linspace(0, 1, 1001);
g=9.8
y = v0*t - 0.5*g*t.^2;
v0=5
plot(t, y);
t = np.linspace(0,1,1001)
xlabel("t (s) ");
y = v0*t-0.5*g*t**2
ylabel(’y (m) ");
plt.plot(t,y)
plt.show()

Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 11


Basic concept
• Interactive computing (command window / shell)
• Similar arithmetic to Excel
• Variables
• Real numbers → floating point numbers by default / if nothing specified
• integers → effective for whole number. e.g. x=int8(1.9) = 2, for 8 bit
• Text and numbers
• real=12.896, integer=42, string=“some message”, real=1.290e+01

Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 12


Basic concept
• Text and numbers
t0 = 2;
dt = 0.55;
% Unformatted print
t = t0 + 0*dt; g = t*sin(t);
fprintf("%g %g\n", t, g);
t = t0 + 1*dt; g = t*sin(t);
fprintf("%g %g\n", t, g);
t = t0 + 2*dt; g = t*sin(t);
fprintf("halo %g %g\n", t, g);

Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 13


Basic concept
• Array: collection of numbers; and plotting
h = zeros(4, 1);
h(1) = 1.60; h(2) = 1.85; h(3)= 1.75; h(4) = 1.80;
H = zeros(4, 1);
H(1) = 0.50; H(2) = 0.70; H(3)= 1.90; H(4) = 1.75;
family_member_no = zeros(4, 1);
family_member_no(1) = 0; family_member_no(2) = 1;
family_member_no(3) = 2; family_member_no(4) = 3;
plot(family_member_no, h, family_member_no, H);
xlabel("Family member number");
ylabel("Height (m)")
Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 14
Basic concept
• Bug = error / mistake
• Debugging = fixing error
• Input data
age = input("What is your age? ")
fprintf("Ok, so you are half way to %d, wow!\n", age*2)
• Exercise:
1. Volume of a cube, sphere, cylinder, etc
2. Area of a cube, sphere, cylinder, etc
3. Average of numbers

Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 15


Assignment 1
• Create an interactive program that can calculate several input (array) of parameter (x)
to a function (y). This can be temperature unit converter, or any other physical
formula.
• Generate an x-y plot based on the calculation above
• Provide the algorithm of the program
• Be creative, make it as unique as possible
• Every student must design a unique program. (cheating → score 0)
• All programs will be demonstrated next week

Enjoy and have fun!

Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 16

You might also like