Introduction to Matlab Lecture Notes PDF

Title Introduction to Matlab Lecture Notes
Course Intro Elec & Comp Eng
Institution University of Florida
Pages 12
File Size 201.5 KB
File Type PDF
Total Downloads 104
Total Views 158

Summary

MatLab notes for intro to EE...


Description

Introduction to MATLAB Lecture Notes – EEL3000 1. Navigating MATLAB a. New files b. Opening files c. “Workspace” panel, explain that’s how to view variables d. Setting up and navigating different directories i. “Current folder” panel e. Creating variables using “new variable” f. Saving workspace g. Command window, and where/how to input commands. 2. Basic Mathematical Computations a. NOTE: Mention using a semicolon to prevent answer from popping up on screen. >> 5+8 ans = 13 >> 5-8 ans = -3 >> 45/9 ans = 5 >> 30/4 ans = 7.5000 >> 1.8*10 ans = 18 >> 4^2 ans = 16

>> exp(4) ans = 54.5982 >> log(exp(4)) ans = 4 >> log10(10) ans = 1 >> sqrt(16) ans = 4

3. Assigning Variables a. Show that variables created show up in the “workspace” panel >> x = 5; >> y = 6; >> x+y ans = 11 >> x+y; >> x-y ans = -1 >> y/x ans =

1.2000 >> x*y ans = 30 >> x^y ans = 15625 >> y^x ans = 7776 >> 6^5 ans = 7776 >> 6*exp(2) ans = 44.3343 >> 6*log10(10) ans = 6 >> x*log10(10) ans = 5 4. If/else statements. >> if x < 5 z = x*y;

else z = y-x; end >> z z= 1 >> if x < 5 z = x*y; else if x == 5 z = 9; else z = y-x; end end >> z z= 9 5. For loop a. Explain that MATLAB numbers start at 1 and NOT 0, which is not like other programming languages, however for for loop “looping variables” it is okay to start at 0. >> for n = 1:10 z = z + 1; end >> z z= 19 6. “help” function a. Use “exp”,”sqrt”, and “log” functions as examples 7. Special command window commands a. clc,clear b. Get list of handy MATLAB commands 8. Trig functions and complex functions/operations a. Trig functions – explain inputs to functions i. Sin – radians, sind - degrees ii. Cos – radians, cosd - degrees iii. Tan –radians, tand - degrees iv. Asin,asind v. Acos,acosd vi. Atan,atand b. i,j

>>i ans = 0.0000 + 1.0000i >> j ans = + 1.0000i >> sqrt(-1) ans = + 1.0000i c. Complex number functions >> x = 5*i; >> angle(x) ans = 1.5708 >> x = 5; >> angle(x)

%90 degrees

ans = 0 >> x = 5+5*i; >> angle(x) ans = 0.7854 >> conj(x) ans = 5.0000 - 5.0000i >> real(x) ans = 5 >> x = 5+3*i

x= 5.0000 + 3.0000i >> imag(x) ans = 3 9. Arrays, Vectors, and Matrices a. Vectors >> a = [3 1 2]; >> b = [2 3 4]; >> a + b ans = 5

4

6

2

2

>> b-a ans = -1

>> a.*b ans = 6

3

8

>> b./a ans = 0.6667 3.0000 2.0000 >> b.^a ans = 8

3 16

What happens if I don’t use the “.” operator?

>> 5*a ans = 15

5 10

>> sin(b) ans = 0.9093 0.1411 -0.7568 >> b = [6;7;8] %column vectors b= 6 7 8 b. Initial Value: Increment: Final Value >> t = [0:1:10];

%Show what it creates

c. Matrices – Creating and Accessing >> A = [1 3 6; 9 4 7; 2 1 4]; >> A(2,3) ans = 7 Explain here that you access matrices starting from index of ‘1’, not ‘0’ d. Helpful Matrix Functions >> A' %transpose ans = 1 3 6

9 4 7

2 1 4

>> det(A) %determinant ans = -51 >> inv(A) %inverse

ans = -0.1765 0.1176 0.0588 0.4314 0.1569 -0.9216 -0.0196 -0.0980 0.4510 rref,size,horzcat,vertcat 10. Plotting a. 2D >> x = [-10:0.01:10]; >> y = x.^2; >> plot(x,y); %Number of elements in x must match number of elements in y >> xlabel('Independent Variable'); >> ylabel('Dependent Variable'); >> title('Plot of y=x^2'); >> hold on %Adding multiple graphs to the same plot >> plot(x,4*x); >> hold off Note: You can also use “plot(x,y,x,4*x)” to plot both functions on the image b. 3D >> x = [-10:1:10]; >> y = [-10:1:10]; >> z = x'*y; >> mesh(x,y,z) >> surf(x,y,z)

%Wire mesh 3D plot %Surface 3D plot

>> plot3(sin(x),cos(x),2*x) c. Complex >> x = [0:1:10]; >> y = x.^2 + x.*i; >> plot(x,imag(y)); >> plot(x,real(y)); >> plot(y) >> plot(real(y),imag(y)) 11. Rotating plots a. Simply use the “rotate” tool (located at top of plotting window) 12. Pulling intersections from plots a. Graphically i. Use “data cursor” function (located at top of plotting window) and visually inspect and record the data values. b. Creating a function that mathematically calculates intersections (Search Mathworks website for ideas)

13. Removing single plots from graph (altering plot handles) >> x = [-10:0.1:10]; >> y1 = x.^2; >> y2 = 4*x; >> h1 = plot(x,y1);

%Assign first plot handle to ‘h1’

>> hold on >> h2 = plot(x,y2);

%Assign second plot handle to ‘h2’

>> delete(h2)

%Deletes the second plot handle. Thus, deleting the graph

>> h2 = plot(x,y2); >> set(h2,'Visible','off') %Hides the second plot handle using the ‘set’ function and turning the ‘Visible’ property off. a. 14. Solving Linear Equations a. Matrices X + 3y + 6z = 2 9x + 4y + 7z = 5 2x + 1y + 4z = 3 >> A = [1 3 6;9 4 7; 2 1 4]; >> s = [2;5;3]; >> A = [A s]; >> rref(A) ans = 1.0000 0 0 0.4118 0 1.0000 0 -1.1176 0 0 1.0000 0.8235 b. Plots >> x = [-10:0.01:10]; >> y = x.^2; >> plot(x,y); %Number of elements in x must match number of elements in y >> xlabel('Independent Variable'); >> ylabel('Dependent Variable'); >> title('Plot of y=x^2'); >> hold on %Adding multiple graphs to the same plot >> plot(x,4*x); >> hold off Note that looking at the intersection of the graphs is a solution

c. Functions >> A = [1 3 6;9 4 7; 2 1 4]; >> s = [2;5;3]; >> linsolve(A,s) ans = 0.4118 -1.1176 0.8235 15. Creating/running scripts and functions a. Scripts i. Show them the “new” tab ii. Create script with plotting function iii. Explain how to call scripts b. Functions i. Show them the “new” tab ii. Explain the output and input arguments iii. Create function which uses 2 inputs (x,y); iv. Create function which returns a resulting value from x and y, outputs z. 16. Saving Sessions a. Explain the “Save Workspace” button 17. Helpful homework functions (Compile a list and post online) a. General b. Signal Processing c. Electromagnetics 18. Rootfinding >> p = [1 -6 5]; %Equation: x^2-6x+5 = 0 >> r = roots(p) r= 5 1 19. Shading plots a. Using the “area” function >> x = [-10:0.1:10]; >> y2 = 4*x; >> area(x,y2)

%Plots the area under the curve ‘y2’

b. Using the “fill” function i. >> x = [-10:0.1:10]; >> y1 = x.^2; >> y2 = 4*x; >> X = [x fliplr(x)]; %Use the 'fliplr' function to flip 'x' vector from left to right. This creates a continuous x value array for plotting >> Y = [y1 fliplr(y2)]; %Create y values for out and then back. >> fill(X,Y,'b'); %plot the filled area

ii. What if you only want to fill graphs for a certain section only?

>> x = [-10:0.1:10]; >> y1 = x.^2; >> y2 = 4*x; >> index = 100:150; >> plot(x,y1,'b') >> hold on; >> h1 = fill(x(index([1 1:end end])),[0 y1(index) 0],'b'); %Plot a polygon that fills in sections from x[100] to x[150] from values 0 to y1(index)

20. Using Inequalites a. In MATLAB, inequalities are mainly used for checking Booleans using conditional operators (such as in an if/else statement or a for loop). b. Operators i. < ii. iv. >= v. ~= c. Symbolic inequalities can be set up using the “Symbolic Math Toolbox”. 21. Symbolic Variables a. Use the ‘sym’ function to create symbolic variables. MATLAB website list of functions: http://www.mathworks.com/help/matlab/functionlist.html...


Similar Free PDFs