Professional Documents
Culture Documents
INTRODUCTION TO
MATLAB PROGRAMS
178
179
Expt. No:
Date:
EXPERIMENT NO.4.1
Solving mathematical equations using MATLAB
AIM
Solve /plot the mathematical equations containing complex numbers, array, matrix
multiplication and quadratic equations etc.
>> scalar = 7
scalar =
>> one_dimension_matrix =8
one_dimension_matrix =
>> row_vector = [3 4 5]
row_vector =
3 4 5
column_vector =
square_matrix =
2 3 4
5 6 7
2 3 5
180
181
two_by_4_matrix =
1 2 3 4
4 5 6 7
>> x=[1:4]
x =
1 2 3 4
>> W=[2:2:8]
W =
2 4 6 8
ARRAY OPERATIONS
>> multiplication_by_scalar=2* [5 6 7]
multiplication_by_scalar = 10 12 14
division_by_scalar =
>>
addition =
7 5 6
subtraction =
4 -3 2
>> x=4+3*i
x = 4.0000 + 3.0000i
182
183
>> A=real(x)
A = 4
>> B=imag(x)
B = 3
>> C=abs(x)
C = 5
ELEMENTAL OPERATIONS
>> P=[2,3,4].*2
P =
4 6 8
M =
4 3 12
K =
1 4 9
>>
A = 2 4 5
3 1 5
5 8 9
184
185
>> A(1,:)
ans =
2 4 5
>> A(:,2)
ans =
>> A(2,3)
ans =
>>
>> A(:,2)=[6 6 6]
A =
2 6 5
3 6 5
5 6 9
MATRIX OPERATIONS
Matrix multiplication
A =
16 22
43 58
38 50
186
187
Eq1='5*X+9*Y=5'
Eq2='3*X-6*Y=4'
S=Solve('5*X+9*Y=5','3*X-6*Y=4')
S.X
S.Y
Equations using matrix multiplication method
3x+4y=5
7x+12y=13
3 4 x 5
This equation can be written in Matrix form as
7 12 y 13
Ax=B
X=A-1B
A = 3 4
7 12
>> x= inv(A)*[5;13]
x =
1.0000
0.5000
eq='1*x^2-7*x+12=0';
s=solve(eq);
188
189
190
2
1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6
2
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3
-1
-2
-7 -6 -5 -4 -3 -2 -1 0 1 2
191
Expt. No:
Date:
EXPERIMENT NO.4.2
DIFFERENT TYPES OF PLOTS IN MATLAB
AIM:
PROGRAM:
x=[2 3 4 5 8 7];
plot(x)
plot(x)
a=[2+2*i,-3-2*i,-7+8*i,1-i]
plot(a)
192
sinusoidal signal
1
0.8
0.6
0.4
function values
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
time axis
0.6
0.4
Function values
0.2
-0.2
-0.4
-0.6
-0.8
-1
-8 -6 -4 -2 0 2 4 6 8
Time axis
193
x=0:pi/100:2*pi
y=sin(x)
plot(x,y)
title('sinusoidal signal')
ylabel('function values')
xlabel('time axis')
x = linspace(-2*pi,2*pi);
y1 = sin(x);
y2 = cos(x);
figure
plot(x,y1,x,y2)
plot(x,y1, 'blue', x, y2, 'red')
title('Multiple Sinusoidal Signals')
ylabel('Function values')
xlabel('Time axis')
legend('signal1', 'signal 2')
legend('Location','NorthWest')
194
1.5
0.5
Function values
-0.5
-1
signal1
-1.5
signal 2
data3
-2
0 1 2 3 4 5 6 7
Time axis
Top Subplot
1
0.5
sin(5x)
-0.5
-1
0 0.5 1 1.5 2 2.5 3
Bottom Subplot
1
0.5
sin(15x)
-0.5
-1
0 0.5 1 1.5 2 2.5 3
195
x = 0:pi/100:2*pi;
y1 = sin(x);
y2 = sin(2*x);
y3 = 2*sin(x);
figure
plot(x,y1,'c*',x,y2,'r--',x,y3,':m')
title('Multiple Sinusoidal Signals with line specifications')
ylabel('Function values')
xlabel('Time axis')
legend('signal1', 'signal 2')
legend('Location','SouthWest') % move legend to upper left
x = linspace(0,3);
y1 = sin(5*x);
y2 = sin(15*x);
subplot(2,1,1); % top subplot
plot(x,y1)
title('Top Subplot')
ylabel('sin(5x)')
196
0.5 0.5
sin(10x)
sin(5x)
0 0
-0.5 -0.5
-1 -1
0 1 2 3 0 1 2 3
4 5*sin(5*x)
5*sin(x)
0
2
0 -5
0 1 2 3 0 1 2 3
197
x = linspace(0,3);
y1 = sin(5*x);
y2 = sin(10*x);
y3=5*sin(x);
y4=5*sin(5*x)
198
helical structure
0.5
sin(x)
-0.5
-1
1
0.5 40
0 30
20
-0.5 10
cos(x) -1 0
angle
0.5
-0.5
-1
2
1 2
0 1
0
-1 -1
-2 -2
199
3 DIMENSIONAL PLOTTING
a) 3D line plots
x=linspace(0,10*pi,1000);
y=cos(x)
z=sin(x)
plot3(x,y,z)
%comet3(x,y,z)
grid
xlabel('angle')
ylabel('cos(x)')
zlabel('sin(x)')
title('helical structure ')
b) MESH
200
0.5
-0.5
2
1 2
0 1
0
-1 -1
-2 -2
201
c) SURF PLOT
202
203
Expt. No:
Date:
EXPERIMENT NO.4.3
GENERATION OF BASIC SIGNALS USING
MATLAB
AIM: To generate signals, Sine, cosine, square, exponential, saw tooth, unit
step and unit impulse using MATLAB functions.
PROGRAM
clc;
clearall;
closeall;
% sine wave
t=0:0.01:1; a=2;
b=a*sin(2*pi*2*t); subplot(3,3,1); stem(t,b); xlabel('time');
ylabel('Amplitude'); title ('sinewave');
% Cosine wave
t=0:0.01:1;
a=2; b=a*cos(2*pi*2*t); subplot(3,3,2); stem(t,b);
xlabel('time'); ylabel('Amplitude'); title ('Cos wave');
% Square wave
t=0:0.01:1;
a=2; b=a*square(2*pi*2*t); subplot(3,3,3); stem(t,b);
xlabel('time'); ylabel('Amplitude'); title ('square wave');
204
205
% Exponential waveform
t=0:0.01:1;
a=2; b=a*exp(2*pi*2*t); subplot(3,3,4); stem(t,b);
xlabel('time'); ylabel('Amplitude');
title ('exponential wave');
%sawtooth
t=0:0.01:1; a=2;
b=a*sawtooth(2*pi*2*t);
subplot(3,3,5); stem(t,b); xlabel('time');
ylabel('Amplitude'); title ('sawtooth wave');
% unit impulse
n=-5:5;
a = [zeros(1,5),ones(1,1),zeros(1,5)];
subplot(3,3,7); stem(n,a); xlabel ('time');
ylabel ('amplitude');
title('Unit impulse');
206
207
Expt. No:
Date:
EXPERIMENT NO 4.4
clc
clear all;
fid=fopen('new.txt','r')
data=textscan(fid,'%5d')
k=data{1}
q=length(k)
for z=1:q-1
for n=1:q-1
if k(n)>k(n+1)
temp=k(n)
k(n)=k(n+1)
k(n+1)=temp
else
end
end
end
fid=fopen('new.txt','w')
count=fprintf(fid,'%5d',k)
fclose(fid)