Introduction To Matlab (Published .Mat File)
Introduction To Matlab (Published .Mat File)
Contents
Inroduction to MATLAB
Vectors
Matrices
Saving and loading data
Relational and logical operators
Plotting with MATLAB
Check the rest of the codes as a homework
Polynomials
Symbolic toolbox in MATLAB
Inroduction to MATLAB
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Error: File: E:\My Files\Courses\MEC709 - Fall 2010\Lab 1\Tutorial.m Line: 159 Column: 2
The expression to the left of the equals sign is not a valid target for an assignment.
Vectors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2+3 % summation
12/3 % divison
[1 2 3 4]
min(b) % minimum
c=[3 4 65 2 8 1]
pi
1 of 7 6/12/2010 12:59 PM
Tutorial file:///E:/My%20Files/Courses/MEC709%20-%20Fall%202010/Lab%2...
exp(a) % exponential
2*4 % multiplication
s3=7:-1:2 % step -1
% Complex numbers
c1=2+3i
c2=3+5j
c3=2-3*i
home % bring the commanf line to the top of the command window
% Strings
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Matrices
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% initializing matrices
A=[2 3 4;
2 4 5;
6 8 10]
B=[2,3,4;2,4,5;6,8,10]
C=[1 6 9; 2 5 1];
A*C % error: you can not multiply A(3x3) with C(3x2) matrix
2 of 7 6/12/2010 12:59 PM
Tutorial file:///E:/My%20Files/Courses/MEC709%20-%20Fall%202010/Lab%2...
A*inv(A)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2==3
a2=3
A>4
A==3
max(max(A))
%for loops
for i=1:5
v(i)=i^2
end
3 of 7 6/12/2010 12:59 PM
Tutorial file:///E:/My%20Files/Courses/MEC709%20-%20Fall%202010/Lab%2...
% if loops
% swaps the values of x and y if x>y
x=5,y=4
if x>y
temp=x;
x=y
y=temp
end
% nested loops
for i=1:3
for j=1:3
if sqrt(A(i,j))>=2 & A(i,j)<5
D(i,j)=A(i,j);
else
D(i,j)=sqrt(A(i,j));
end
end
end
D
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hold on, plot(x,x.^-2,'g--'),hold off; % adds another plot in the same figure
4 of 7 6/12/2010 12:59 PM
Tutorial file:///E:/My%20Files/Courses/MEC709%20-%20Fall%202010/Lab%2...
subplot(2,2,4),plot(x,exp(x))
% bar plots
x1=0:0.3:2
bar(x1,sin(x1))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Polynomials
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% division of polynomials
[Q,R]=deconv(P1,P3) % Q-quotient, R-remainder P1=P3*Q+R
% alternative way
s=tf('s')
sys3=4*(s-1)/(s^4-6*s^2+5)
zpk(sys3)
5 of 7 6/12/2010 12:59 PM
Tutorial file:///E:/My%20Files/Courses/MEC709%20-%20Fall%202010/Lab%2...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
diff(sin(x)+(x^2/4)-tan(x/3))
% integration
int(x) % indefinite integral
% definite integral
int('x+2',-2,6) % integral of (x+2)dx from -2 to 6
% laplace transformation
syms t % define a symbolic variable
f4=exp(-2*t) % define a function e^(-2t)
F4=laplace(f4) % find the solution
pretty(F4) % display the solution in a 'pretty' way (fractions)
ezplot(f4) % plot the function f4
% another example
f5=exp(-t)-2*t*exp(-2*t)-2*exp(-2*t)
F5=laplace(f5)
pretty(F5)
6 of 7 6/12/2010 12:59 PM
Tutorial file:///E:/My%20Files/Courses/MEC709%20-%20Fall%202010/Lab%2...
7 of 7 6/12/2010 12:59 PM