Académique Documents
Professionnel Documents
Culture Documents
Introduction
MATLAB: MATrix LABoratoy. Designed for matrix computations With a variety of graphical capabilities It has its own programming language, similar to C MATLAB is not a symbolic computation system (SCS) such as Mathematica: this does not make it better or worse, it is a tool designed for different tasks and is therefore not directly comparable
MATLAB ENVIRONMENT
Command Window Workspace
Save workspace option
Arithmetic operators
A+B & A-B: A and B must have the same size Matrix multiplication: C=A*B Array multiplication: D=A.*B is the element by element product of the arrays A and B. A & B must have the same size Matrix right division: B/A = B*inv(A) Array right division: A./B = A(i,j)/B(i,j) Matrix left division: A\B = inv(A)*B Array left division: A.\B = B(i,j)/A(i,j) Some miscellaneous commands
Graphics:
GRAPHS
Creating a Plot Figure Windows Adding plots to an existing graph Exercise1.m: plot for 0.01t 0.01, with time intervals of 1e-7 sec.
p (t ) = Z I 2 cos Z + Z I 2 cos( 2t + Z )
Z= I=10, Z = 45, =2f, f = 60 Hz. Find maximum & minimum value of p(t). 9 In the same graph, plot ZI2cos(2 t + Z) (hold on) 9 Plot both in different graphs. (figure)
Programming in MATLAB
Flow control:
If Switch and case For While Break: lets you exit early from a for or while loop.
Programming in MATLAB
Scripts: When you invoke a script, MATLAB simply executes the commands found in the file. Although scripts do not return output arguments, any variables that they create remain in the workspace, to be used in subsequent computations. In addition, scripts can produce graphical output using functions like plot. For example, create a file called magicrank.m that contains these MATLAB commands: % Investigate the rank of magic squares r = zeros(1,32); for n = 3:32 r(n) = rank(magic(n)); end r bar(r)
Programming in MATLAB
Functions: are M-files that can accept input arguments and return output arguments. The name of the M-file and of the function should be the same. Syntax:
function [output1, output2,] = function-name(input1,input2,)
For example, create a function root2.m to calculate the roots of the quadratic polynomial ax2+bx+c=0 using:
b b 2 + 4ac x1 , x2 = 2a