Professional Documents
Culture Documents
MATLAB Basics
Assignment 1
Name: S. B. Nawarathne
Index No: 090594D
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
12
16
20
16
12
16
16
16
20
128
288
512
800
128
512
288
512
512
512
800
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
ans =
4
12
16
20
13
17
21
16
17
12
16
13
17
16
16
17
17
20
21
13
17
21
16
24
32
40
17
16
32
13
17
24
32
17
17
32
32
21
40
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
>> linsolve(a,-b)
ans =
-0.0461
-0.0371
-0.0248
-0.0125
-0.0011
Q3.
a.
>> 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
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.
Part B
Q4.
k=5;
n=2^k -1;
theta=pi*(-n:2:n)/n;
3.1416 to 3.1416
phi=(pi/2)*(-n:2:n)/n;
to 1.5708
Q5.
>> x = [0 0.8 1.4 1.8 2.0]'
x =
0
0.8000
1.4000
1.8000
2.0000
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.
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;
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
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
10
Q10.
Plotting Rastrigins 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
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)
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:
13
14