You are on page 1of 31

177

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.

COMMANDS AND PROGRAMS


ARRAY& MATRIX REPRESENTATION

>> scalar = 7

scalar =

>> one_dimension_matrix =8

one_dimension_matrix =

>> row_vector = [3 4 5]

row_vector =

3 4 5

>> column_vector =[2;3;5]

column_vector =

>> square_matrix =[2 3 4;5 6 7;2 3 5]

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]

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 =[5 4 3]/2

division_by_scalar =

2.5000 2.0000 1.5000

>>

>> addition= [2 1 1]+[5 4 5]

addition =

7 5 6

>> subtraction=[6 5 3]-[2 8 1]

subtraction =

4 -3 2

COMPLEX NUMBER REPRESENTATION IN MATLAB

>> 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=[2 3 4].*[2 1 3]

M =

4 3 12

>> K=[1 2 3].^2

K =

1 4 9

>>

EXTRACTING ELEMENTS OF A MATRIX

>> A=[2 4 5;3 1 5; 5 8 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 =

>>

Changing elements of matrix

>> A(:,2)=[6 6 6]

A =

2 6 5

3 6 5

5 6 9

MATRIX OPERATIONS

Matrix multiplication

>> A=[1 2 3; 4 5 6; 5 4 3]*[3 4;5 6;1 2]

A =

16 22

43 58

38 50

186

187

SOLVING EQUATIONS USING MATLAB PROGRAMS

Solving A Set Of Equations Using Matlab Function

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]

A = 3 4

7 12

>> x= inv(A)*[5;13]

x =

1.0000

0.5000

SOLVING QUADRATIC EQUATIONS USING MATLAB

Using Matlab Function

eq='1*x^2-7*x+12=0';
s=solve(eq);

188

189

disp('firt root is'), disp(s(1))


disp('second root is'), disp(s(2))

Without Using Matlab Function


a=1;
b=-7;
c=12;
syms x
eq='a*x^2+b*x+c=0';
first_root = -(b + (b^2 - 4*a*c)^(1/2))/(2*a)
second_root = -(b - (b^2 - 4*a*c)^(1/2))/(2*a)

190

Plot function of a Vector


8

2
1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6

Plot Function To Plot A Matrix


10

2
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3

Plot An array of Complex Numbers Using MATLAB


8

-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:

To obtain different types of 2D and 3D PLOTS in MATLAB

PROGRAM:

Plot Function To Plot A Vector

x=[2 3 4 5 8 7];

plot(x)

Plot Function To Plot A Matrix

x=[2 3 4;5 6 7;8 9 10];

plot(x)

Plot An Array Of Complex Numbers Using MATLAB

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

Multiple Sinusoidal Signals


1
signal1
0.8 signal 2

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

Visualize A Single Function Using MATLAB Plot

x=0:pi/100:2*pi
y=sin(x)
plot(x,y)
title('sinusoidal signal')
ylabel('function values')
xlabel('time axis')

Create Multiple Plots In One Figure Window Using Plot Function

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

Multiple Sinusoidal Signals with line specifications


2

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

Plot Multiple Signals With Line Specifications

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

Multiple Signals Plotted In Same Window Using Subplot Function

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)')

subplot(2,1,2); % bottom subplot


plot(x,y2)
title('Bottom Subplot')
ylabel('sin(15x)')

196

subplt 2,2,1 subplot 2,2,2


1 1

0.5 0.5

sin(10x)
sin(5x)

0 0

-0.5 -0.5

-1 -1
0 1 2 3 0 1 2 3

Subplot 2,2,3 Subplot 2,2,4


6 5

4 5*sin(5*x)
5*sin(x)

0
2

0 -5
0 1 2 3 0 1 2 3

197

Subplot Function with Four Subplots in One Window

x = linspace(0,3);
y1 = sin(5*x);
y2 = sin(10*x);
y3=5*sin(x);
y4=5*sin(5*x)

subplot(2,2,1); % top subplot


plot(x,y1)
title('subplt 2,2,1')
ylabel('sin(5x)')

subplot(2,2,2); % bottom subplot


plot(x,y2)
title('subplot 2,2,2')
ylabel('sin(10x)')

subplot(2,2,3); % top subplot


plot(x,y3)
title('Subplot 2,2,3')
ylabel('5*sin(x)')

subplot(2,2,4); % bottom subplot


plot(x,y4)
title('Subplot 2,2,4')
ylabel('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

xa = -2:0.2:2; % range for x


ya = -2:0.2:2; % range for y
[x,y]=meshgrid(xa,ya); % returns
z = cos(x .* y); % evaluate at grid points
mesh(x,y,z)

200

0.5

-0.5
2
1 2
0 1
0
-1 -1
-2 -2

201

c) SURF PLOT

[x,y] = meshgrid([-2:.2:2]); % set up 2-D plane


Z = x.*exp(-x.^2-y.^2); % plot 3rd dimension on plane
figure
surf(x,y,Z,gradient(Z))

202

EXPERIMENT 2: GENERATION OF BASIC SIGNALS

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 step signal


n=-5:5;
a = [zeros(1,5),ones(1,6)];
subplot(3,3,6); stem(n,a); xlabel ('time');
ylabel ('amplitude');
title('Unit step');

% 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

Sort numbers stored in an External file using MATLAB


AIM
Sort numbers in ascending order and save to another text file using text read and sort
function after reading n floating point numbers from a formatted text file stored in the system.

PROGRAM

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)

You might also like