fundamentals of programming 20-enfd-112 sections 001 to 007 instructor: prof. dieter schmidt...

13
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

Upload: russell-perry

Post on 18-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fundamentals of Programming 20-ENFD-112 Sections 001 to 007 Instructor: Prof. Dieter Schmidt Lecture: Monday, Wednesday 3:00-3:50 Web page :

Fundamentals of Programming20-ENFD-112

Sections 001 to 007

Instructor: Prof. Dieter SchmidtLecture: Monday, Wednesday 3:00-

3:50Web page

http://www.blackboard.uc.edu

Page 2: Fundamentals of Programming 20-ENFD-112 Sections 001 to 007 Instructor: Prof. Dieter Schmidt Lecture: Monday, Wednesday 3:00-3:50 Web page :

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”

Page 3: Fundamentals of Programming 20-ENFD-112 Sections 001 to 007 Instructor: Prof. Dieter Schmidt Lecture: Monday, Wednesday 3:00-3:50 Web page :

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

Page 4: Fundamentals of Programming 20-ENFD-112 Sections 001 to 007 Instructor: Prof. Dieter Schmidt Lecture: Monday, Wednesday 3:00-3:50 Web page :

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

Page 5: Fundamentals of Programming 20-ENFD-112 Sections 001 to 007 Instructor: Prof. Dieter Schmidt Lecture: Monday, Wednesday 3:00-3:50 Web page :

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

Page 6: Fundamentals of Programming 20-ENFD-112 Sections 001 to 007 Instructor: Prof. Dieter Schmidt Lecture: Monday, Wednesday 3:00-3:50 Web page :

Arithmetic Operations Lowest precedence

+ Addition: a+b - Subtraction: a-b

Next higher precedence * Multiplication:a*b / right division: a/b \ left division: a\b same as b/a

good for matrix operations, avoid otherwise Highest precedence

^ exponentiation: a^b Evaluation from left to right for equal

precedence Use parentheses to change order

Page 7: Fundamentals of Programming 20-ENFD-112 Sections 001 to 007 Instructor: Prof. Dieter Schmidt Lecture: Monday, Wednesday 3:00-3:50 Web page :

Format statements, for display of results

format short displays up to 6 decimal digits 1.6667 format long displays up to 16 decimal digits

1.666666666666667 format short e base 10 notation 1.2345e+002 format long e base 10 notation 16 digits format bank two decimal digits 123.45 format + displays only +,-, or blank (of limited

use) format rat displays rational approximation format compact suppresses some line feeds format loose undoes format compact

Internal representation: roughly 15-16 decimal digits accuracy

depends on computer usually 64 bit binary floating point numbers, IEEE format

Page 8: Fundamentals of Programming 20-ENFD-112 Sections 001 to 007 Instructor: Prof. Dieter Schmidt Lecture: Monday, Wednesday 3:00-3:50 Web page :

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.

Page 9: Fundamentals of Programming 20-ENFD-112 Sections 001 to 007 Instructor: Prof. Dieter Schmidt Lecture: Monday, Wednesday 3:00-3:50 Web page :

Available Mathematical Functions exp(x) sqrt(x) log(x) natural logarithm, base e log10(x) base 10 cos(x) sin(x) tan(x) acos(x) cos -1(x) asin(x) atan(x) and others

Page 10: Fundamentals of Programming 20-ENFD-112 Sections 001 to 007 Instructor: Prof. Dieter Schmidt Lecture: Monday, Wednesday 3:00-3:50 Web page :

Help within Matlab

In command window help function helpwin topic lookfor topic type filename doc function

or use menu bar

Page 11: Fundamentals of Programming 20-ENFD-112 Sections 001 to 007 Instructor: Prof. Dieter Schmidt Lecture: Monday, Wednesday 3:00-3:50 Web page :

Plotting of functions

x = [ -10 : 0.1 : 10 ]; y = sin(x) ; plot( x, y), xlabel('x'), ylabel('y')

Page 12: Fundamentals of Programming 20-ENFD-112 Sections 001 to 007 Instructor: Prof. Dieter Schmidt Lecture: Monday, Wednesday 3:00-3:50 Web page :

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

Page 13: Fundamentals of Programming 20-ENFD-112 Sections 001 to 007 Instructor: Prof. Dieter Schmidt Lecture: Monday, Wednesday 3:00-3:50 Web page :

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