You are on page 1of 13

W.N.L.

Weerakkody
100573H

PART A
Question 1

Last digit of the index number = x = 3
>> A=[3 6 9 12 15; 6 9 1 1 1; 9 1 9 1 1; 12 1 1 9 1; 15 1 1 1 3]

A =

3 6 9 12 15
6 9 1 1 1
9 1 9 1 1
12 1 1 9 1
15 1 1 1 3

a).

>> B = A*(A+A')

B =

990 216 282 348 234
216 240 148 184 208
282 148 330 256 298
348 184 256 456 388
234 208 298 388 474

>> det(A)

ans =

-151776

>> inv(A)

ans =

-0.0127 0.0003 0.0051 0.0098 0.0582
0.0003 0.1167 -0.0085 -0.0086 -0.0348
0.0051 -0.0085 0.1146 -0.0123 -0.0566
0.0098 -0.0086 -0.0123 0.1091 -0.0784
0.0582 -0.0348 -0.0566 -0.0784 0.0990

b).

>> C = [A A+1; A+1 2*A]

C =

3 6 9 12 15 4 7 10 13 16
6 9 1 1 1 7 10 2 2 2
9 1 9 1 1 10 2 10 2 2
12 1 1 9 1 13 2 2 10 2
15 1 1 1 3 16 2 2 2 4
4 7 10 13 16 6 12 18 24 30
7 10 2 2 2 12 18 2 2 2
10 2 10 2 2 18 2 18 2 2
13 2 2 10 2 24 2 2 18 2
16 2 2 2 4 30 2 2 2 6

Question 2

>> b=ones(5,1)

b =

1
1
1
1
1

>> X=linsolve(A,(-b))

X =

-0.0607
-0.0651
-0.0424
-0.0196
0.0127

Question 3

a).
H is an order 5 Hilbert matrix.
>> H=hilb(5)

H =

1.0000 0.5000 0.3333 0.2500 0.2000
0.5000 0.3333 0.2500 0.2000 0.1667
0.3333 0.2500 0.2000 0.1667 0.1429
0.2500 0.2000 0.1667 0.1429 0.1250
0.2000 0.1667 0.1429 0.1250 0.1111

>> y=[1 2 2 2 2; 0 1 2 2 2; 0 0 1 2 2; 0 0 0 1 2; 0 0 0 0 1]

y =

1 2 2 2 2
0 1 2 2 2
0 0 1 2 2
0 0 0 1 2
0 0 0 0 1

>> Y=y+H^5+H^4+H^3+H^2+H
>> Y=y+H^5+H^4+H^3+H^2+H

Y =

14.8635 9.9475 7.6982 6.4745 5.6955
7.9475 5.6666 5.3793 4.6678 4.2107
5.6982 3.3793 3.4626 3.9524 3.6227
4.4745 2.6678 1.9524 2.5529 3.2939
3.6955 2.2107 1.6227 1.2939 2.0803

b). (i).

>> Y(5,4)

ans =

1.2939

b). (ii).

>> Y(8,9)
??? Index exceeds matrix dimensions.







PART B
Question 4

Part of code Meaning

k=5;

Assign integer value 5 to variable k

n=2^k -1;

Assign integer value 2
k
- 1 to variable n



theta=pi*(-n:2:n)/n;


A row vector theta is assigned with values in the series pi/n into
values in the series from (-n) to n with a gap of 2.



phi=(pi/2)*(-n:2:n)/n;


A row vector phi is assigned with values in the series pi/2 into
values in the series from (-n) to n with a gap of 2.



X=[cos(phi)]'*[cos(theta)];


Transpose matrix of cosine values of the phi matrix multiplied by
the matrix of the cosine values of theta matrix. It is assigned to a
matrix X.



Y=[cos(phi)]'*[sin(theta)];


Transpose matrix of cosine values of the phi matrix multiplied by
the matrix of the sine values of theta matrix. It is assigned to a
matrix Y.


Z=[sin(phi)]'*ones(size(theta));


Transpose of sine values of the phi matrix multiplied by a matrix
of 1s of the size of theta matrix. It is assigned to a matrix Z


colormap([0 0 0;1 1 1]);


A color map with two colors for the plot. First row of RGB vector
comprises of 0s (black). The other row of the RGB vector consists of 1s
(white).


C=hadamard(2^k);

Assign Hadamard matrix of order 2
k
to matrix C.


surf(X,Y,Z,C);


A 3D shaded surface from the components of matrices X, Y and
Z. The color variation is defined by the values in matrix C.


axis square;


Create the current axes region square.


Question 5

>> x=[0 0.8 1.4 1.8 2.0 2.1];
>> y=polar(sin(x),cos(x),'r');
>> title('100573H - Nipuna Weerakkody');


Question 6

Part of code Meaning

th=(0:127)/128*2*pi;


Returns a row matrix with 128 columns.


x=cos(th);y=sin(th);


Return two row matrices with sine and cosine values of values of matrix th.

f=abs(fft(ones(5,1),128));


128 points discrete Fourier transform is done on a matrix of all 1s of the size
5x1. Assign the absolute value to a row vector f.

stem3(x,y,f','d','fill');

Plot a 3D stem plot from the x, y values from row vectors x and y.
stem3(X,Y,Z) plots the surface Z at the values specified in X and Y.


view([-65 30])


Sets the viewpoint to be 65 in the x-y plane with clockwise direction with an
elevation of an angle 30 above the x-y plane.



Output:

Question 7

Index Number = 100573H
Last two digits of the Index number = 73
Hence,

f(x) = 73x
3
sin(x) + x
2
+ 73

>> f='73*x^3*sin(x)+x^2+73'

f =

73*x^3*sin(x)+x^2+73

>> ezplot(f,[-30,30]);
>> grid on


By using function fzero to find roots,

>> fzero(f,[-30,-27.5])

ans =

-28.2749

>> fzero(f,[-27.5,-25])

ans =

-25.1321

>> fzero(f,[-25,-20])

ans =

-21.9919

>> fzero(f,[-20,-17.5])

ans =

-18.8487

>> fzero(f,[-17.5,-15])

ans =

-15.7091

>> fzero(f,[-15,-10])

ans =

-12.5648

>> fzero(f,[-10,-7.5])

ans =

-9.4274

>> fzero(f,[-7.5,-5])

ans =

-6.2770

>> fzero(f,[-5,-2.5])

ans =

-3.1771

>> fzero(f,[0,5])

ans =

3.1771

>> fzero(f,[2.5,5])

ans =

3.1771

>> fzero(f,[5,7.5])

ans =

6.2770

>> fzero(f,[7.5,10])

ans =

9.4274

>> fzero(f,[12.5,15])

ans =

12.5648

>> fzero(f,[15,17.5])

ans =

15.7091

>> fzero(f,[17.5,20])

ans =

18.8487

>> fzero(f,[20,22.5])

ans =

21.9919

>> fzero(f,[25,27.5])

ans =

25.1321

>> fzero(f,[27.5,30])

ans =

28.2749
Roots = -28.2749, -25.1321, -21.9919, -18.8487, -15.7091, -12.5648, -9.4274, -6.2770, -3.1771, 3.1771, 6.2770,
9.4274, 12.5648, 15.7091, 18.8487, 21.9919, 25.1321, 28.2749

PART C

Question 8

>> x=-2.5:0.5:2.5;
>> y=(cos(10.*x).^2).*exp(-x.^2);
>> trapz(x,y)

ans =

0.8865

Accuracy can be increased by sub dividing the intervals of x into smaller portions.
>> x=-2.5:0.1:2.5;
>> y=(cos(10.*x).^2).*exp(-x.^2);
>> trapz(x,y)

ans =

0.8858

>> x=-2.50:0.01:2.50;
>> y=(cos(10.*x).^2).*exp(-x.^2);
>> trapz(x,y)

ans =

0.8858


Question 9

The code to create a data file.

>> fid = fopen('Nipuna_Weerakkody_100573H.dat','w');
>> ind = 1;
>> for x=-5:0.05:5
f=(cos(10.*x).^2).*exp(-x.^2);
fprintf(fid,'%d %f %f\n', ind,x,f);
ind=ind+1;
end
>> fclose(fid)

ans =

0

The code to load the data file.

>> load Nipuna_Weerakkody_100573H.dat;
>> Z = Nipuna_Weerakkody_100573H;
>> Z1 = Z;
>> Z1(:,1)=[];
>> X=Z1(:,2);
>> X1=X';
>> Y=Z1(:,1);
>> Y1=Y';
>> plot(Y1,X1);
>> grid on



Question 10

Code to plot the graph.

>> [x1,x2]=meshgrid(-1:0.05:1);
>> f=20+x1.^2+x2.^2+10.*(cos(2.*pi.*x1)+cos(2.*pi.*x2));
>> mesh(x1,x2,f);
>> zlabel('Function Value');
>> grid on




Code for global minima.

>> Function_minima=50;
>> for i=1:10000
x1=2*rand()-1;
x2=2*rand(1)-1;
f=20+x1.^2+x2.^2+10.*(cos(2.*pi.*x1)+cos(2.*pi.*x2));
if f<=Function_minima
Function_minima=f;
min_x1=x1;
min_x2=x2;
end
end
>> fprintf('Global minima=%f\n',Function_minima);
Global minima=0.503842
>> fprintf('Corresponding (x1,x2) at the global minima=(%f,%f)\n',min_x1,min_x2);
Corresponding (x1,x2) at the global minima=(-0.502161,-0.494293)





Question 11

Part of the code Meaning

r =zeros(1,32);


132 row matrix named r. All elements are zeros.


for n =3:32


Iterates the block of commands mentioned below from n=3 to n=32 times.



r(n) =rank(magic(n));


Creating an nn matrix out of the integers from 1 to nn whose numbers in
each row, and in each column, and the numbers in the forward and backward
main diagonals, all add up to the same number (magic matrix) and assign the
rank of that magic matrix to the n
th
element of matrix r.


end


End of the iteration of the block of commands i.e. after the 32
nd
iteration. The
final output will be a matrix whose first two elements are zero while others
being the rank of magic square produced at each iteration mentioned above.


bar(r);

Draw a bar chart for each element in r.





EE4073




Computer Aided Design and Simulation
Assignment 1
MATLAB BASICS

W.N.L. Weerakkody
100573H
Department of Electrical Engineering
University of Moratuwa

You might also like