Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro MATLAB: Special Purpose Computer Program Optimized to perform engineering and scientific calculations Implements the MATLAB programming language.

Similar presentations


Presentation on theme: "Intro MATLAB: Special Purpose Computer Program Optimized to perform engineering and scientific calculations Implements the MATLAB programming language."— Presentation transcript:

1 Intro MATLAB: Special Purpose Computer Program Optimized to perform engineering and scientific calculations Implements the MATLAB programming language – Has an extensive library of predefined functions

2 Advantages and disadvantages Advantages: – Easy to use Interpreted, not compiled Integrated editor/debugger – Platform Independence Works on Windows, Linux, Unix, and MacIntosh – Many predefined functions E.g., arithmetic mean, standard deviation, median, etc. Disadvantages: – Interpreted, not compiled May execute more slowly

3 Command Window In command window, can type: – area=pi*2.5^2 If line is too long, can add … to end of line to continue it on the next line E.g., – x1 = 1 + ½ + 1/3 + ¼ + 1/5 + 1/6 – x1 = 1 + ½ + 1/3 + ¼ … + 1/5 + 1/6 Note the space between ¼ and …!

4 Edit/Debug Window Can create new Matlab files or modify existing ones – Created automatically when you create a new M-file or open an existing one Select File->New->Blank M-file from the desktop menu Select File->Open from the desktop menu – In edit window, type: %This m-file calculates the area of a circle. %and displays the result. radius = 2.5; area = pi * 2.5^2; string = [‘The area of the circle is ‘ num2str(area)]; disp(string); Save file as calc_area.m Run by typing calc_area in Command Window

5 Figure Windows Used to display MATLAB graphics – Can be a two- or three-dimensional plot of data, an image, or a GUI – Write program to plot sin x: %sin_x.m: This M-file calculates and plots the %function sin(x) for 0 <= x <= 6. x = 0:0.1:6 y = sin(x) plot (x,y) Save as sin_x.m and type sin_x into Command Window

6 Getting Help in MATLAB 1.Use the Help Browser – ? Icon in desktop toolbar – Type “helpdesk” or “helpwin” in Command Window 2.Type “help” or “help” followed by function name in Command Window – “help” -displays a list of possible help topics – “help” function – displays help for that function 3.“lookfor” command – Searches summaries of each function for a match Helpful in finding a function you don’t know the name of E.g., suppose you wanted to find a function that takes the inverse of a matrix. – There’s no function called “inverse” – Do “lookfor inverse” – Get: » INVHILB Inverse Hilbert Matrix » ACOSInverse cosine » ACOSHInverse Hyperbolic cosine » ACOTInverse Cotangent » ACSCInverse cosecant » ACSCHInverse Hyperbolic secant » ASINInverse sine » ASINHInverse Hyperbolic sine » ATANInverse Tangent » Etc.

7 Other Useful Commands “demo” in command window (or select demos from the start button – Gives you demos of MATLAB’s capabilities “clc” in command window – Clears content of command window “clf” in command window – Clears content of figure window “clear” in command window – Clears content of workspace Good idea to avoid variables in one program affecting results in another program Abort command (Ctrl-C) – Stops a running program – Good for infinite loops “!” – sends commands to operating system and they are executed as if they’re typed into the operating system’s command prompt – Lets you embed op sys commands into MATLAB programs “diary” filename – Once typed, all input and most output will be echoed into the diary file. Helps find problems – To stop: type “diary off” – To continue: type “diary on”

8 Matlab vs Python Python Functions: def func(x): “”” Summary of this function goes here Detailed explanation goes here “”” return(x*x) Matlab function [y] = func( x ) %Summary of this function goes here %Detailed explanation goes here y = x*x end

9 Python vs Matlab def func(): inp = input('would you like to continue?') totalcost = 0 while (inp =='yes'): totalcost = totalcost + 3 inp = input('Would you like to buy something else?') return totalcost Matlab: function [totalcost ]= func() inp = input('would you like to continue?','s'); totalcost = 0; while (strcmpi(inp,'yes') == True) totalcost = totalcost + 3; inp = input('Would you like to buy something else?','s'); end

10 Python Vs Matlab arr = [3, 2, 8, 1, 4, 7, 9] k = len(arr) print(k) total = 0 for i in range (0,k): total = total + arr[i] print(total); Matlab: arr = [3 2 8 1 4 7 9] k = length(arr); disp(k) total = 0; for i=1:k %Note where loop starts!!! total = total + arr(i); end disp(total)

11 Matlab vs Python arr = [3, 2, 8, 1, 4, 7, 9] k = length(arr) print(k) total = 0 for i in range (0,k,2): total = total + arr[i] print(total); Matlab: arr = [3 2 8 1 4 7 9] k = length(arr); disp(k) total = 0 for i=1:2:k %Note where increment is!!! total = total + arr(i); end disp(total);

12 Vectors In Matlab A vector is a list of numbers expressed as a 1 dimensional array. A vector can be n×1 or 1×n. –Columns are separated by commas (or spaces): h= [1, 2, 3] –Rows are separated by semicolons: v = [1; 2; 3]

13 Matrices in Matlab A matrix is a two dimensional array of numbers. For example, this is a 4×3 matrix: m=[3.0, 1.8, 3.6; 4.6, -2.0, 21.3; 0.0, -6.1, 12.8; 2.3, 0.3, -6.1] 123 13.01.83.6 24.6-2.021.3 30.0-6.112.8 42.30.3-6.1 Columns Rows

14 Defining (or assigning) arrays An array can be defined by typing in a list of numbers enclosed in square brackets: – Commas or spaces separate numbers. A = [12, 18, -3] or A = [12 18 -3] – Semicolons indicate a new row. B = [2, 5, 2; 1, 1, 2; 0, -2, 6] 12 18 -3 2 5 2 1 1 2 0 -2 6

15 Array vs. Matrix Operations Example: x = [2,1; 3,4] y = [5,6; 7,8] 2 1 5 6 3 4 7 8 z = x.* y results in [ 10, 6 21, 32] this is array multiplication z = x * y results in [ 17, 20; 43, 50] this is matrix multiplication So, do NOT forget the dot if you want to do array operations! (.*./.^)

16 Matrix vs Array Multiplication Multiply a.* b 10 15 4 36 a * b 20 50 20 42 b * c 11 16 b.* c error b * d –e–error b.* d –e–error a = [ 10 5 2 9 ] b = [ 1 3 2 4 ] c = [ 2 3 ] d = [ 2 3 ]

17 Left division: q = b\a is equivalent to a x q = b 10x + 5y = 2 2x + 9y = 3 x =.2 -.5y x = 1.5 -4.5y.2 -.5y = 1.5 – 4.5y 4y=1.3 y =.325 x =.2 -.5x.325 x =.0375 So q = [ 0.0375 ; 0.3250 ] (right division: q=a/b is q x a = b, but rarely used!) a = [ 10 5 2 9 ] b = [ 2 3 ]

18 Example:We know the following: In combining colors: 1.If you combines 7 drops of a red hue, 12 drops of green hue, and 8 drops of the blue hue, you get a saturation of 143 2.If you combines 3 drops of red, 6 drops of green and 14 drops of blue you get a saturation of 84 3.If you combines 12 drops of red, 2 drops of green, and 4 drops of blue, you get a saturation of 152. First, write this as a system of 3 linear equations: 7x + 12y + 8z = 143 3x + 6y + 14z = 84 12x + 2y + 4z = 152 Next, in matlab, write the matrices representing these equations. A=[7 12 8;b=[143; 3 6 14; 84; 12 2 4] 152] Now, in matlab, write the equation that would solve for the 3 variables (red- saturation, green-saturation, and blue-saturation) Sol = b\a Sol: [.0608,.0499,.0579]


Download ppt "Intro MATLAB: Special Purpose Computer Program Optimized to perform engineering and scientific calculations Implements the MATLAB programming language."

Similar presentations


Ads by Google