Presentation is loading. Please wait.

Presentation is loading. Please wait.

Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = 2 -3 4 1 6 -3 -5 1 2 B = 10 4 3 >> X = A\B X = 1.2609 2.2261 3.5391.

Similar presentations


Presentation on theme: "Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = 2 -3 4 1 6 -3 -5 1 2 B = 10 4 3 >> X = A\B X = 1.2609 2.2261 3.5391."— Presentation transcript:

1 Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = 2 -3 4 1 6 -3 -5 1 2 B = 10 4 3 >> X = A\B X = 1.2609 2.2261 3.5391 >> 2*X(1) -3*X(2) + 4*X(3) ans = 10

2 Element by Element Operators Multiplication.* x(n) = A(n)*B(n) Division./x(n) = A(n)/B(n) Exponentiation.^x(n) = A(n)^y(n) Left Division.\ x(n) = B(n)/A(n) x = A.*B x = A./ B x = A.^ y x = A.\B

3 EGR 106 – Week 4 – Files Saving commands as simple “programs” Using Matlab’s editor Useful script commands Saving and reading data Textbook chapter 4, pages 77-100

4 So far: Commands typed in the command window: – Executed by pressing “enter” – Edited using the arrow keys or the history window Results are temporary: – Rerun commands – Keep a “diary” file Scripts or m files Data files

5 Script Concept A file containing Matlab commands – Can be re-executed – Are easily changed Commands are executed in sequence – Results appear in the command window (or use ; ) – File is executed by typing its name (without.m) Can be created using any text editor –.m extension – Listed in current directory window

6 Matlab’s Built-in, Color Editor: Can create a new file or open an existing m file (icons or click on file name) Color used to aid in file creation (command types, typos)

7 – typical windows menu – line numbers – “run” button or F5 – debug capability – comment lines – note use of semicolons – note use of colors

8 Where Matlab Looks for Things 1. As a variable in the current workspace 2. As a built in function 3. As an m file in the “current directory”

9 4. Down the “search path” of directories Conclusion – use unique names

10 How Scripts Get Data From arrays in the current workspace From arrays defined in the script Using the “input” command: – Numeric: x = input(' how many? ') – String: x = input(' name? ', 's')

11 How Scripts Shows Data Command of the array name Using the display command: – Existing array – disp(x) – Text disp(' The task is done ')

12 Example : Note that disp shortens the resulting output by dropping the array name and removing blank lines

13 Formatted print command fprintf(format,data) – Combines text and data – Allows complete control of the output – format is a string (in quotes) containing text and control commands (using \ or %) – like in C – An example: \n = new line %d = put data here The data itself

14 Escape characters: – New line ( \n ), backspace ( \b ), tab ( \t ), … Conversion codes for data: – Decimal ( %d ), character ( %c ), exponential notation ( %e ), fixed precision ( %f ), … – Can add field width and precision information: 5 characters wide, 2 after the decimal point

15 fprintf works element-by-element on array data: \n added for line return

16 fprintf can also write to a file: fid = fopen( 'name', 'w' ) fprintf(fid,format,data) … fclose(fid) – fid is a variable name to specify which file – name is the name of the file w indicates “write”

17 Example Scripts

18

19

20 Matlab Data Files (not in the text) Types: – ascii = regular text files –.mat = Matlab’s proprietary format (multiple variables)

21 Saving data: – save filename – save filename array1 array2 – save filename –ascii Reading in (loading) data: – load filename – load filename array1 array2 – load filename –ascii

22 Debugging Errors Syntax errors: Note red text = bad news

23 Run-time errors: inf or NaN results Note black text = is okay

24 Logical errors in your program – hard to find – Example: quadratic equation solver – But x 2 +2x+1 = (x+1) 2  x = -1 Use the built-in debugger Missing parentheses

25 New Homework Procedure Electronic submission via e-mail One single m file: executing it should – Present solutions in order – Pause in between problems – Use clear, clc, and clf to clear out old data, results, and/or plots – Use ; to suppress unnecessary outputs – Name file as yourlastname_4.m


Download ppt "Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = 2 -3 4 1 6 -3 -5 1 2 B = 10 4 3 >> X = A\B X = 1.2609 2.2261 3.5391."

Similar presentations


Ads by Google