Professional Documents
Culture Documents
%a)
A= [1 3 2
5 0 2];
%b)
% Matrix of zeros
V=zeros(4,2);
% Matrix of ones
W=ones(3,1);
% Identity matrix
I= eye(3,3);
save(DFILE);
clear;
%c)
% An easy way to do this is:
% Column vector with 10 normally distributed random variables
% RAND([M,N]) returns an M-by-N matrix.
R0 = randn(10,1);
% Generate values from the uniform distribution on the interval [a, b].
% r = a + (b-a).*rand(100,1);
R10 = 10+randn(1,10);
% Uniform distribution
U = rand(10,1);
% d)
A(1,2) = 8;
% e)
B = A(2,:);
% f)
C = [4 1 2; 1 3 3];
D = C(C<=3);
%% 3 Basic Manipulations
clear all %clears the workspace
clc %clears the command window
%a)
E=[4 1;1 3]
F=[1 1;2 3]
[n,m]=size(E);
E+F
E-F
%b)
% Matrix multiplication (not element by element):
G=E*F
% Multiplication element by element:
H=E.*F
% Division element by element:
X=E./F
%c)
K=inv(E)*F
K=E^-1*F
K=E\F
% Determinant of a matrix:
det(K)
%a)
%x = random('Uniform',0,100,1,100);
x = 100*rand(100,1);
%b)
plot(x);
%c)
x1 = x(1:50,1);
x2 = x(51:100,1);
figure(1);
subplot(211);
plot(x1);
subplot(212);
plot(x2);
%other possibility
figure(2);
subplot(121);
plot(x1);
subplot(122);
plot(x2);
%editing
figure(3);
plot(x,'r:');
title('100 RANDOM DRAWS');
xlabel('number of the draw');
ylabel('drawn number');
legend('random draws');
axis([0 100 0 100]);
%d)
mean_x =mean(x);
std_x =std(x);
mean_x1=mean(x1);
std_x1 =std(x1);
mean_x2=mean(x2);
std_x2 =std(x2);
%repeating b)
mean_x_vector=zeros(100,1);
mean_x_vector(1:100,1)=mean_x;
figure(4);
hold on ;
plot(x,'b');
plot(mean_x_vector,'r--');
title('100 RANDOM DRAWS');
xlabel('number of the draw');
ylabel('drawn number');
legend('random draws','Location','SouthOutside') ;
axis([0 100 0 100]);
hold off;
%b)
A=2
while A<10;
A=A+1
end
%c) IF-loop
r=rand(1,1);
if r<0.9;
disp('This number is smaller than 0.9');
else
disp('This number is greater than 0.9');
end
%% 6 Geometric series
% a)
s=zeros(10,1);
q=0.5;
for n=1:10
s(n,1)=q^(n-1);
end
%b)
SUM=sum(s);
disp('Sum for n=10, q=0.5')
disp(SUM)
%c)
s=zeros(10,3);
q1=0.2;
q2=0.5;
q3=0.8;
for n=1:10
s(n,1)=q1^(n-1);
s(n,2)=q2^(n-1);
s(n,3)=q3^(n-1);
end
SUM=zeros(1,3);
for i=1:3
SUM(1,i)=sum(s(:,i));
end
disp('Sums for q1=0.1, q2=0.5, q3=0.8')
disp(SUM)
%% 7 Difference equations
clear;
x=20;
T=(1:x)';
f=0.7;
y=zeros(x,1);
w=zeros(x,1);
y(1,1)=1.5;
% a)
% y_t = f*y_(t-1) + w_t
for t=1:x-1
y(t+1,1)=f*y(t,1)+w(t+1,1);
end
figure;
plot(T,y,'b-');
% set f=1:
f=1;
y1=zeros(x,1);
y1(1,1)=1.5;
for t=1:x-1
y1(t+1,1)=f*y1(t,1)+w(t+1,1);
end
figure;
plot(T,y1,'r-');
%c)
% First approach: Analytical
IRF1_analytical=zeros(x,1);
for t=1:x-1
IRF1_analytical(t+1,1)=sum(IRF_analytical(1:t+1,1));
end
figure
plot(T,IRF1_analytical,'g--');
%d) Optional
w(2:x,1)=randn(x-1,1)*0.1;
ys=y;
for t=1:x-1
ys(t+1,1)=f*ys(t,1)+w(t+1,1);
end
figure;
plot(T,ys,'b-');
sold=1;
q=0.5;
diff=1;
for n=1:1000000000;
snew=sold+q^n;
diff=snew-sold;
if diff<=0.000000000001;
break;
else sold=snew;
end;
end;
n
f=0.7;
yold=0;
for t=1:1000000000
ynew=f^(t-1)+yold;
diff=ynew-yold;
if diff<=0.0000001;
break;
else yold=ynew;
end;
end
t
disp('Long-Term Effect (after t periods)')
disp(ynew)
% NOT NECESSARY ACCORDING TO THE QUESTION
disp('Theoretical Value')
disp(1/(1-f))
%% 10 Function file
%b)
disp('Press any key to draw a random number N(0,1)')
pause
z=randn(1,1);
judgement=draw(z); % Call function file "draw.m"
disp(judgement)
disp(z)