introduction to matlab - · pdf fileintroduction to matlab am 581 computational laboratory...
TRANSCRIPT
Introduction to MATLAB
AM 581 Computational Laboratory
Department of Applied Mechanics, IIT Madras
The language of technical computing
MATLAB: technical computing language & interactive environment for
•Algorithm development (writing programs)
•Data visualization (graphs, figures etc)
•Data analysis
•Numerical computations
MATLAB 7.1.lnkClick on the icon
Workspace
Command HistoryCommand window
Path to current directory
To quit type at the prompt
>> exit
or
>> quit
Scalars
(1) Real Numbers (default)
>> 3.1416
(2) Complex Numbers>> 3 + 4*j
>> 3 + 4*i
>> complex(3,4)
• Other Types– strings
>>b = ‘Hello’
– Syms (symbolic expressions)>> syms x alpha real
By default, all computations are in double precisions
>> format long
>> pi
ans =
3.14159265358979
>> format short
>> pi
ans =
3.1416
Arithmetic Operations
• Arithmetic operations (+,-,*,/)>>7/45>>(1+i)*(2+i)
• Exponentiation (^ )>>4^2>>(3+4*j)^2
• Complicated expressions, use parentheses>>((2+3)*3)/10.3
• Multiplication is NOT implicit given parentheses>>3(1+0.7) gives an error
Logical operations
>> a == b, a >= b EQUAL, GREATER OR EQUAL
>> a ~= b, a ~>b NOT EQUAL, NOT GREATER
>> a & b a AND b
>> a | b a OR b
>> a | ~ b a AND NOT b
Answer is either 0 or 1
0 is FALSE
1 is TRUE
Scalar variables• If not assigned, value stored in ans
>>0.5*73
• is the same as>>ans = 0.5*73;
• Once assigned, can use variables >>a=14.5; b=3.7; >>c=a^2-2*b;
• If a variable is not in the environment, an error is returned>>c = A + b gives an error
• variable names are case-sensitive• names must start with a letter
Unlike in Fortran/C, not required to define all variables at the start of the program
Vectors & Matrices
• Row vectors
>> a=[1 2 3 4];
• Column vectors
>> b=[1;2;3;4];
•Matrix (3 x 2)
>> c = [ 1 2*i; 3 4; 5*j 6];
This is equivalent to
>> c = [ 1 2*i;
3 4;
5*j 6];
Vectors & Matrices>> a = linspace(1,10,4)
This is a row vector a = [1 4 7 10 ]
>> b = eye(2)
This is a 2 x 2 identity matrix
>> c = ones(3,4)
>> d = zeros(3,4)
c and d are 3 x 4 matrices whose all elements are unity and zero respectivelyThe general function call is:
var=zeros( M, N);Number of rows Number of columns
Basic matrix operations
• arithmetic operations
>> a+b >> a-b
>> a*b
>> a*inv(b) is the same as
>> a/b
>> size(a) returns the size of matrix a
>> length(a) returns the length if a is a vector
If a is a matrix, this is equivalent to
>> max(size(a))
Matrices a and b should be of equal dimemsions
Element-wise Operations
• All the scalar operators work element-wise also >>t = [1 2 3];
>>f = exp(t);
• is the same as>>f = [exp(1) exp(2) exp(3)];
>> a = [ 1 2; 3 4];
>> a+2
ans =
3 4
5 6
>> a=[1 2;3 4];>> b=[5 6;7 8]; >> c3 = a.*bc3 =
5 1221 32
>> c4 = a./bc4 =
0.2000 0.33330.4286 0.5000
>> size(a)ans =
2 2>> length(b)ans = 2
Matrices a and b should be of equal dimemsions
a = 1 2 b = 5 6
3 4 7 8
Graphics
>> t=linspace(0,10,100);
>> y = sin(pi*t);
>> plot(t,y)
>> y2 =cos(pi*t);
>> hold on
>> plot(t,y2,’r--’)
>>t=0:.01:2*pi;>>polar(t,abs(sin(2*t).*cos(2*t)));
>>t=linspace(0,10,1000);>>y=sin(pi*t);>>z=pi*cos(pi*t);>>plot3(t,y,z);>>xlabel(‘time, t’)>>ylabel(‘displacement’)>>zlabel(‘velocity’)
Plotting a function of two independent variables
z=exp(-x^2-y^2)>> x=linspace(-2,2,10);>> y=linspace(-2,2,20);>> for k1 = 1:length(y)
z(k1,:) = exp(-x.^2-y(k1)^2);end
>> surf(x,y,z)>> xlabel(‘x’)>> ylabel(‘y’)>> zlabel(‘z’)>> size(x) [1 10]>> size(y) [1 20]>> size(z) [20 10]
MATLAB programs (m files)
Script files function files• sequence of MATLAB statements
• Create new functions in addition to already existing MATLAB functions
• Can be called directly in script files
% This is a test program
clear all;
x=-2:0.2:2;
y = -3:0.2:3;
for k1 = 1:length(y)
z(k1,:) = exp(-x.^2+y(k1)^2);
end
figure
surf(x,y,z)
function [mean, stdev] = statistics(x);% returns the mean and standard deviation
n = length(x);mean = sum(x)/n;stdev = sqrt(sum(x-mean).^2)/n;
>> x = rand(1,1000);
>> [mean_x, std_x] = statistics(x);
>> help statistics
returns mean and standard deviation
[out1,out2,…,out_n] = function (arg1,arg2,.., arg_n)
Function files
for <condition>
program
end
for j = 1,100
a(j) = j;
end
if <condition1>program1
elseif <condition2>program2
end
LOOPS
if a > b
c = exp(a-b);
elseif a < b
c = exp(b-a);
else
c = 0;
end
while k < 100
a(k) = exp(-k);
k = k + 1;
end
Writing programs in MATLAB
1. Save the program, for example, as “first.m”
2. Change the working directory to where the program is saved.
3. At the prompt, type
>> first
Some useful commands>> clear c lears the workspace
>> clc clears a cluttered screen
>> cd ../otherdirectory change directory
>> ls displays files in directory
>> who displays variables in workspace
>> whos displays sizes of variables
>> save work.mat saves all the variables in binary
>> load work loads all the variables in workpace
>> more on
>> why Try this !!! Its fun!!!
Demos and online help
Visit the URL www.mathworks.com
academia
tutorial
file exchange
>> demo
>> demo matlab graphics
>> helpwin
>> help plot
>> lookfor plot