0% found this document useful (0 votes)
59 views6 pages

A1 Code

Task 1 generates 10 cosine wave plots with frequencies from 10 to 100 Hz. Task 2 simulates 10 random walks of 1000 steps each, plotting each step and calculating the average position. Task 3 uses iterative methods to find the solution to a cubic equation within 0.0001 error in 100 iterations or less.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views6 pages

A1 Code

Task 1 generates 10 cosine wave plots with frequencies from 10 to 100 Hz. Task 2 simulates 10 random walks of 1000 steps each, plotting each step and calculating the average position. Task 3 uses iterative methods to find the solution to a cubic equation within 0.0001 error in 100 iterations or less.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Task 1

t = 0:0.001:1;
f = 10:10:100;
for i = 1:1:10
y = cos(2*pi.*f(i).*t);
figure
plot(t,y)
title(sprintf('Cosine wave of %0.0f Hz',f(i)));
xlabel('Time(s)');
ylabel('Amplitude');
axis([0 0.2 -1 1]);
end

Task 2

Instead of 100 walks and 10000 steps I used 10 walks of 1000 steps since my computer is
not able to run so many points

clear all
clc
PastPos = [0,0];
CurrPos = [0,0];
i = 1;
dataX = zeros(1,100);
dataY = zeros(1,100);
for j = 1:10
for i = 1:1000

ran = rand();

if ran >=0 & ran <=0.25 % -x


CurrPos(1) = PastPos(1)-1;

elseif ran >0.25 & ran <=0.5 %+x


CurrPos(1) = PastPos(1)+1;

elseif ran >0.5 & ran <=0.75 %-y


CurrPos(2) = PastPos(2)-1;

elseif ran >0.75 & ran <=1 %+y


CurrPos(2) = PastPos(2)+1;

end

PastPos;
dataX(i) = CurrPos(1);
dataY(i) = CurrPos(2);
plot([PastPos(1) CurrPos(1)],[PastPos(2) CurrPos(2)])
hold on
PastPos = CurrPos;
i = i + 1;
title('Random Walk')
end

end

Average = [mean2(dataX),mean2(dataY)];
Mean = abs(mean(Average))

Task 3

syms x
f = x^3 - 2.*x - 5
fd = diff(f)
f = inline(f);
fd = inline(fd);
err = 0.0001;
x = 1;
for i = 1:100
y=x;
x = y - (f(x)/fd(x));
Err = abs(x-y);
if Err<err
break
end
end
fprintf('Number of iterations: %0.0f\n',i)
fprintf('The solution is %0.8f',x)

Task 4

f = [1 0 -2 -5];
roots(f)
Task 5

board = zeros(8);

P =[1,5 ; 2,7 ; 3,3 ; 4,8 ; 5,2 ; 6,5 ; 4,1 ; 6,6 ] ;


for i = 1:8
board(P(i,1),P(i,2)) = 1;
end
board

CheckX = zeros(1,8);
CheckY = zeros(1,8);
for i = 1:8
X = sum(board(i,:));
Y = sum(board(:,i));
CheckX(i) = X;
CheckY(i) = Y;
end

CheckDiag1 = zeros(1,15);
CheckDiag2 = zeros(1,15);
for i = -7:1:7
D1 = sum(diag(board,i));
D2 = sum(diag(fliplr(board),i));
CheckDiag1(i+8) = D1;
CheckDiag2(i+8) = D2;
end
CheckX;
CheckY;
CheckDiag1;
CheckDiag2;

if sum(CheckX>=2) >= 1
[r] = find(CheckX >= 2);
fprintf('There is a check on row %0.0f \n',r )
end

if sum(CheckY>=2) >= 1
[c] = find(CheckY >= 2);
fprintf('There is a check on column %0.0f \n',c );
end
if sum(CheckDiag1>=2) >=1
[r,c] = find(CheckDiag1>=2);
c = c-8;
for i = c
diag((board),i);
end

for i = 1:length(c)
idx = CheckDiag1>=2;
[r,c] = find(idx);
if idx == zeros(1,length(CheckDiag1))
c = 8;
end
c = c-8;

if c(i) >= 0
idx = CheckDiag1(8:15)>=2;
col1 = find(board(1,:)==1 & (idx==1));
if isempty(col1) ~= 1
for i = 1:length(col1)
fprintf('Check in row 1 and column %0.0f diagonally left to right\n',col1(i))
end
end
else
idx = c<0;
r = abs(c(idx))+1;
col2 = find(board(r,:)==1 & sum(CheckDiag1>=2) >=1);
if isempty(col2) ~= 1
for i = 1:length(col2)
fprintf('Check in row %0.0f and column %0.0f diagonally left to right\n',r,col2(i))
end
end
end
end
end

if sum(CheckDiag2>=2) >=1
[r,c] = find(CheckDiag2>=2);
c = c-8;
for i = c
diag(fliplr(board),i);
end
for i = 1:length(c)
idx = CheckDiag2>=2;
[r,c] = find(idx);
if idx == zeros(1,length(CheckDiag1))
c = 8;
end
c = c-8;

if c(i) >= 0
idx = flip(CheckDiag2(8:15)>=2);
col3 = find((board(1,:))==1 & (idx==1));
if isempty(col3) ~= 1
for i = 1:length(col1)
fprintf('Check in row 1 and column %0.0f diagonally right to left\n',col3(i))
end
end
else
idx = c<0;
r = abs(c(idx))+1;
col4 = find(board(r,:)==1 & sum(CheckDiag2>=2) >=1);
if isempty(col4) ~= 1
for i = 1:length(col4)
fprintf('Check in row %0.0f and column %0.0f diagonally right to left\n',r,col4(i))
end
end
end
end
end

Task 6

n = 2000;
data = zeros(1,n);
for i = 1:1:n
k = i;
f = 1/(k^2);
data(1,i) = f;
end
Solution = sum(data)

data = zeros();
i = 1;
while sum(data)<1.6449
k = i;
f = 1/(k^2);
data(1,i) = f;
sum(data);
i = i+1;
end
fprintf('%0.0f iterations are needed to get 4 decimal precision ',i)
sum(data)

You might also like