Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

Similar presentations


Presentation on theme: "CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING."— Presentation transcript:

1 CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING

2 This is what computers are made of! The Uber Stack Applications Middleware Operating Systems Computers ProcessorsMemories Display Components Interconnects Materials Physics

3 How we Interact with Computers Applications Operating System Hardware User

4 Our CPU is at the core Fetch, Decodes, Executes Instructions in sequence

5 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 !

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

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

8 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

9 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

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

11 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).

12 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 …..not very user friendly! But believe it or not, computers were programmed in machine language at one time.

13 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

14 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

15 Some High Level Languages Still in Use Today Fortran Basic PL/1 Cobol C C++ VISUAL BASIC Matlab LISP ADA JAVA PHP Python

16 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

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

18 Examples of expressions Legal expressions >> 4 >> 5 + pi >> 6*sqrt(2)^4-12 >> 6 * sqrt( 2.0) ^ >> 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

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

20 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

21 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?

22 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…

23 Quiting Matlab Type quit at the prompt, or Select Exit Matlab from the File menu However…

24 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

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

26 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

27 Syntax errors Violations of Matlab code Read them carefully Line number First error Error can help you solve issues

28 Help Documentation Help

29 Questions What is software? What is a program? Do we execute the code of a program?

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

31 Figure window Plots Images

32 QUESTIONS??

33 Resources “Introduction to Programming with Matlab”, J. Michael Fitzpatrick and John D. Crocetti Lecture slides E77, Andy Packard, Lecture slides, The Digital World, Donald Stanford


Download ppt "CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING."

Similar presentations


Ads by Google