Introduction To Matlab: Muhammad Abdullah
Introduction To Matlab: Muhammad Abdullah
Processing Lab
Introduction to Matlab
Muhammad Abdullah
UNIVERSITY OF ENGINEERING AND TECHNOLOGY KSK
LAHORE
>> %Lab Task
>> 3+7.5
ans =
10.5000
>> 18/4
ans =
4.5000
>> 3*7
ans =
21
>> 3^2
ans =
9
%Example 1:
>> a=5;x=2;y=8
>> y=exp(-a)*sin(x)+10*sqrt(y)
y=
28.2904
>>
>> %Example 2:
>> sin(pi/4)
ans =
0.7071
>> exp(10)
ans =
2.2026e+04
>> A=[1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
>> A=[1,2,3;4,5,6;7,8,9];
>> A=[1,2,3;4,5,6;7,8,9]
A=
1 2 3
4 5 6
7 8 9
>>
>> a=[
123
456
7 8 9]
a=
1 2 3
4 5 6
7 8 9
>>
>> b=a'
b=
1 4 7
2 5 8
3 6 9
>>
>> a=[1 2 3 4 ; 5 6 7 8]
a=
1 2 3 4
5 6 7 8
>> a(:,:)
ans =
1 2 3 4
5 6 7 8
>> a(1,2)
ans =
>> a(1,:)
ans =
1 2 3 4
>> a(:,[1,3])
ans =
1 3
5 7
4
8
>> b=a(:,3:end)
b=
3 4
7 8
>> diag(b)
ans =
3
8
a=
1 2 3 4 5
>> a(2)=6
a=
1 6 3 4 5
a=
0 6 0 4 5
a=
1 3 4
5 7 8
>> A'
ans =
1 4 7
2 5 8
3 6 9
>> Flipr(a)
Undefined function 'Flipr' for input arguments of type 'double'.
ans =
4 3 1
8 7 5
ans =
5 7 8
1 3 4
>>%basic plotting
>> x=[1 2 3 4 5 6];
>> y=[3 -1 2 4 5 1];
>> plot(x,y)
>>
>>legend('2*cos(x)','cos(x)','0.5*cos(x)')
>> axis([0 2*pi -3 3])
>> %This is a sample m-file
>> a=[1,2,3;0,1,1;1,2,3]
a=
1 2 3
0 1 1
1 2 3
>> b=a';
>> c=a+b
c=
2 2 4
2 2 3
4 3 6
>> d=inv(c)
d=
-1.500000000000000 0 1.000000000000000
0 2.000000000000000 -1.000000000000000
1.000000000000000 -1.000000000000000 0
>>
run the matlab as administrator
%This is a sample m-file
run the matlab as administrator
Home>new>function then past the code
function y = prod(a,b)
y=a*b;
save with the name as prod.m
>> prod enter
ans =
>>
run the matlab as administrator
Home>new>function then past the code
%This is a sample m-file
a=[1,2,3;0,1,1;1,2,3]
b=a';
c=a+b
d=inv(c)
save with the name as rkg.m
>> rkg enter
a=
1 2 3
0 1 1
1 2 3
c=
2 2 4
2 2 3
4 3 6
d=
-1.5000 0 1.0000
0 2.0000 -1.0000
1.0000 -1.0000 0
x=
-5.0000 + 9.0000i
>> y = 6 -2*i
y=
6.0000 - 2.0000i
>> x-y
ans =
-11.0000 +11.0000i
>>
>> x*y
ans =
-12.0000 +64.0000i
>> x/y
ans =
-1.2000 + 1.1000i
>>%Q #2
>> 6*(35^1.4)+14^0.35
ans =
873.1743
>>%Q #3
>>
x=[1:1.5/1000:1.4999];
>> y = 4*(sqrt((6*x) +
1));
>> z = 5*(exp(0.3*x))-
(2*x);
>> plot(x,y,'--',x,z,'-- ')
>> xlabel('0< x< 1.5')
>> ylabel('Algebric
functions')
>>
legend('4*(sqrt((6*x) +
1))','5*(exp(0.3*x))-
(2*x)')
>> axis([1 1.5 4 15])
>>%Q #4
>> t=[1:5/1000:4.9999];
>> s=2*sin(3*t+2)+sqrt(5*t+1);
>> plot(t,s,'--')
>> xlabel('0< t< 5')
>> ylabel('Trignometric functions')
>> legend('2*sin(3*t+2)+sqrt(5*t+1)')
>> axis([1 5 0 8])
>>%Q #5
>> x=[0:pi/1000:2*pi];
>> y=[exp(-x)].*sin(8*x);
>> plot(x,y,'--')
>> xlabel('0\leq x \leq 2\pi')
>> ylabel('Exponential and
Trigonometric function')
>> legend('[exp(-
x)].*sin(8*x)')
>> axis([0 2*pi -0.6 1])
>>
>>%Q #6
>> x=[-pi:pi/1000:pi];
>> y=(4/pi)*[(sin(x))+(sin(3*x)/3)+(sin(5*x)/5)+(sin(7*x)/7)];
>> plot(x,y,'--')
>> xlabel('-pi< x<pi')
>> ylabel('Forier Series')
>> legend('(4/pi)*[(sin(x))+(sin(3*x)/3)+(sin(5*x)/5)+(sin(7*x)/7)]')
>>axis([-4 4 -1.5 1.5]
>>%Q #7
>> a= [1 1 1;0 0 0;0 0 0;0 0 0]
a=
1 1 1
0 0 0
0 0 0
0 0 0
>>%Q#8
%Solve set of equations
>> syms x
>> syms y
>> syms y
>> eqn1= 6*x-4*y+8*z==112;
>> eqn2= -5*x-3*y+7*z==75;
>> eqn3= 14*x+9*y-5*z==-67;
>> [a,b]=equationsToMatrix([eqn1,eqn2,eqn3],[x,y,z]);
>> x=linsolve(a,b)
x=
2
-5
10
>>