CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

33
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING

Transcript of CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

Page 1: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING

Page 2: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

This is what computers are made of!

The Uber Stack

Applications

Middleware

Operating Systems

Computers

Processors Memories Display Components Interconnects

Materials

Physics

Page 3: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

How we Interact with Computers•

Applications

Operating System

Hardware

User

Page 4: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

Our CPU is at the core

Fetch,

Decodes,

Executes Instructions

in sequence

Page 5: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

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 !

Page 6: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

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.”

Page 7: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

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.”

Page 8: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

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

Page 9: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

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

Page 10: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

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.

Page 11: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

Machine LanguageEvery 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).

Page 12: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

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 like110011011111100111110000011110000010000011111100010001000100100001000001111000001110000000010000100010010000101010000111001110000011000

…..not very user friendly! But believe it or not, computers were programmed in machine language at one time.

Page 13: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

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

Page 14: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

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

Page 15: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

Some High Level Languages Still in Use Today

• Fortran• Basic• PL/1• Cobol• C• C++• VISUAL BASIC

• Matlab• LISP• ADA• JAVA• PHP• Python

Page 16: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

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

Page 17: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

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 (+, -)

Page 18: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

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

Page 19: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

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 = RHSRead 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.

Page 20: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

Examples of Variables and Assignment

Legal

>> A = sqrt(13)>> B = exp(2);>> A = 2*B>> A = A + 1>> C = tan(pi/4)

Illegal (all for different reasons)

>> D = sqrt(E) + 1;>> 3 = E>> 3*A = 14>> F = 2 3

Page 21: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

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

>> whosare used to see what is in the workspace.

You can clear (ie, erase) variables with the clear command

>> clear Aclears the variable A from the workspace.

How do we check that it worked?

Page 22: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

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-2Precedence group is lower than addition and subtraction, so…

Page 23: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

Quiting Matlab

Type quit at the prompt, or

Select Exit Matlab from the File menu

However…

Page 24: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

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>> savesaves 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

Page 25: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

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

>> loadloads all of the variables from the file matlab.mat

>> load Xenialoads 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.

Page 26: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

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

Page 27: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

Syntax errors• Violations of Matlab code

• Read them carefully • Line number• First error• Error can help you solve issues

Page 28: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

Help• Documentation

• Help <fcn>

Page 29: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

Questions• What is software?

• What is a program?

• Do we execute the code of a program?

Page 30: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

Comments

% Author: Nickolas Zeppos% Section 1, CS 170% January 22, 2013% HW Assignment 1

Page 31: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

Figure window• Plots

• Images

Page 32: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

QUESTIONS??

Page 33: CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

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