Вы находитесь на странице: 1из 14

EE4072 - Computer Aided Design & Simulation

# Assignment 1

Name: S. B. Nawarathne Index No: 090594D

1 Part A
Q1.
>> x = 4
x =
4
>> A = [x 2.*x 3.*x 4.*x 5.*x; 2.*x x.*x 1 1 1; 3.*x 1 x.*x 1 1; 4.*x 1 1 x.*x 1;
5.*x 1 1 1 x]
A =
4
8
12
16
20
8
16
1
1
1
12
1
16
1
1
16
1
1
16
1
20
1
1
1
4
a. >> A.*(A + A')
ans =
32
128
288
512
800
128
512
2
2
2
288
2
512
2
2
512
2
2
512
2
800
2
2
2
32
>> det(A)
ans =
-1.6558e+06
>> inv(A)
ans =
-0.0094
0.0013
0.0038
0.0063
0.0440
0.0013
0.0636
-0.0034
-0.0038
-0.0206
0.0038
-0.0034
0.0622
-0.0055
-0.0324
0.0063
-0.0038
-0.0055
0.0595
-0.0441
0.0440
-0.0206
-0.0324
-0.0441
0.0541
2 b. >> [A A+1; A+1 2.*A]
ans =
4
8
12
16
20
5
9
13
17
21
8
16
1
1
1
9
17
2
2
2
12
1
16
1
1
13
2
17
2
2
16
1
1
16
1
17
2
2
17
2
20
1
1
1
4
21
2
2
2
5
5
9
13
17
21
8
16
24
32
40
9
17
2
2
2
16
32
2
2
2
13
2
17
2
2
24
2
32
2
2
17
2
2
17
2
32
2
2
32
2
21
2
2
2
5
40
2
2
2
8
Q2.
>> syms z1 z2 z3 z4 z5
>> X = [z1; z2; z3; z4; z5]
X =
z1
z2
z3
z4
z5
>> b = [1; 1; 1; 1; 1]
b =
1
1
1
1
1
3

>> linsolve(a,-b) ans =

-0.0461

-0.0371

-0.0248

-0.0125

-0.0011

Q3.

• a. Assuming H refers to the 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] + 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 = 0.1429 ii. >> Y(8,9)

Index exceeds matrix dimensions.

4

### Part B

Q4.

 k=5; % give variable k, a value of 5 n=2^k -1; % n = 31

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

3.1416 to 3.1416

% creates 32 values for variable theta ranging from -

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

to 1.5708

% creates 32 values for variable phi ranging from -1.5708

X=[cos(phi)]'*[cos(theta)]; %transpose of cos(phi) array multiplied with cos(theta), creates 1024(32x32) values and assigns to X

Y=[cos(phi)]'*[sin(theta)]; %transpose of cos(phi) array multiplied with sin(theta), creates 1024(32x32) values and assigns to Y

Z=[sin(phi)]'*ones(size(theta)); % ones(size(theta)) creates an array of 1s of size 32, and multiplies it with transpose of sin(phi) to assign to variable Z

colormap([0 0 0;1 1 1]); %colours the surface based on a 3x2 matrix, the values for each row define the intensity of colours; red, green and blue. In this case first row defines black and second row defines white

C=hadamard(2^k); % creates a 32x32 matrix of 1s and -1s whose columns are orthogonal

surf(X,Y,Z,C); % draws a surface based on the values of X,Y,Z, colours the surface based on a linear transformation of the matrix, C and the colormap, for this reason there is a unique shaded pattern on the surface.

axis square; % arranges all 3 axes in equal length, gives cube like structure to the plot region.

5

Q5.

>> x = [0 0.8 1.4 1.8 2.0]' x =

0

0.8000

1.4000

1.8000

2.0000

>> polar(sin(x),cos(x),'r'); hold on; title('S. B. Nawarathne 090594D');

The output is shown below: 6

Q6.

th=(0:127)/128*2*pi; % creates 128 values ranging from 0 to 6.234 at regular intervals

x=cos(th); % creates array of 128 values for cos(th) y=sin(th); % creates array 128 values for sin(th)

f=abs(fft(ones(5,1),128)); % assigns absolute value of the fast fourier transform of a 1x5 matrix of 1s to variable f

stem3(x,y,f','d','fill'); % plots discrete values of “f” according to values of x and y, , displays as stems from (x,y) points. Plots 128 points, points are diamond shaped due to „d‟ and filled with colour due to „fill‟

view([-65 30])

%sets the viewing angle, i.e. 60 degrees about the z-axis measured

from the negative y-axis and an elevation of 30 degrees about the xy plane.

The output of the program is shown below: 7

Q7.

Now using Matlab, we the graph and find the points where the praph intersects the x-axis.

>> x=-30:0.01:30; z = 94.*(x.^3).*sin(x) + x.^2 + 94; y = zeros(1,length(x)); plot(x,y,x,z); grid on;

The above code gives rise to the following graph shown below: As it can be seen, there are infinitely many solutions to this equation, but the graph is symmetrical about x =0, therefore all corresponding negative and positive roots are equal in magnitude.

Since we need a more accurate way of finding the roots, a separate function was created in a separate file called f.m:

function y = f(x) y = 94.*(x.^3).*sin(x) + x.^2 +94;

8

By examining the graph (zoom in), there are roots near x= 3.18, 6.25, 9.43, 12.57, 15.71, 18.85, 21.98, 25.15,

28.28.

fun = @f;

>> p = fzero(fun,3.18) p =

3.176158927086919

>> p = fzero(fun,6.15)

p =

6.277448085165116

>> p = fzero(fun,9.43) p =

9.427100062077138

>> p = fzero(fun,12.57)

p =

12.565019861333038

>> p = fzero(fun,15.71) p =

15.708898449042263

>> p = fzero(fun,18.85) p =

18.848842191254882

>> p = fzero(fun,21.98) p =

21.991726336464605

>> p = fzero(fun,25.15)

p =

25.132254941151132

>> p = fzero(fun,28.28)

p =

28.274754368359893

9

So for -30 <= x <= 30, the roots of the equation are:

• x = 3.176158927086919, -3.176158927086919

• x = 6.277448085165116, -6.277448085165116

• x = 9.427100062077138, -9.427100062077138

• x = 12.565019861333038, -12.565019861333038

• x = 15.708898449042263, -15.708898449042263

• x = 18.848842191254882, -18.848842191254882

• x = 21.991726336464605, -21.991726336464605

• x = 25.132254941151132, -25.132254941151132

• x = 28.274754368359893, -28.274754368359893

### Part C

Q8.

>> fun = @(x) ((cos(10.*x)).^2).*exp(-x.^2); >> q = integral(fun,-2.5,2.5); >> q; q =

0.885820145396710

By using changing as AbsTol and RelTol, we can change the absolute error and relative error of the numerical computation as desired. AbsTol = |q-Q| and RelTol = |q-Q|/|Q| where q is the computed value and Q is the actual value, for example:

q = integral(fun,-2.5,2.5,'RelTol',1e-8,'AbsTol',1e-12);

Q9.

First to create a file:

>> fid=fopen('SajithN_090594D.dat','w'); >> x = -5:0.01:5; >> y =(((cos(10.*x)).^2).*exp(-x.^2));

for i = 1:1000

%because there are 1000 steps between -5 and 5

fprintf(fid,'%4.0f %4.4f %4.12f \n ',i,x(i),y(i)); end >> fclose(fid);

10

Now to plot the graph:

>> load SajithN_090594D.dat; >> z=SajithN_090594D; >> i = 201:801 % for values of -3 to 3 only >> plot (z(i,2),z(i,3));

The plot generated is shown below: Q10.

Plotting Rastrigin’s function

>> x = -1:0.01:1; y = -1:0.01:1; [X,Y] = meshgrid(x,y); Z = 20 + X.^2 + Y.^2 - 10.* (cos(2.*pi.*X) + cos(2.*pi.*Y)); mesh(X,Y,Z);

11

The code yields: Now to find the global minima:

>> for i = 1:10000000 %make 10 million random calculations between -1 and 1

x(i) = -1 + 2.*rand(1);

% stores random x, y and z values in arrays

y(i) = -1 + 2.*rand(1); z(i) = 20 + x(i).^2 + y(i).^2 - 10.* (cos(2.*pi.*x(i)) + cos(2.*pi.*y(i))); end >> min(z) % this gives global minimum ans =

4.971059407665734e-06

>> [n,m] = min(z) %returns the value of the minimum and the array index n =

4.971059407665734e-06

m =

9875713 % array index

>> x(m),y(m)

%returns the corresponding x and y values for the minimum

12

ans =

-1.513995608313223e-04

ans =

-4.620516111453199e-05

Therefore, Global minima = 4.971059407665734e-06 Corresponding x and y values; x = -1.513995608313223e-04 and y = -4.620516111453199e-05

Q11.

The output of the code is shown below: r = zeros(1,32); % returns a 1x32 matrix of zeros

for n = 3:32

% for n equal 3(must be 3 or greater for magic(n)) to 32

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

%

returns rank of magic(n), magic(n) creates a matrix of

size nxn with values ranging from 1 to n^2 whose row and column sums are equal.

end bar(r); %plots a bar graph of rank vs n

13 14