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

25
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
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    221
  • download

    3

Transcript of 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...

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

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

Page 2: 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.

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

Page 3: 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.

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

Page 4: 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.

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

Page 5: 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.

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

Page 6: 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.

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)

Page 7: 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.

•typical windows menu•line numbers •“run” button or F5 •debug capability

•comment lines

•note use of semicolons

•note use of colors

Page 8: 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.

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”

Page 9: 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.

• Down the “search path” of directories

• Conclusion – use unique names

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

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')

Page 11: 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.

How Scripts Shows Data

Command of the array name Using the display command:

– Existing array – disp(x)– Text

disp(' The task is done ')

Page 12: 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.

•Example:

•Note that disp shortens the resulting output by dropping the array name and removing blank lines

Page 13: 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.

•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

Page 14: 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.

•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

Page 15: 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.

•fprintf works element-by-element on array data:

\n added for line return

Page 16: 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.

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

Page 17: 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.

Example Scripts

Page 18: 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.
Page 19: 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.
Page 20: 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.

Matlab Data Files (not in the text)

Types:– ascii = regular text files– .mat = Matlab’s proprietary format (multiple variables)

Page 21: 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.

•Saving data:•save filename•save filename array1 array2•save filename –ascii

•Reading in (loading) data:•load filename•load filename array1 array2 •load filename –ascii

Page 22: 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.

Debugging Errors

Syntax errors:

Note red text = bad news

Page 23: 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.

•Run-time errors: inf or NaN results

Note black text = is okay

Page 24: 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.

•Logical errors in your program – hard to find•Example: quadratic equation solver

•But x2+2x+1 = (x+1)2 x = -1

•Use the built-in debugger

Missing parentheses

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

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