Fundamentals of Programming 20-ENFD

Download Report

Transcript Fundamentals of Programming 20-ENFD

Fundamentals of Programming
20-ENFD-112
Sections 001 to 007
Instructor: Prof. Dieter Schmidt
Lecture: Monday, Wednesday 3:00-3:50
Web page http://www.blackboard.uc.edu
Overview of MATLAB

MATLAB



Computer Algebra Program for solving
mathematical problems in engineering
MATLAB like a scientific calculator – but much
much more.
Other Computer Algebra Programs



Mathematica, Maple, Magma, and others
Difference to MATLAB: These programs do most of
their calculations in “symbolic” form and usually
use exact “integer arithmetic”
MATLAB uses “floating point arithmetic”
Availability of Computer
Algebra Programs at UC

Mathematica





Site license for entire campus
Available in all labs
Individual copies can be bought in computer store
Used by Department of Mathematics in Computer Lab for
calculus
MATLAB




Site license only for College of Engineering
Available only in labs (and classrooms) of College of
Engineering under folder Mathematics
Individual student version can be bought for $100
Used in several courses in CoE
Student version of MATLAB




Same capabilities as full version
Prompt is EDU>> instead of >>
Restriction: no commercial use
Full version of MATLAB is expensive

It has many modules for different fields, the first two come
with student version of MATLAB, others have to be bought







Simulink
Mathematics toolbox
Bioinformatics toolbox
Data Acquisition toolbox
Financial toolbox
Statistics toolbox
and others
Starting MATLAB


MATLAB has to be installed
Double click on icon


For student version CD of MATLAB has to
be in CD drive.
MATLAB desktop



Command Window
Command History Window
Current Directory Window
Arithmetic Operations

Lowest precedence



+
-


*
/
\


Multiplication:
right division:
left division:
a*b
a/b
a\b
same as b/a
good for matrix operations, avoid otherwise
Highest precedence


a+b
a-b
Next higher precedence


Addition:
Subtraction:
^
exponentiation: a^b
Evaluation from left to right for equal precedence
Use parentheses to change order
Format statements,
for display of results


format short
format long

format
format
format
format
format
format
format

Internal representation: roughly 15-16 decimal digits accuracy








short e
long e
bank
+
rat
compact
loose
displays up to 6 decimal digits 1.6667
displays up to 16 decimal digits
1.666666666666667
base 10 notation 1.2345e+002
base 10 notation 16 digits
two decimal digits 123.45
displays only +,-, or blank (of limited use)
displays rational approximation
suppresses some line feeds
undoes format compact
depends on computer
usually 64 bit binary floating point numbers, IEEE format
Predefined values

ans
always the result of the previous execution
eps
accuracy of floating point numbers, that is, last
binary digit (bit) that can not be stored
Inf
numbers ≥ 21024 10308
NaN
not a number i.e. 0/0
pi
Note with format rat: pi=355/113
i,j
imaginary unit sqrt(-1)

Watch out, these values can be redefined







This may lead to unexpected results
i,j are less of a problem, when correct format for complex
numbers are used.
Available Mathematical
Functions











exp(x)
sqrt(x)
log(x)
log10(x)
cos(x)
sin(x)
tan(x)
acos(x)
asin(x)
atan(x)
and others
natural logarithm, base e
base 10
cos -1(x)
Help within Matlab

In command window






help function
helpwin topic
lookfor topic
type filename
doc function
or use menu bar
Plotting of functions



x = [ -10 : 0.1 : 10 ];
y = sin(x) ;
plot( x, y), xlabel('x'), ylabel('y')
Arrays


x=[-10 : 0.1 : 10]
x is an array of 201 values




-10,-9.9,-9.8,…,9.8,9.9,10.0
x(1),x(2),x(3),…,x(199),x(200),x(201)
[start : increment : stop]
Note ":" with "," it would only be an array
with three elements:



x=[-10 , 0.1 , 10]
gives x(1)=-10, x(2)=0.1, x(3)=10
x(4) and others are now undefined
Functions of arrays

Most built-in functions produce desired result



y = sin(x) produces array y of same length as x
plot(x,y) requires two arrays of same length
y = x^2 will not work, nor does y=x



*
x
for Matlab x is a matrix (array)
rules for matrix multiplication are different
to tell Matlab to do element by element operation
need to write y = x.^2 or y =x.*x