You are on page 1of 7

Computer Aided Design and Simulation - Assignment for tutorial 2

Index No : 180214J
Name: Gunathilake S.D.

Part A
Q1

syms f(x);
f(x) = cos(10*x)^2 * exp(-x^2);
xRange = (-5:0.05:5);
fxRange = f(xRange);
size_xRange = size(xRange, 2);
index = (1: size_xRange);
mat_x_fx = [index(:), xRange(:), fxRange(:)];
csvwrite('180214J.csv', mat_x_fx, 0);
mat_x_fx_csv = load("180214J.csv");
firstindex = find(mat_x_fx_csv(:,2) == -3);
lastindex = find(mat_x_fx_csv(:,2) == 3);
x_data = mat_x_fx_csv(firstindex:lastindex,2);
fx_data = mat_x_fx_csv(firstindex:lastindex,3);
plot(x_data, fx_data)
xlabel("x");
ylabel("f(x)");
Q2.

[X,Y] = meshgrid(-1:0.05:1); %create grid coordinates


Z = 20+(X).^2+(Y).^2+10.*(cos(2*pi*X)+cos(2*pi*Y)); %Rastrigin's function
mesh(X,Y,Z); %plot the function
xlabel("x1");
ylabel("x2");
zlabel("f(x1,x2)");

Xrandom = -500 + (1000)*rand(1,500); %create random numbers between -


500 to 500
Yrandom = -500 + (1000)*rand(1,500);

Z_min = min(min(Z));
X_min = 0;
Y_min = 0;
for i=1:1:100 %checking the global minima
X = Xrandom(i);
for j=1:1:100
Y = Yrandom(j);
Z = 20+(X).^2+(Y).^2+10.*(cos(2*pi*X)+cos(2*pi*Y));
if Z<Z_min
Z_min = Z;
X_min = X;
Y_min = Y;
end
end
end

fprintf(' Global Minima of f(x1,x2) = %f , x1 = %f , x2 = %f',Z_min,X_min,Y_min);

Global Minima of f(x1,x2) = 0.500000 , x1 = 0.000000 , x2 = 0.000000


Q3

r = zeros(1,32);

for n = 3:32
r(n) = rank(magic(n));
end
bar(r);
Line 1 create a 1x32 matrix of zeros.

Line 2 runs a for loop from 3 to 32

Line 3 returns the rank of the magic(n) and assign it to nth number of r. magic(n) returns an n by n
matrix constructed from the integers 1 through n*2 with equal row and column sums

Line 4 ends the for loop

Line 5 plots a bar graph for each element of r

Q4
a)

x =(0:0.2:14);
Y1=besselj(1,x);
Y2=besselj(2,x);
Y3=besselj(3,x);
plot(x,Y1);
hold on
plot(x,Y2);
plot(x,Y3);
b)

xlabel("X");
ylabel("Y");

c)

legend("bessel: l", "bessel: 2","bessel: 3");


title("Bessel function(Order: 1,2,3)");
hold off

PartB
Q5
Same as Q3

Q6.
1)

s = linspace(-5,5,100);

coeff = [1 3 3 1];
A = polyval(coeff,s);

plot(s,A);

xlabel('s');
ylabel('A(s)');
roots(coeff);

Line 1 creates a row vector from -5 to 100 with interval of 5

Line 2 creates a row vector of coefficients of the polynomial

Line 3 creates the polynomial for s from the coefficients of the coeff row vector

Line 4 plot the polynomial

Line 5 labels the x axis as s

Line 6 lables the y axis as A(s)

Line 7 find the roots of the polynomial


Q7

You might also like