CS 170 – Intro to Programming for Scientists and Engineers

Download Report

Transcript CS 170 – Intro to Programming for Scientists and Engineers

CS 170 – INTRO TO
SCIENTIFIC AND
ENGINEERING
PROGRAMMING
The Uber Stack
Applications
Middleware
Operating Systems
Computers
Processors
Memories
Display
Components
Interconnects
Materials
Physics
This is what computers are made
of!
How we Interact with Computers
•
User
Applications
Operating System
Hardware
Our CPU is at the core
Fetch,
Decodes,
Executes Instructions
in sequence
What is Software?
• Software = Programs
• Programs = a set of computer instructions for
carrying out computing tasks
• Programmer = He or She that attempts to
correctly structure those instructions to achieve a
desired result !
Computer Programmers;
a definition
“Their rumpled clothes, their unwashed and
unshaven faces, and their uncombed hair all
testify that they are oblivious to their bodies and
to the world in which they move. These are
computer bums, compulsive programmers.”
Programmers having fun at work
“The trouble with programmers is that you can
never tell what a programmer is doing until it’s
too late.”
Pseudo Code(ing)
• Allows you to write a program in english for the purpose of
design without having to worry about specific computer
language syntax and allows the programmer to
concentrate on the logical sequences
• Start
• Input (amount)
• Amount = amount*4.33
• If amount < 58 then print “It’s less than 58”
• Else print “It’s over 58”
• End
Programming Language
• A programming language is a convenient way of
expressing instructions for a computer to execute
• Computer languages have evolved to the point
where anyone can learn the basics of using one
Low and High Level Languages
• Programming languages are divided up into low-
level languages and high-level languages.
• The closer the language is to machine language,
the lower the level.
• High level languages make things easier to
program.
Machine Language
Every computer CPU has its machine language,
the set of instructions it knows how to execute.
This is the lowest level.
A typical instruction might say, get the contents of
a memory location and put it in the accumulator
(perhaps in preparation for adding it to another
number).
A Machine Language Program
• Put contents of memory location 10 in accumulator.
• Add contents of memory location 11 to accumulator.
• Put contents of accumulator back in location 10
• It might look like
110011011111100111110000011110000010000011111
100010001000100100001000001111000001110000000
010000100010010000101010000111001110000011000
…..not very user friendly! But believe it or not, computers were
programmed in machine language at one time.
Natural Languages
• Computers don’t understand English
• Need to deal with
• Ambiguity
• Redundancy
• Literalness
• So, we express what a computer should do in a
formal language
High-Level Languages
In a high-level language the previous piece of code
might look like this:
Input current_balance
current_balance = current_balance +new_check;
store current_balance
Some High Level Languages Still in Use
Today
• Fortran
• Basic
• Matlab
• PL/1
• LISP
• Cobol
• ADA
•C
• JAVA
• C++
• PHP
• VISUAL BASIC
• Python
Matlab Basics
Starting Matlab
• double-click the Matlab icon, or
• in a terminal window, type matlab, and return
The Command Window and the Matlab prompt >>
The Desktop menu
• undocking command window
• tiling other windows (command history, workspace, current directory,
profiler)
The File menu
• Preferences
The Help menu
Matlab Basics
Matlab as a calculator
• Type expressions at the >>, and press return
• Result is computed, and displayed as ans
• Use numbers, +, *, /, -, (), sin, cos, exp, abs,
round,…
Precedence rules in expressions
• Left-to-right within a precedence group
• Precedence groups are (highest first)
• Highest precedence is parenthesis, then…
• Power (^)
• Multiplication and division (*, /)
• Addition and subtraction (+, -)
Examples of expressions
Legal expressions
>> 4
>> 5 + pi
>> 6*sqrt(2)^4-12
>> 6 * sqrt(
2.0) ^ 4 - 12
>> sin(pi/3)^2 + cos(pi/3)^2
>> 1.0/0.0
>> -4/inf
>> 0/0
Illegal expressions
>> 2 4
>> (2,4)
Error messages
– Read them carefully – a large portion of the time you will quickly figure out
what is wrong
Variables
Use names to assign result of an expression to a variable
– Variables do not need to be declared before assignment
A single “equal” sign (=) is the assignment operator,
LHS = RHS
Read this as
– evaluate expression on the right-hand side, and then…
– assign the result to the variable named on the left-hand-side
Therefore
– The right-hand-side needs to be a legal Matlab expression
– The left-hand-side needs to be a single variable name (this will get
more sophisticated later on)
A semicolon at the end of the RHS expression suppresses the
display, but the assignment still takes place.
Examples of Variables and Assignment
Legal
>>
>>
>>
>>
>>
A
B
A
A
C
=
=
=
=
=
sqrt(13)
exp(2);
2*B
A + 1
tan(pi/4)
Illegal (all for different reasons)
>> D = sqrt(E) + 1;
>> 3 = E
>> 3*A = 14
>> F = 2 3
The “workspace”
All variables that you create are accessible from the prompt >>
Variables are accessed using their name as a reference
Builtin Matlab commands
>> who
>> whos
are used to see what is in the workspace.
You can clear (ie, erase) variables with the clear command
>> clear A
clears the variable A from the workspace.
How do we check that it worked?
Comparison (as opposed to assignment)
Compare the equality of two expressions with a double equal sign, ==
Several arithmetic comparisons are available. Can also compare
– greater than (with >)
– less than (with <)
– greater than or equal (with >=)
– less than or equal (with <=), and
– not equal (with ~=)
The comparison is itself an expression
– Its value is either 1 (true) or 0 (false), and can be used in assignment
>> 5==sqrt(25)
>> E = 1.72>tan(pi/3)
What do you need to know to determine the result of
>> E = 4>5-2
Precedence group is lower than addition and subtraction, so…
Quiting Matlab
Type quit at the prompt, or
Select Exit Matlab from the File menu
However…
Saving the workspace
When you “quit” Matlab, the variables in the workspace are erased from
memory. If you need them for later use, you must save them.
You can save all variables (or just some of them) to a file using the
command save
>> save
saves all of the variables in the workspace into a file called matlab.mat
(it is saved in the current directory)
>> save Important A B C D*
saves the variables A, B, C and any variable beginning with D in the
workspace into a file called Important.mat
Loading from a .mat file
load is the opposite of save. It reads a .mat file, putting all
variables contained in the .mat file into the workspace
>> load
loads all of the variables from the file matlab.mat
>> load Xenia
loads all of the variables from the file xenia.mat
There are no known security problems with load. Hence, you
can safely send (as attachment), receive and use .mat files from
others.
Complex Numbers
All arithmetic in Matlab works on complex numbers as well.
When you start Matlab, two variables already exist, and are equal
to
. They are i and j. It’s common to overwrite them
without even realizing, but you can always create the number with
the expression sqrt(-1)
>> i
>> sqrt(-1)
>> j
>> 4 + 6j
>> 4 + 6*j
>> C = 1 – 2i;
>> real(C)
>> imag(C)
>> abs(C)
>> angle(C)*180/pi
Syntax errors
• Violations of Matlab code
• Read them carefully
• Line number
• First error
• Error can help you solve issues
Help
• Documentation
• Help <fcn>
Questions
• What is software?
• What is a program?
• Do we execute the code of a program?
Comments
%
%
%
%
Author: Nickolas Zeppos
Section 1, CS 170
January 22, 2013
HW Assignment 1
Figure window
• Plots
• Images
QUESTIONS??
Resources
• “Introduction to Programming with Matlab”, J. Michael
Fitzpatrick and John D. Crocetti
• Lecture slides E77, Andy Packard,
http://jagger.me.berkeley.edu/~pack/e77
• Lecture slides, The Digital World, Donald Stanford
http://cs.brown.edu/courses/csci0020.html