You are on page 1of 13

MATLAB SIMULINK Assignments 7.7.2 ANNEX Basic Exercises 2______________ .

John Bofarull Guix, attached; -- 1 / 13



7.7.2.- BASIC EXERCISES

MATLAB Basic Exercises
Arrays Intro
Access single elements
Access element blocks
Use another array to extract elements
Matrix construction
Matrix orientation: Scalar Matrix operations, Standard Arrays, Replication, Array manipulation, Subsets access,
reshape, throw away row, duplicate row, subscripts, Cell Arrays, Structure generation, Dynamic addressing,
Structure Functions
Handles and Functions
Handle to Anonymous function
Handle to named function
Symbolic
Nested function
Information on a function handle for a given function
function handle from function name string
Function name string to function handle
ISA
FFT
Marker on peak

Symbolic: differentiation, Bessel functions 1
st
kind, Jacobian, Limits, Integration, pareto tail example to construct
piecewise distributions
Integration with Real parameters
Summation
Taylor series
Function definitions
Max Min
Inflections
Zeros
Conv

A Few cheesy test Questions

warm up games:
http://www.gutenberg.org/files/27635/27635-h/27635-h.htm
www.chess.com/play/computer
Simon Says online
Sudokus

_________________________________________________________________________________________________





















MATLAB SIMULINK Assignments 7.7.2 ANNEX Basic Exercises 2______________ .

John Bofarull Guix, attached; -- 2 / 13

1.- MATLAB BASIC EXERCISES

ARRAYS INTRO________________________________ .

step=0.1*pi;x=0:step:pi;y=sin(x);

x(3);y(5) % access single elements

x(1:5);x(7:end);y(3:-1:1);x(2:2:7) % access block of elements

y([8 2 9 1]); y([1 1 3 4 2 2]) % use another array to extract elements


x=(0:0.1:1)*pi;x=linspace(0,pi,11);logspace(0,2,11) % array construction
a=[1:7];b=[linspace(1,7,5)];a=(1:7)'
a=1:5, b=1:2:9
c=[b a]
d=[a(1:2:5) 1 0 1] % choosing 1st 3rd 5th + 1 0 1

c=[1;2;3;4;5];a=1:5;b=a';w=b' % ARRAY ORIENTATION
c=a.'; d=complex(a,a) % transpose and conjugate

e=d';f=d.'

g=[1 2 3 4;5 6 7 8]
g=[1 2 3 4 % enter = ;
5 6 7 8
9 10 11 12]


g-2;2*g-1;2*g/5+1 % scalar array operations
h=[1 1 1 1;2 2 2 2;3 3 3 3];g+h;ans-h

2*g-h;g.*h
g./h;h.\g;1./g
f=[1 1 1 1; 1 1 1 1; 1 1 1 1]
f./g;f./h;g/h % wrong rank

g, h

g.^2;g.^-1;1./g;2.^g;g.^h;g.^(h-1)

ones(3);size(g);ones(size(g));eye(4);eye(2,4) % STANDARD ARRAYS
eye(4,2);rand(3);rand(1,5);b=eye(3);rand(size(b))
randn(2) % NORMAL zero mean, unit variance
randn(2,5)

a=1:4;diag(a);diag(a,1);diag(a,-2)

d=pi;d*ones(3,4);d+zeros(3,4);d(ones(3,4))

repmat(d,3,4) % replicate matrix, D matrix, d scalar
D(r*c)=d % row vector r*c-th element is d
D(:)=d % fill all out with d
D=reshape(D,r,c)

A=[1 2 3;4 5 6;7 8 9] % ARRAY MANIPULATION

A(3,3)=0;A(2,6)=1
A(:,4)=4 % 4th column all to value 4
A(:,4)=[4;4;4] % same as previous
A(:,4)=[4 4 4] % ain't work

A=[1 2 3;4 5 6;7 8 9]
MATLAB SIMULINK Assignments 7.7.2 ANNEX Basic Exercises 2______________ .

John Bofarull Guix, attached; -- 3 / 13

B=A(3:-1:1,1:3) % reversing reading order
B=A(end:-1:1,1:3) % same as previous
B=A(3:-1:1,:) % same

C=[A B(:,[1 3])] % appending rows

B=A(1:2,2:3);B=A(1:2,2:end) % sub sets access

C=[1 3];B=A(C,C)

A=[1 2 3;4 5 6;7 8 9];B=[1 3;7 9]
B=A(:);B=B.'

B=reshape(A,1,9);B=reshape(A,[1 9]) % reshaping
B=A

B(:,2)=[] % throw away row 2

C=B.';reshape(B,2,3);C(2:)=[];A(2,:)=C

B=A(:,[2 2 2 2]);B=A(:,2+zeros(1,4));B=repmat(A(:,2),1,4)

C(1:2,:)=A % error
C(3:4,:)=A(2:3,:)

A=[1 2 3;4 5 6;7 8 9];A(:,2:3);G(1:6)=A(:,2:3)

H=ones(6,1);H(:)=A(:,2:3)

A(2,:)=0;A(2,:)=[0 0 0];A(1,[1 3])=pi

D(2*4)=2;D(:)=2;D=reshape(D,2,4);A=reshape(1:12,3,4)'

r=[3 2 1];Ar=[A(:,1)-r(1) A(:,2)-r(2) A(:,3)-r(3)]

R=r([1 1 1 1],:) % duplicate r to have 4 rows

Ar=A-R;R=r(ones(size(A,1),1),:)
R=repmat(r,size(A,1),1);D=reshape(1:12,3,4)
D(2);D(5);D(end)

sub2ind(size(D),2,4) % subscripts refer to row and column location of element in array
[r,c]=ind2sub(size(D),11)

x=-3:3;abs(x)>1;y=x(abs(x)>1)

B=A(:,[1 3 5 2 4]) % reorder array A where [1 3 5 2 4] are the index positions


CELL ARRAYS .

A(1,1)={[1 2 3;4 5 6;7 8 9]};A(1,2)={1+3i};A(2,1)={'A character string'};A(2,2)={12:-2:0}

A{1,1};A{1,2};A{2,1};A{2,2};celldisp(A);A{1,:}

B={[1 2],'John Smith'; 2+3i, 5} % {} are to cells what [] are to arrays
C=cell(2,3)

C(1,1)='this doesn't work';C(1,1)={'this works'}

C=[A;B];D=C([1 3],:);C(3,:)=[]

X=cell(3,4);size(X) % ans= 3 4

MATLAB SIMULINK Assignments 7.7.2 ANNEX Basic Exercises 2______________ .

John Bofarull Guix, attached; -- 4 / 13
Y=reshape(X,6,2);size(Y);Z=repmat(Y,1,3)
x=B{2,2};class(Y);class(Y{1});iscell(Y)

[Y1, Y2, Y3]=deal(X);[Y1, Y2, Y3]=deal(X1, X2, X3)
[X(:)]=deal(A.field);[X{1:m}]=deal(A.field)

cellfun('function_name',A)

iscell()
cellstr()
isequal()

% STRUCTURE GENERATION, structure fields
% structure creation by direct assignment
circle.radius=2.5;circle.center=[0 1];
circle.linestyle='--';circle.color='red';

size(circle);

circle(2).radius=3.4;circle(2).center=[2.3 -1.2];
circle(2).linestyle='-:';circle(2).color='green';

size(circle);

circle(2).radius

fldstr='color';circle.(fldstr) % DYNAMIC ADDRESSING


values1={2.5 'sqr(2)' 25.4}; % structure definition using 'struct'
values2={[0 1] [2.3 -1.2] [-1 0]};
values3={'--' ':' '-.'};
values4={'red' 'green' 'blue'};
values5={'yes' 'no' 'yes'};
CIRCLE=struct('radius',values1,'center',values2,...
'linestyle',values3,'color',values4.'filled'.values5)

isequal(circle,CIRCLE)

C=[circle(1:2) CIRCLE(3)]

isequal(C,circle)

area1=pi*circle(1).(fldstr)^2

% simultaneous data extraction from structure arrays
% build reception container first
a=ones(2,3); b=zeros(2,1); c=(3:4)'; d=[a b c];
d=cat(2,a,b,c)
[m,n]=size(d);

cent=cat(1,circle.center)
cent=cat(1,circle(1).center,circle(2).center,circle(3).center)

[c1 c2 c3]=deal(circle.color);
[rad1 rad3]=deal(circle([1 3]).radius)
[circle.radius]=deal(5,14,83)

[triangle(1:3).type]=deal('right','isosceles','unknown')

fieldnames(circle) % STRUCTURE FUNCTIONS

isfield(circle,'color')
class(circle)
isstruct(circle)
MATLAB SIMULINK Assignments 7.7.2 ANNEX Basic Exercises 2______________ .

John Bofarull Guix, attached; -- 5 / 13

circle2=rmfield(circle,fnames{5})

circle3=rmfield(circle,fnames{1:3})
circleA=orderfields(circle)
circleB=orderfields(circleA,CIRCLE)
circleC=orderfields(circle,[2 5 1 4 3])
circleD=orderfields(circle,fnames(end:-1:1))

car.year=2012 % specify direct contents

car=struct() % specify all fields at once
cars=[car;car]
cars(2).mpg=40
cars.mpg returns all mpg
Cell1={1 2 3;'test1',[1;2],false}

cell of arrays:
c{1}=[31 7];c{2}=[3 78]; c{:}
accessing one array of the cell: c{1}
copy single input to all requested outputs with deal()
[Y1, Y2, Y3, ...] = deal(X) same as Y1=X, Y2=X, Y3=X, ...
[Y1, Y2, Y3, ...] = deal(X1, X2, X3, ...) same as Y1=X1; Y2=X2; Y3=X3; ...

MAPS .

isKey() keys length remove size values

tMap1=containers.Map({key1 key2 ..},{val1 val2 ..})

val1=tMap1('key1')



TABLES .
http://blogs.mathworks.com/pick/2008/10/10/html-tables/
look-up table in Simulink


HANDLES and FUNCTIONS __________________________________ .

sqr=@(x) x.^2; a=sqr(5) % HANDLE TO ANNONYMOUS FUNCTION

x=-5:.1:5;a=1;b=-2;c=1;f=@(x)(a*x.^2+b*x+c)
plot(x,f(x))

minimum=fminbnd(f,-2,2)
plot(minimum,f(minimum),'d')

fhandle=@humps % HANDLE TO NAMED FUNCTION
x=fminbnd(fhandle,0.3,1) % =.6370

function y=myfunc(x)
y=1/.(x.^3-2*x-5)
Q=quad(@myfunc,0,2) % call
Q=-.4605

f=@(x) x.^2;g=@(x) 3*x; % NESTED FUNCTIONS
h=@(x) g(f(x));h(3)

% INFORMATION ON A FUNCTION HANDLE FOR A GIVEN FUNCTION
f=functions(@poly) % it has 3 fields; function, type, file
f.type

% FUNCTION HANDLE FROM FUNCTION NAME STRING
MATLAB SIMULINK Assignments 7.7.2 ANNEX Basic Exercises 2______________ .

John Bofarull Guix, attached; -- 6 / 13
% no access to nested functions, use @ for nested functions access

fh=str2func('sin') % fh=@sin
function fh=makeHandle(funcname)
fh=str2func(funcname) % makeHandle('sin') % ans=@sin

% CELL ARRAY OF FUNCTION HANDLES
fh_array=cellfun(@str2func, {'sin' 'cos' 'tan'}, ...
'UniformOutput', false);
fh_array{2} (5) % ans = cos(5)=.2837

fhandle=@sin; % FUNCTION NAME STRING TO FUNCTION HANDLE
func2str(fhandle) % ans = sin

function catcherr(func, data)
try
ans=func(data);
disp('Answer is:');
ans
catch
disp(sprintf('Error erxecuting function ''%s''\n', ... funct2str(func)))
end

%call that resturns no evaluation error;
catcherr(@round, 5.432) % ans= Answer is 5

xstuct.value=5.432;catcherr(@round,xstruct) % call that catches evaluation error
Error executing function "round"


isa(rand(3,4),'double') % ISA
polynom_obj=polynom([1 0 -2 -5]);
isa(polynom_obj,'polynom') % isa type parameter: logical, char,numeric, integer,
% int8, uint8, int16, uint16, int32, uint32, int64, uint64,
% float, single, double, cell, struct, function_handle,
% 'class_handle'

load sunspot.dat;year=sunspot(:,1);;relNums=sunspot(:,2); % FFT
plot(year,relNums);title('Sunspot Data')

plot(year(1:50),relNums(1:50),'b.-');Y = fft(relNums);Y(1)=[];
plot(Y,'ro')
title('Fourier Coefficients in the Complex Plane');
xlabel('Real Axis');
ylabel('Imaginary Axis');

n=length(Y);power = abs(Y(1:floor(n/2))).^2;
nyquist = 1/2;
freq = (1:n/2)/(n/2)*nyquist;
plot(freq,power);xlabel('cycles/year');title('Periodogram')

plot(freq(1:40),power(1:40));xlabel('cycles/year')

period=1./freq;
plot(period,power);axis([0 40 0 2e+7])
ylabel('Power');xlabel('Period (Years/Cycle)');

hold on; % marker on peak
index=find(power==max(power));
mainPeriodStr=num2str(period(index));
plot(period(index),power(index),'r.', 'MarkerSize',25);
text(period(index)+2,power(index),['Period = ',mainPeriodStr]);
hold off;


MATLAB SIMULINK Assignments 7.7.2 ANNEX Basic Exercises 2______________ .

John Bofarull Guix, attached; -- 7 / 13

SYMBOLIC .

% differentiation
syms x; f=sin(5*x); diff(f); ans = 5*cos(5*x)
g=exp(x)*cos(x); diff(g); ans = exp(x)*cos(x)-exp(x)*sin(x)
diff(g,2);
diff(diff(g))
c=sym('5'); diff(c);
diff(5)

syms s t
f=sin(s*t)
diff(f,t)
diff(f,s)
findsym(f,1)
diff(f,t,2)

syms a b x n t theta
f=x^n; diff(f)
f=sin(a*t+b); diff(f)
f=exp(i*theta); diff(f)




% Bessel function 1st kind
syms nu z
b=besselj(nu,z)
db=diff(b)

syms a x
A=[cos(a*x),sin(a*x);-sin(a*x),cos(a*x)]
diff(A)

syms r l f % Jacobian
x=r*cos(l)*cos(f); y=r*cos(l)*sin(f); z=sin(l);
J=jacobian([x;y;z],[r l f])
detJ=simple(det(J))

syms h n x % Limits
limit((cos(x+h)-cos(x))/h,h,0)
limit((1+x/n)^n,n,inf)
limit(x/abs(x),x,0,'left')
limit(x/abs(x),x,0,'right')
limit(x/abs(x),x,0) % NaN

int(x^n); int(x^n,x) % Integration
int(sin(2*x),0,pi/2); int(sin(2*x),x,0,pi/2)
g=cos(a*t+b); int(g,t)
int(beselj(1,z),z)
syms a b theta x y n u z

X^n; piecewise([n=-1, log(x)],[n<>-1,x^(n+1)/(n+1)])
MATLAB SIMULINK Assignments 7.7.2 ANNEX Basic Exercises 2______________ .

John Bofarull Guix, attached; -- 8 / 13
y^(-1);n^x;
sin(a*theta+b)
1/(1+u^2);atan(u)
exp(-x^2);pi^.5*erf(x)/2
x^7; a=0; b=1; 1/8
1/x; a=1; b=2; log(2)
log(x)*sqrt(x); a=0; b=1; -4/9
exp(-x^2); a=0; b=inf; pi^(1/2)/2
besselj(1,z)^2; a=0; b=1; hypergeom([3/2,3/2],[2,5/2,3],-1)/12

syms z
a=int(besselj(1,z)^2,0,1); a=double(a); 0.717

% paretotail example to construct piecewise distributions
% available methods: cdf, disp, display, icdf, nsegments, pdf, random, segment
t = trnd(3,100,1);
obj = paretotails(t,0.1,0.9);
[p,q] = boundary(obj);
x = linspace(-5,5);
plot(x,cdf(obj,x),'b-','LineWidth',2)
hold on
plot(x,tcdf(x,3),'r:','LineWidth',2)
plot(q,p,'bo','LineWidth',2,'MarkerSize',5)
legend('Pareto Tails Object','t Distribution',...
'Location','NW')

% Integration with R parameters
syms x; a=sym(1/2); f=exp(-a*x^2); ezplot(f)
syms a positive; syms x; f=exp(-a*x^2); int(f,x,-inf,inf)

% integration complex domain
syms a x clear; f=1/(a^2+x^2); F=int(f,x,-inf,inf)
syms a x clear; f = 1/(a^2 + x^2); F = int(f, x, -inf, inf); F = (pi*signIm(i/a))/a
g=subs(F,1+i);
double(g)

% Symbolic summation
syms x k
s1=symsum (1/k^2,1,inf)
s2=symsum(x^k,k,0,inf); s1=pi^2/6; s2=piecewise([1<=x,Inf],[abs(x)<1,-1/(x-1)])


% Taylor series
syms x; f=1/(5+4*cos(x)); T=taylor(f,8); pretty(T)
syms x; g=exp(x*sin(x));t=taylor(g,12,2); size(char(t))
t=simplify(t); size(char(t));
t=simple(t); size(char(t));
xd=1:0.05:3; yd=subs(g,x,xd); ezplot(t,[1,3]); hold on; plot(xd, yd, 'r-.');
title('Taylor approximation vs actual function'); legend('Taylor','Function')

% Function definitions
syms x; num = 3*x^2 + 6*x -1; denom = x^2 + x - 3; f = num/denom; ezplot(f)

% Asymptotes
limit(f,inf); roots=solve(denom); ezplot(f); hold on;plot([-2*pi 2*pi], [3 3],'g')
plot(double(roots(1))*[1 1], [-5 10],'r'); plot(double(roots(2))*[1 1], [-5 10],'r')
title('Horizontal and Vertical Asymptotes'); hold off

%Max Mins
f1=diff(f); f1=simplify(f1); pretty(f1);
crit_points=solve(f1);
ezplot(f); hold on; plot(double(crit_pts), double(subs(f,crit_pts)),'ro')
title('Maximum and Minimum of f'); text(-5.5,3.2,'Local minimum')
text(-2.5,2,'Local maximum'); hold off

MATLAB SIMULINK Assignments 7.7.2 ANNEX Basic Exercises 2______________ .

John Bofarull Guix, attached; -- 9 / 13
% Inflections
f2 = diff(f1); inflec_pt = solve(f2); double(inflec_pt); inflec_pt=inflec_pt(1);
pretty(simplify(inflec_pt)); ezplot(f,[-9 6]);
hold on; plot(double(inflec_pt), double(subs(f,inflec_pt)),'ro'); title('Inflection Point of f')
text(-7,2,'Inflection point'); hold off

% example
syms x; f = 1/(5+4*cos(x)); f2=diff(f,2); ezplot(f2); axis([-2*pi 2*pi -5 2]);title('Graph of f2')

% zeros
f3=diff(f2); pretty(f3);
f3=simple(f3); pretty(f3); zeros=solve(f3); format; zerosd=double(zeros)
ezplot(f3); hold on; plot(zerosd,0*zerosd,'ro'); plot([-2*pi,2*pi], [0,0],'g-.'); title('f3 Graph')
zerosd=[zerosd(1) zerosd(3) zerosd(4) pi];

ezplot(f3); hold on; plot(zerosd,0*zerosd,'ro'); plot([-2*pi,2*pi], [0,0],'g-.'); % Plot x-axis
title('Zeros of f3'); hold off;
[zerosd; subs(f2,zerosd)];

clf; ezplot(f2); axis([-2*pi 2*pi -4.5 1.5]);
ylabel('f2'); title('Maxima and Minima of f2'); hold on; plot(zerosd, subs(f2,zerosd), 'ro')
text(-4, 1.25, 'Absolute maximum'); text(-1,-0.25,'Local minimum')
text(.9, 1.25, 'Absolute maximum'); text(1.6, -4.25, 'Absolute minimum'); hold off;

g= int(int(f2))
d=f-g; pretty(d);
F=int(f);
limit(F, x, pi, 'left'); limit(F, x, pi, 'right');
J = sym(2*pi/3)*sym('round(x/(2*pi))'); ezplot(J,[-2*pi 2*pi]);
F1=F+J; ezplot(F1);


Defining Objects and Classes in MATLAB Intro

http://www.mathworks.co.uk/support/2012a/matlab/7.14/demos/WhatsNewR2008a_ObjectOrientedProgramming.html


CONV

y=conv_m(x_exp,k,,n+k2)

n2=-10:1:10;
k2=-5:1:5;

x2(-10)=sum(k=-5,5,exp(-abs(k))*delta(-10-2*k))=
exp(-abs(-5))*delta(-10-2*(-5))+
exp(-abs(-4))*delta(-10-2*(-4))+
exp(-abs(-3))*delta(-10-2*(-3))+
exp(-abs(-2))*delta(-10-2*(-2))+
exp(-abs(-1))*delta(-10-2*(-1))+
exp(-abs(0))*delta(-10-2*0)+
exp(-abs(1))*delta(-10-2*1)+
exp(-abs(2))*delta(-10-2*2)+
exp(-abs(3))*delta(-10-2*3)+
exp(-abs(4))*delta(-10-2*4)+
exp(-abs(5))*delta(-10-2*5) =

exp(-abs(-5))*delta(-10+10)+
exp(-abs(-4))*delta(-10+8)+
exp(-abs(-3))*delta(-10+6)+
exp(-abs(-2))*delta(-10+4))+
exp(-abs(-1))*delta(-10+2)+
exp(-abs(0))*delta(-10)+
exp(-abs(1))*delta(-10-2)+
exp(-abs(2))*delta(-10-4)+
MATLAB SIMULINK Assignments 7.7.2 ANNEX Basic Exercises 2______________ .

John Bofarull Guix, attached; -- 10 / 13
exp(-abs(3))*delta(-10-6)+
exp(-abs(4))*delta(-10-8)+
exp(-abs(5))*delta(-10-10) =

exp(-abs(-5))*delta(0)+
exp(-abs(-4))*delta(-2)+
exp(-abs(-3))*delta(-4)+
exp(-abs(-2))*delta(-6)+
exp(-abs(-1))*delta(-8)+
exp(-abs(0))*delta(-10)+
exp(-abs(1))*delta(-12)+
exp(-abs(2))*delta(-14)+
exp(-abs(3))*delta(-16)+
exp(-abs(4))*delta(-18)+
exp(-abs(5))*delta(-20) = exp(-abs(-5))

x2(-9)= sum(k=-5,5,exp(-abs(k))*delta(-9-2*k))=
exp(-abs(-5))*delta(-9-2*(-5))+
exp(-abs(-4))*delta(-9-2*(-4))+
exp(-abs(-3))*delta(-9-2*(-3))+
exp(-abs(-2))*delta(-9-2*(-2))+
exp(-abs(-1))*delta(-9-2*(-1))+
exp(-abs(0))*delta(-9-2*0)+
exp(-abs(1))*delta(-9-2*1)+
exp(-abs(2))*delta(-9-2*2)+
exp(-abs(3))*delta(-9-2*3)+
exp(-abs(4))*delta(-9-2*4)+
exp(-abs(5))*delta(-9-2*5) =

exp(-abs(-5))*delta(1)+
exp(-abs(-4))*delta(-1)+
exp(-abs(-3))*delta(-3)+
exp(-abs(-2))*delta(-5)+
exp(-abs(-1))*delta(-7)+
exp(-abs(0))*delta(-9)+
exp(-abs(1))*delta(-11)+
exp(-abs(2))*delta(-13)+
exp(-abs(3))*delta(-15)+
exp(-abs(4))*delta(-17)+
exp(-abs(5))*delta(-19) = 0


x2(-8)= sum(k=-5,5,exp(-abs(k))*delta(-8-2*k))=
exp(-abs(-5))*delta(-8-2*(-5))+
exp(-abs(-4))*delta(-8-2*(-4))+
exp(-abs(-3))*delta(-8-2*(-3))+
exp(-abs(-2))*delta(-8-2*(-2))+
exp(-abs(-1))*delta(-8-2*(-1))+
exp(-abs(0))*delta(-8-2*0)+
exp(-abs(1))*delta(-8-2*1)+
exp(-abs(2))*delta(-8-2*2)+
exp(-abs(3))*delta(-8-2*3)+
exp(-abs(4))*delta(-8-2*4)+
exp(-abs(5))*delta(-8-2*5) =

exp(-abs(-5))*delta(2)+
exp(-abs(-4))*delta(0)+
exp(-abs(-3))*delta(-2)+
exp(-abs(-2))*delta(-4)+
exp(-abs(-1))*delta(-6)+
exp(-abs(0))*delta(-8)+
exp(-abs(1))*delta(-10)+
exp(-abs(2))*delta(-12)+
exp(-abs(3))*delta(-14)+
exp(-abs(4))*delta(-16)+
MATLAB SIMULINK Assignments 7.7.2 ANNEX Basic Exercises 2______________ .

John Bofarull Guix, attached; -- 11 / 13
exp(-abs(5))*delta(-18) = exp(-abs(-4))

x2(-7) = 0

x2(-6)= sum(k=-5,5,exp(-abs(k))*delta(-6-2*k))=
exp(-abs(-5))*delta(-6-2*(-5))+
exp(-abs(-4))*delta(-6-2*(-4))+
exp(-abs(-3))*delta(-6-2*(-3))+
exp(-abs(-2))*delta(-6-2*(-2))+
exp(-abs(-1))*delta(-6-2*(-1))+
exp(-abs(0))*delta(-6-2*0)+
exp(-abs(1))*delta(-6-2*1)+
exp(-abs(2))*delta(-6-2*2)+
exp(-abs(3))*delta(-6-2*3)+
exp(-abs(4))*delta(-6-2*4)+
exp(-abs(5))*delta(-6-2*5) = exp(-abs(-3))
..

only n even yields x(n)!=0 because of the 2k.

q1=conv_m(x_exp,length(x_exp),delta2,n_delta2)

since x(n)=sum(-inf,inf,x(k)*delta(n-k)) and from
the above p&s calculation, Problem 2.1.2
is interleaving zeros into an original sequence
k=-5,5, exp(-abs(k))

x=[3,11,7,0,-1,4,2];
nx=[-3:3];

[y,ny]=sigshift(x,nx,-2);

w1=randn(1,length(y));
nw=ny;

[y,ny]=sigadd(y,ny,w1,nw);
[x,nx]=sigfold(x,nx); % calculating x(-n)

% crosscorrelation with noise sequence 1
%
[rxy,nrxy]=conv_m(y,ny,x,nx);

%_______________________

n=-10:10
k=-5:5
x_exp=exp(-abs(k)) % 1st signal into convolution
[x_delta,n_x_delta]=impseq(0,-10,10) % 2nd signal

[y,ny]=sigshift(x_delta,n_x_delta,n(2))

_________________

DECONV, CONV2, CONVN, FILTER and, in the Signal
% Processing Toolbox, XCORR, CONVMTX.


n=[n1:n2];x=[(n-n0)==0];

_____________________

for q=-5:1:5
{
s_delt=zeros(1,11)
for p=-5:1:5 s_delt(p)=delt(n-2*q);
MATLAB SIMULINK Assignments 7.7.2 ANNEX Basic Exercises 2______________ .

John Bofarull Guix, attached; -- 12 / 13
x(m)=sum(x_exp.*s_delt(n-2*q));
}


--------------------------------


for (v=(n_x2(1)+(length(x2)+1)/2):1:(n_x2(11)+(length(x2)+1)/2)),
for (q=-5:1:5),

__________________________--

for v=-10:1:10

s_delt=zeros(1,11)
for p=1:1:11, s_delt(p)=delt(-10-2*(p-6)), end

x2(v+11)=sum(x_exp.*s_delt(n_x2-2*q));
end



2.- A FEW TEST QUESTIONS

http://www.mathworks.co.uk/services/training/online.html

What command will return the corner elements of a 10-by-10 matrix A?

**A. A([1,end], [1,end])
B. A([1,1], [end,end]) returns 2x2 with same A(1,:) central element
C. A({[1,1], [1,end], [end,1], [end,end]}) what strings format has to do with selecting elements from matrix?
D. A(1:end, 1:end) =A

What command will return the fraction of positive numbers in a 10-by-10 matrix A?

A. A(A > 0)/A error. Had it been ../det(A), since det(A)=0 yet MATLAB answers
B. numel(A > 0)/numel(A)
C. sum(A > 0)/prod(size(A))
**D. nnz(A > 0)/numel(A) %% 0 is neither positive nor negative so it is not taken into account

What command will delete (completely remove) the last cell of a cell-array C?

A. C{end} = [];
B. C[end] = [];
** C. C(end) = [];
D. C{end} = {[]};

What command will create a plot of acceleration vs. time (i.e., a vector time on the x-axis and a vector acceleration on
the y-axis)?

** A. plot(time, acceleration)
B. plot(acceleration, time)
C. plot([time, acceleration])
D. plot([acceleration, time])

What command will give the standard deviation for each column in a 10-by-5 matrix Z?

A. std(Z(:))
B. std(std(Z))
** C. std(Z(1:5, :))
D. std(Z)




MATLAB SIMULINK Assignments 7.7.2 ANNEX Basic Exercises 2______________ .

John Bofarull Guix, attached; -- 13 / 13

3.- WARM UP GAMES

http://www.gutenberg.org/files/27635/27635-h/27635-h.htm
www.chess.com/play/computer
Simon Says online
Sudokus

You might also like