MATLAB Script Notes

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

===================================================================================

===================================================================================
=======
--------------------------------------------------------------MATLAB commands and
notes------------------------------------------------------------------------------
--------
===================================================================================
===================================================================================
=======

>>whos %gives variables in workspace and info about them


>>who %gives variables that are in your workspace

*matrix
>>A = [1 2 3 4] %creates a 1x4 matrix with data
>>B = [1,2,3,4] %also creates a 1x4 matrix with data
>>C = [5;6;7;8] %creates 4x1 matrix with data
>>D = [1 2 3 4; 5 6 7 8; 9 10 11 12] %creates a 4x3 matrix

>>x = A(1,2) %assigns element in row 1, column2 to x


>>y = A(2,:) %assigns elements in row 2 and all of its
column to y

>>0:2:10 %creates vector of elements from 0-10 with


spacing of 2

>>D*2 %multiplies the matrix D by scalar 2


>>D+6 %adds 6 to every element in matrix
>>A*B %does vector multiplication of the
matrices
>>A.*B %does element by element multiplication
of the matrices

*solving a linear equation


%solve: ==> Gives:
% 30 = 5x + 2y + z [30] [5 2 1] [x]
% 27 = x + y + 2z [27] = [1 1 2] [y]
% -1 = 2x - y [-1] [2 -1 0] [z]

>>A = [5 2 1; 1 1 2; 2 -1 0]
>>B = [30; 27; -1]
>>s = inv(A)*B %using inverse
%OR
>>s = A\B %using matrix division
%OR
>>augmented = [A B] %creates augmented matrix
>>reduced_augmented = rref(augmented) %reduces augmented matrix
>>s = reduced_augmented(: , 4) %gives answer to s

*complex numbers
>>V_magnitude = abs(V) %assigns the magnitude of V
>>V_angle = angle(V) %assigns the angle of V

*plots
>>plot (x,y) %plots x versus y. X AND Y MUST HAVE SAME
LENGTH.
>>title('Lab 1') %puts a title
>>xlabel('Voltage') %puts an x-axis title
>>ylabel('Current') %puts an y-axis title
>>axis([xmin, xmax, ymin, ymax]) %makes limit for the axis
>>grid %puts grid lines in graph

>>hold on %keeps the same graph


>>y=y+5 %creates a new plot

You might also like