Syllabus Instructor: Chris Policastro Class Website: https://math.berkeley.edu/~cpoli/math98/fall2016.html See website for 1 Class Number 2 Oce hours 3 Textbooks 4 Lecture schedule slides programs
Syllabus Class Website: https://math.berkeley.edu/~cpoli/math98/fall2016.html See website for 1 Homework grading policy schedule assignments solutions Homework Submission: https://bcourses.berkeley.edu See bcourses site for 1 Suggestions and hints on the homework and project. 2 Homework must be submitted by 11:59pm
Syllabus Class Website: https://math.berkeley.edu/~cpoli/math98/fall2016.html See website for links to the documentation 1 Language fundamentals: http: //www.mathworks.com/help/matlab/language-fundamentals.html syntax operators data types array indexing and manipulation 2 Programming Scripts and Functions http://www.mathworks.com/help/matlab/ programming-and-data-types.html
Getting started with MATLAB MATLAB has ve features: 1 Command Window is a programmable calculator with broad capabilities. Here we can perform calculations, dene variables, debug and much more. 2 Editor allows us to write collections of commands and save them as M-les. 3 Current Directory shows the les that MATLAB is accessing. By default MATLAB cannot execute les contained in other folders. Use pwd to check current directory Use dir to check contents 4 Workspace is a MAT-le containing the variables you have dened in your session. 5 Command History Can be accessed using up arrow. It should be closed in the interface.
Lecture 1: Turning formulas into programs 1 Problems (problem_1_1) Surface area of a sphere (Chapter 1.1 van Loan) (problem_1_2) Minimum of quadratic polynomial on an interval (Chapter 1.2 van Loan) 2 Concepts variable, initialization, arithmetic, string conditional, logical expression, boolean expression
(problem_1_1) Surface area of a sphere The formula for the surface area of a sphere of radius r is A(r) = 4πr 2 For large values of r, the numbers r and r 2 dier by a large amount. The factor r 2 in the formula implies that a small change in r (call it δr) aects a large change in surface area (call it δa) We want to study δa as a function of δr to illustrate rounding errors through comparison of the (approximate) formulas 1 4π(r + δr) 2 4πr 2 2 4π(2r + δr) δr 3 8πr δr for δa.
Arithmetic Operations: +,,, /,ˆ. (2 3 (5 4)) 4/5 = 4 (2 3(5 4)) 5 1/2/3/4 = ((1/2)/3)/4. Order of operations goes parentheses, division, multiplication, addition/subtraction 2 + 3, 2 3, 2/3, 2 3,2 3,2 1/3 log 2 3, π 3
Command line Use a semicolon to suppress the output of command. Multiple commands can be placed on a line separated by commas or semicolons. sqrt(5); abs( 3), sign( 10); Use SHIFT-ENTER to start a new line. Use ellipses (... ) and SHIFT-ENTER to continue a line. Ellipses cannot be inserted anywhere e.g. sqrt(5... );
Variables The value of an arithmetic expression is saved as ans. ans is a variable whose value is stored in the Workspace. The Workspace is an example of a MAT-le, which is a special le that contains MATLAB data. Variables can be dened by Variable_Name = Value for example r=6367 This is called initializing a variable. MATLAB is case sensitive. However all built-in MATLAB functions use lower case letters. Therefore try to use at least one capital letter to avoid conict (e.g. Abs not abs)
Example 1 %%%S u r f a c e A r e a.m%%% 2 % 3 % Computes s u r f a c e a r e a o f s p h e r e o f r a d i u s r 4 % User i s prompted to i n p u t r 5 6 r=i n p u t ( ' E n t e r r a d i u s r : ' ) ; 7 A=4 p i r ^2; 8 f p r i n t f ( ' r = %10.3 f A = %10.3 e \n ', r, A) ;
Scripts and m-les A script is a short computer program, that is, a procedure implemented as a sequence of instructions for the computer. The order of the instructions is important. An m-le is a le containing a script for MATLAB. The name of the le must have the format filename.m. For MATLAB to execute the le, it must be saved in the Current Directory.
input A string is a sequence of characters enclosed by single quotation e.g. `stringexample&)' The command input will prompt the user to input a number or string. If the input should be a string, then use input('exampleinstructions ','s')
fprintf The command fprintf is used to display formatted strings that incorporate specied values fprintf('formattedstring',listofvariables) The formats are %d for integer, %e for scientic notation, and %f for decimal format. fprintf('temperature = %4d Pressure = %5.9f \n',t,p) \n is to start a new line.
Commenting with % Except within a string, everything following % is a comment. Comments facilitate the reading of code by providing information about the organization. Comments should always appear at the beginning of a document. Providing comments is called documenting. Using explanatory le names and variable names helps with documenting. temp for a temporary variable not auehh125
Review of problem_1_1 There are two sources of error in the calculation from problem_1_1. 1 π is an irrational number meaning its decimal expansion continues without repeating. A computer can only process irrational numbers through approximation by rational numbers. 2 Even rational numbers are rounded in MATLAB. Think of a calculator rendering 1 divided by 3 as.3333. This error is carried through a calculation. The sources of error account for 1 The dierence between method 1 and method 2 2 The nearness of method 1 and method 3 Note the large change in surface area from small change in radius. Compare to the surprising fact that increasing circumference by 2πδ means increasing the radius by δ.
exercise_1_1 A temperature can be converted from the Fahrenheit scale into the Celsius scale using the formula c = (5/9)(f 32) where c is Celsius and f is Farenheit Write a script that prompts the user for a temperature in Farhenheit, converts it to Celsius and prints it.
(problem_1_2) Minimum of quadratic polynomial on an interval A quadratic polynomial has form f(x) = x 2 + bx + c. It attains minimum over R at x = b/2. On an interval [L, R] the minimum may be attained at one of the end points. We want to write a program that will 1 prompt the user for a quadratic polynomial x 2 + bx + c 2 prompt the user for an interval [L, R] 3 compare the values of b/2, L and R 4 print the location of the minimum on the interval For this we need to know about conditional statements.
Relations The following statements take value 0 or 1 a<b a less than b a>b a less than or equal to b a<=b a greater than or equal to b a>=b a greater than or equal to b a==b a equal to b a = b a not equal to b
Logical statements 1 and(a,b) equivalently a & b 2 or(a,b) equivalently a b 3 not(a) 4 xor(a,b) What do the commands && and do?
boolean expression A boolean expression is any expression involving relations or logical statements ((4 <= 100) ( 2 >= 7))& (0.5 == 1e16) A boolean expression takes the values 1 (for true) and 0 (for false). Note that 0 and 1 are just number; there does not exist a separate class for them. The order of operations for evaluations of boolean expressions is 1 negation 2 relations (<, =,...) 3 and 4 or
if-else The construct is used in situations where the decision to choose between a pair of alternative computations is based upon a boolean expression 1 i f b o o l e a n e x p r e s s i o n 2 3 command to be e x e c u t e d when e x p r e s s i o n i s t r u e 4 5 e l s e 6 7 command to be e x e c u t e d when e x p r e s s i o n i s not t r u e 8 9 end See van Loan and Fan for a variant on the construction using if-elseif-else
Review of problem_1_2 Just as arithmetic comes with rules such as 1 order or operations 2 association 3 distribution... manipulations with boolean expressions have rules such as 1 order or operations 2 association 3 distribution... By taking to 0 to be false and 1 to be true, we can represent boolean expressions on a computer. These expressions are the ingredients of conditional statements, which execute commands based on the validity of boolean expressions.
exercise_1_1 A string can be inserted into another string using fprintf. The place holder for a string is %s. Take Str='MATH98'. fprintf('%s is a class',str) would print MATH98 is a class to the command window. Write a script that prompts the user for two numbers. Call them x and y. Use an if-else statement to determine whether x equals y. Output either The numbers are equal or The numbers are not equal to the command window.