You are on page 1of 33

Beginning Programming

for Engineers
CSCI-1190
Lecture 1

Minoo Aminian
Rensselaer Polytechnic Institute
Read the complete Syllabus from the
class website:
http://rpilms.rpi.edu

10/18/2010 Programming For Engineers-CSCI 1190 2


Program
• To solve a problem by writing a program you
should:
1. Understand the problem
2. Develop instructions/algorithm to solve the
problem.
3. Express the algorithm in a way that the computer
can execute it.
• This is the program.
4. Test your program.

10/18/2010 Programming For Engineers-CSCI 1190 3


Algorithm
• Procedure to solve a problem, e.g.:
Rise –and – shine algorithm
Get out of bed
Take off pajamas
Take a shower
Get dressed
Eat breakfast
Carpool to work

• Algorithms can be written either by pseudocode or by


flowcharts.
• Pseudocode:
informal language to develop a program
10/18/2010 Programming For Engineers-CSCI 1190 4
Flowchart
Graphical representation of program

Simple flowchart

10/18/2010 Programming For Engineers-CSCI 1190 5


Computer Organization

• Basic virtual logical units


• Von Neumann Model
Memory: low capacity warehouse

Input: receiving Output: shipping


section section

Control Unit: Processing Unit:


administrative manufacturing
section section

10/18/2010 Programming For Engineers-CSCI 1190 6


High-level Computer Languages

• For a computer to do something, you have to


feed a particular list of instructions

• Machine language: language that computer


understands

• High-level programming languages, like


Matlab, C
– Simplify programming
– Easier to understand
10/18/2010 Programming For Engineers-CSCI 1190 7
Compilers
• Source code: what programmer writes in high-
level language
• Object code: translation of the source code to
machine language by compiler
• Interpreter: a computer program that reads the
source code and executes that.
Source code Object code
Compiler

10/18/2010 Programming For Engineers-CSCI 1190 8


Writing a Program

• Understand the problem.

• Design an algorithm.
Debug
• Develop source code from algorithm.

• Translate and run source code.

• Verify that program works correctly.

10/18/2010 Programming For Engineers-CSCI 1190 9


Origins of Matlab

• Matlab was created around


1980 to allow students to
work with matrix software
without learning Fortran,
etc.

• Mathworks, Inc. has further


developed Matlab.
Cleve Moler
• Now widely used for
engineering and science

10/18/2010 Programming For Engineers-CSCI 1190 10


Using Matlab

• Matlab has features like:


o Command line and history
o Workspace window
o Built-in editor
o Built-in debugger
o Help!
• Matlab needs access to a
license server.

10/18/2010 Programming For Engineers-CSCI 1190 11


Matlab as a Calculator

10/18/2010 Programming For Engineers-CSCI 1190 12


Matlab and Trig. Functions
Try these computations:

sin(90)
sin(pi/2)
cos(pi/4)
sin(pi/4)
tan(pi/4)

sind(90)
cosd(90)
tand(90)

What units do these functions


expect?
10/18/2010 Programming For Engineers-CSCI 1190 13
Variables and Memory Objects
Type in these expressions:

ang = pi/4
c = cos(ang)
s = sin(ang)
c^2 + s^2

• Notice how variables


are assigned values
and used.

• Watch the
"memory objects" in
the Workspace window.

10/18/2010 Programming For Engineers-CSCI 1190 14


Clearing memory and the command
window

• You can access variable values through the workspace


window, or by typing in the name:
c
ang

• Observe the workspace window, after issuing:

clear

• You can clear the command window:

clc

10/18/2010 Programming For Engineers-CSCI 1190 15


Suppressing Output
Try these expressions:

ang = pi/4;
c = cos(ang);
s = sin(ang);
c^2 + s^2;
ans

What seems to be the function of


ending a line with a semicolon?

10/18/2010 Programming For Engineers-CSCI 1190 16


Simple Operations on Vectors
Try these expressions:

a = [1 2 3]
a+a
5*a
a*a
a .* a
a'
a * a'
a-3
a/2
2\a

Last expression is "left division".


10/18/2010 Programming For Engineers-CSCI 1190 17
Simple Operations on Vectors (2)

Try these expressions:

b = [ 2 ; 3 ; 4]

a*b

b = [ 5

7]

10/18/2010 Programming For Engineers-CSCI 1190 18


Using subscripts

Try these expressions: We can assign into


subscripted elements of
a = [ 10 20 30 40 50] vectors. The vector will
a(1)
grow as needed.
a(4)
a(12/4)
a(9) Try these:

The expression in parenthesis z(4) = 42


z(2) = 9
is a subscript or index. z(5) = 88

10/18/2010 Programming For Engineers-CSCI 1190 19


Entering Matrices

Try these expressions:

a = [ 1 2 3
4 5 6 ]

b = [10 20 ; 30 40; 50 60]


a*b
10/18/2010 Programming For Engineers-CSCI 1190 20
Subscripts and Matrices

Try these commands:


a = [ 1 2 ; 3 4]
a(1,2) = 9
a(3,2) = 8

• Normally, use (row, column)

• Single subscript counts down column, then proceeds


to next column... a(5) = 20

10/18/2010 Programming For Engineers-CSCI 1190 21


Simple Ranges

Try these expressions:

1:10
0:10:50
50:-5:15

10/18/2010 Programming For Engineers-CSCI 1190 22


Simple Functions on Vectors
Try these expressions:

theta = 0:10:180;
thetarad = theta*(pi/180)
c = cos(thetarad);
s = sin(thetarad);
theta(4)
c(4)^2 + s(4)^2

10/18/2010 Programming For Engineers-CSCI 1190 23


The plot command
Using the results in c, s from the last slide,
try this command: plot(c,s);

10/18/2010 Programming For Engineers-CSCI 1190 24


Writing Matlab scripts
• Sequences of commands can be saved to a
script or “m-file”.

• Comments start with % or %% symbols.

• Long lines can be continued using …

10/18/2010 Programming For Engineers-CSCI 1190 25


A simple script: space_craft.m
% This program computes v0 the initial velocity of a space
%craft traveling under constant acceleration from the
%equation: V0 = √2gs , with s being the distance travelled
%and g (the force of gravity).
%
% Programmed by Minoo Aminian
%% Clear memory, etc.
clear
clc
%% Computations
g = 9.81;
h2 = 100; % km – given
h1 = 7500; % m - given
s = (h2*1000) – h1; % m
v0 = (2*g*s)^0.5; % the answer

10/18/2010 Programming For Engineers-CSCI 1190 26


Running a script

• To run a script, just type its name.

• Alternatively, use the "run" button on the editor window.

10/18/2010 Programming For Engineers-CSCI 1190 27


Scripted input and output
• Use input to get values.
• Use disp to print messages.
% space_craft2.m: Get h1, h2 and calculate v0.
%
% Programmed by Minoo Aminian

clear
clc

%% Get the values


h1 = input('Enter h1:');
h2 = input('Enter h2:');

%% Do the calculation
s = (h2*1000) – h1; % m
v0 = (2*g*s)^0.5; % the answer
disp('The initial velocity has been calculated. It
is:');
disp(v0);
10/18/2010 Programming For Engineers-CSCI 1190 28
Simple functions

function CircleArea(r)
% Function CircleArea calculates the area of a
% circle.
%
% Input: r - Radius
% Output: A - Area
% Programmed by Minoo Aminian

%% Calculate
A = pi*r^2;

%% Show result
disp('Calculated Area is');
disp(A);

end
10/18/2010 Programming For Engineers-CSCI 1190 29
Writing a function

• The function must go into an m-file with the same name as the
function you are defining. E.g., the function foo_bar must be defined
in the file foo_bar.m
• The Matlab "path" determines where Matlab will look for scripts and
functions.
• A common pitfall is to create a script or file whose name conflicts
with the name of a Matlab function that your program needs. Ways to
avoid this:
o Use names that are rather long.
o Use names that can not possibly conflict with Matlab names, e.g.
"homework1".
o Begin names with your initials, e.g., ma_plot instead of plot
• The help command uses comments for its text.

10/18/2010 Programming For Engineers-CSCI 1190 30


More on plotting
• The figure command can group related graphics and set
some attributes for all the graphics.
• The axis command can set the range of graphics shown,
the size of the window, tick-marks, etc.
• The hold command prevents the figure from being
cleared and redrawn for each plot.
• Use help to get more information!

figure('Color', 'w'); % White background


hold; % Don't clear...
axis([0 10 -5 5]); % 0<=x<=10, -5<=y<=5
axis manual; % Use my size!

10/18/2010 Programming For Engineers-CSCI 1190 31


More on plotting-ctd
• Try the following:
>> help plot % more info on plot
>> help axis % more info about axis
>> % an example:
>> figure('Color', 'w');% White background
>> hold; % Don't clear...
>> axis([0 10 -5 5]); % 0<=x<=10, -5<=y<=5
>> axis manual; % Use my size!
>> x = [0 1 2]
>> y = [0 1 0]
>> plot(x, y)

10/18/2010 Programming For Engineers-CSCI 1190 32


Exercise
• Run the following function. What is the output when you give
values 5 and 6 to the function?
% cylinderCompute function calculates the volume &
% surface area of a cylinder.
% Input: r - Radius (any unit)
% h - Height (same length unit as r)
% Output: V - volume
% A - surface area

function cylinderCompute(r,h)

%% Calculations of the volume of a cylinder


V = pi*h*r^2;
%% Calculations of the surface area of a cylinder
A= 2*pi*r*h;
disp(V);
disp(A);
end
10/18/2010 Programming For Engineers-CSCI 1190 33

You might also like