Download presentation
Presentation is loading. Please wait.
Published byBelinda Moody Modified over 9 years ago
1
Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms and programs 3. Variables and data types 4. User I/O 5. Operators 6. Conditionals (IF) 1
2
1. Computer 101 The Von Neumann computer architecture is mostly what we use today. The architecture separates a computer in 3 major parts: The Central Processing Unit (CPU) The computer memory The Input/Output (I/O) devices 2 CPU + memory Screen=output Speakers=output Mouse=input Keyboard=input ? ? Knob=input History 4/4
3
Exam Topics Software: OS, apps, compilers 1. Describe the purpose of a compiler. 2. What differentiates the Operating System (OS) from an application (app)? Answer: Next slide 3
4
Categories of software Software contains the instructions the CPU uses to run programs. There are several categories, including: Operating systems (OS) – manager of the computer system as a whole Software applications – commercial programs that have been written to solve specific problems Compilers / Interpreters - to ‘translate’ programs written by people into something understandable by the machine 4
5
Generations of Languages used to write software 1) Machine language – also called binary language. Sequence of 0’s and 1’s. 2) Assembly language – each line of code produces a single machine instruction (add, subtract…), see bottom of page 11. 3) High-level language – slightly closer to spoken languages. 5 addb,c adda,b a= a + b + c; This line does the same as the two above.
6
Finally… MATLAB Is an interpreted language – does not require compilation, but instead has an interpreter running behind the scenes Has an interactive environment – “In the MATLAB environment, you can develop and execute programs that contain MATLAB commands. You can execute a MATLAB command, observe the results, and then execute another MATLAB command that interacts with the information in memory, observe its results and so on.” 6
7
2. Develop a Solution Follow these steps in order: 1. State the problem clearly 2. Identify the givens vs. the results wanted This will be referred as the I/O diagram 3. Manually solve the problem 4. Computerize the solution 5. Test, test, test!!! 7
8
3. MATLAB interface, Variable and Data Types Know variable and file naming constraints Understand the functions listed in the lecture notes [e.g. sin(), cos(), sind(), cosd(), sqrt(), etc] – know the difference between sin() and sind(), for example. What is ans ? What does clear do? What about clc ? How do you suppress default output? 8
9
9 Frame #1 “The Command Window” Frame #2 “Current Directory” Frame #3 “Workspace” Frame #4 “Command History” “Your Calculator Screen” You can do exactly as on your calculator: add, subtract, divide, multiple, use parentheses to override the order of operations… Later on, you will realize you can do a LOT more in this frame. This frame shows the files (excel, text, Matlab files) that can immediately be used. It will show the variables that have been created. It will show all the commands executed previously.
10
10 Exam Topics Starting with MATLAB
11
11 Exam Topics Starting with MATLAB
12
Variables A variable is a name given for a memory location “Assigning a value to a variable” means to place a value in the memory location associated with the variable name Matlab usually shows what the variables are - in the Command Window, but also in the Workspace.
13
Functions Common functions sin(), cos(), tan(), sind(), cosd(), tand(), asin(), acos(), atan(), asind(), acosd(), atand() exp(), sqrt(), log(), log10() abs(), round() 13
14
“Suppressing” the output Second version, with semi-colons 14 Using a semicolon on these lines “suppresses the output”. In other words, it does not show the result of the command. But… did the commands do anything?
15
Data Types A data type is a category of information. The data type determines how information is stored in the computer and how it can be used. Integers whole numbers Floats numbers with fractional portion Strings sequences of characters 15
16
4. Inputs and Outputs What data-type is the user asked for? num_apples = input('How many WHOLE apples? '); The user is asked to provide a number – specifically an integer. name = input('Enter your name: ', 's'); The user is asked to provide a sequence of characters – i.e. a string. These are the only 2 forms of using the input() built-in function. 16
17
Form #1. input() In the first form, only 1 prompt is inside the parentheses: num_apples = input('How many WHOLE apples? '); There is only one argument to the function. Arguments are inputs to the function – information being sent into the function so it can do its job. The one argument is the prompt string: 'How many WHOLE apples:' 17
18
Form #2. input(…,‘s’) In the second form of the input() function, there are TWO arguments: the prompt string, and another string: ‘s’ name = input('Enter your name: ', 's'); If this argument is present, it must be the letter 's' and no other letter! 1 st argument2nd argument 18
19
19 The fprintf() function The format string Placeholders Format modifiers left justification width specifier precision specifier Escape sequences Output
20
Using fprintf() 1. fprintf(...) % is the built-in function 2.fprintf(‘format String InSeRtEd hErE!’) % The format string allows you to put words and specify a format (UPPER CASE vs. Lower case, and punctuation only) 3. fprintf(‘format string with placeholders’, list of variables to print are inserted here. If more than one variable is to be printed, each is separated by a comma from the previous one) % Placeholders allow a specific format to be set (aligned right, and 2 decimal places for example) 20
21
Stop for vocabulary Just a quick recall about the vocabulary. fprintf(‘The value in result is %f meters.’, result); “function call” ‘format string’ “placeholder” (part of format string) variable to be printed 21
22
Most common placeholders Each data-type has a placeholder Integer %d Floats %f Strings %s A single letter %c 22
23
Special Characters Escape sequences can also be used within the format string: \n - this will create a new line when printing the string \t - tab (tabs the text to the right) '' - this will place one apostrophe in the final sentence displayed Example of all three: >> fprintf('%s''s age:\t\t%d years old\n\n', name, age); Fred's age:47 years old >> 23
24
Format Modifiers Once the base placeholder is ready, modifiers further change how the values are displayed. Complete Format Modifier form: %-7.2f Left-justify the value TOTAL width to occupy Nb. of decimal places 24
25
5. Operators & Operands Operators work on operands. There are 2 types of operands: 1. Numerical 1, 3.5, -47 2. Logical true, false Arithmetic ( +, -, /, *, ^ ) and relational (, >=, ==, ~= ) operators work with numerical operands Boolean ( &&, ||, ~ ) operators work on logical operands 25
26
Boolean Operators These operators take logical values and perform some operation on them to yield a logical value Two Boolean operators allow to COMBINE relational expressions && Logical AND || Logical OR One Boolean operator allows to NEGATE the result ~ Logical NOT “Negates”: turns true values into false, and false values into true 26
27
Operators: unary vs. binary Operators can be unary – taking only one operand: y = -x; opposite = ~result; Or binary operators – taking two operands: z = x * y; z = x + y; z = x – y; %both unary and binary! z = x / y; z = x ^ y; x >= 7 (x y) 27
28
Operators: Output values TypeInput valuesOutput values Arithmetic: Numbers Numbers e.g. 5 * 315 Relational:NumbersLogical e.g. 5 < 3false Boolean:LogicalLogical e.g. ~truefalse 28
29
Unary vs Binary operators &, &&, |, ||, +, -, *, /, ~, <, =, == From the set of operators above… Which one(s) can be used as either unary or binary operators? Which one(s) are only unary operators? 29
30
Name these operators: Relational <, >, <=, >=, ==, ~= Boolean &&, ||, ~ Know the true and false keywords 30
31
31 Q: Operators
32
6. IF: General template if elseif. elseif else end 32
33
% ask user for day day = input(‘What day # is it? ’); % find which day it is if day == 7 %saturday state = ‘weekend’; elseif day == 1 %sunday state = ‘weekend’; else %any other day state = ‘weekday’; end 33 Example: weekend? weekday?
34
Using Logical OR % ask user for day day = input(‘What day number is it (1-7)? ’); % find which day it is if day == 7 || day == 1 %Saturday or Sunday state = ‘weekend’; else %any other day state = ‘weekday’; end 34
35
Common Mistakes WHAT YOU CANNOT DO: is shortcut the expression %if angle is between 0 and 90 degrees if 0<=angle<90 quadrant = 1; elseif 90<angle<=180 %quadrant 2 quadrant = 2; end 35 DOES NOT WORK RIGHT Instead, rewrite each condition separately! if 0<=angle && angle<90 quadrant = 1; elseif 90<angle && angle<=180 quadrant = 2; end
36
36 Q: Conditionals Boolean expressions Using &&, ||, and ~ What is the output of this code? a = true; b = 1; c = -4; if a && ~(b>c) disp('IF'); else disp('ELSE'); end
37
37 Q: Conditionals
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.