Professional Documents
Culture Documents
for Engineers
CSCI-1190
Lecture 1
Minoo Aminian
Rensselaer Polytechnic Institute
Read the complete Syllabus from the
class website:
http://rpilms.rpi.edu
Simple flowchart
• Design an algorithm.
Debug
• Develop source code from algorithm.
sin(90)
sin(pi/2)
cos(pi/4)
sin(pi/4)
tan(pi/4)
sind(90)
cosd(90)
tand(90)
ang = pi/4
c = cos(ang)
s = sin(ang)
c^2 + s^2
• Watch the
"memory objects" in
the Workspace window.
clear
clc
ang = pi/4;
c = cos(ang);
s = sin(ang);
c^2 + s^2;
ans
a = [1 2 3]
a+a
5*a
a*a
a .* a
a'
a * a'
a-3
a/2
2\a
b = [ 2 ; 3 ; 4]
a*b
b = [ 5
7]
a = [ 1 2 3
4 5 6 ]
1:10
0:10:50
50:-5:15
theta = 0:10:180;
thetarad = theta*(pi/180)
c = cos(thetarad);
s = sin(thetarad);
theta(4)
c(4)^2 + s(4)^2
clear
clc
%% 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.
function cylinderCompute(r,h)