This document provides solutions to basic MATLAB exercises involving arrays, random numbers, plotting, and iteration. The exercises include: 1) Concatenating even and odd number arrays, 2) Plotting a cosine function and modifying values above a threshold, 3) Calculating even Fibonacci numbers, 4) Plotting a 2D function and its averages, 5) Generating and plotting Gaussian random variables, 6) Plotting PDFs of transformed random variables, 7) Creating a matrix with diagonal and off-diagonal elements and plotting it, and 8) Simulating a random walker and finding the PDF to reach a target position. Plots and code solutions are provided.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
101 views9 pages
Vector Matrice Hw02aqqq
This document provides solutions to basic MATLAB exercises involving arrays, random numbers, plotting, and iteration. The exercises include: 1) Concatenating even and odd number arrays, 2) Plotting a cosine function and modifying values above a threshold, 3) Calculating even Fibonacci numbers, 4) Plotting a 2D function and its averages, 5) Generating and plotting Gaussian random variables, 6) Plotting PDFs of transformed random variables, 7) Creating a matrix with diagonal and off-diagonal elements and plotting it, and 8) Simulating a random walker and finding the PDF to reach a target position. Plots and code solutions are provided.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9
Basic MatLab exercises: Solutions
Statistical Signal Processing
February 7, 2014 Working with Arrays 1. Generate an array E of even numbers from 0 to 10. Generate another array O of odd numbers from 0 to 10. Concatenate the two arrays and print the results. >> E=[0:2:10] E = 0 2 4 6 8 10 >> O=[1:2:10] O = 1 3 5 7 9 >> [E O] ans = 0 2 4 6 8 10 1 3 5 7 9 2. Generate a time vector t from 0 seconds to 10 seconds with a sampling interval of 0.1 second. Generate the function f(t) = cos(t). Plots this function. Now replace all values in f(t) that are above 0.6 with 1. Plot the modied array. >> t=[0:0.1:10]; >> f=cos(pi*t); >> plot(t,f,k.-); >> xlabel(Time in seconds); >> ylabel(f(t)); >> I=find(f>0.6); >> f(I)=1.0; >> hold on; plot(t,f,r.-); hold off; The output of the above code is shown in Figure 1 1 Figure 1: Plot of f(t) = cos(t) (black solid line) and the same function with all values above 0.6 replaced with 1.0 (red solid line). 3. Write a program to generate the rst 20 Fibonacci numbers, and calculate how many of them are even. close all; clear all; fnum=zeros(20,1); fnum(1)=0; fnum(2)=1; for ind=3:20 fnum(ind)=fnum(ind-1)+fnum(ind-2); end I=find(mod(fnum,2)==0); length(I) ans = 7 4. Write a program to plot the 2D function f(x, y) = cos(x) sin(y) for < x, y < . Also plot the average of the function along the x axis, and the average along the y axis as subplots in the same gure. x=linspace(-pi,pi,201);x=x(1:200); % pi and -pi are the same. Done sample them twice! y=linspace(-pi,pi,201);y=y(1:200); [X Y] = meshgrid(x,y); func=cos(X).*sin(Y); 2 Figure 2: Figure showing the function f(x, y) = cos(x) sin(y) for < x, y < (top-left panel), and its average along the x and y axes (top-right and bottom-left panel respectively) favgx=mean(func,1); favgy=mean(func,2); figure(1) subplot(221) imagesc(x,y,func); xlabel(x value); ylabel (y value); title(f(x,y)=cos(x)sin(y)); colorbar; subplot(222) plot(x,favgx); xlabel(x value); ylabel(Average along y); subplot(223) plot(y,favgy); xlabel(y value); ylabel(Average along x); Figure 2 shows the output of the above program. Note that theoretically the averages along both the x and y axes must be 0. This is not the case numerically as is evident in Figure 2. This is due to the limited numerical precision of the computations. Working with random numbers 1. Generate 1000 Gaussian random numbers with mean = 3 and variance = 2. Plot the his- togram of these numbers. Also compute the PDF from the random number array and plot it along with the true PDF on the same plot. 3 Figure 3: Figure showing the histogram (left panel) and computed PDF (right panel) of 1000 nealizations of a Gaussian random variable with mean = 3 and variance = 2. close all; clear all; r=randn(1000,1)*sqrt(2)+3; [n,x]=hist(r,100); figure(1); plot(x,n,k); xlabel(Value of x); ylabel(Number of occurances); figure(2); comp_pdf=n/1000/(x(2)-x(1)); plot(x,comp_pdf,ko); xlabel(Value of x); ylabel(PDF of x) hold on; true_pdf=1/sqrt(2*pi*2)*exp(-(x-3).^2/2/2); plot(x,true_pdf,r); hold off; legend(Computed,True); Output of the above program is shown in Figure 3. 2. A random variables x and y are independent with a standard normal distribution (Gaussian with 0 mean and unit variance). Numerically evaluate and plot the PDFs of x, angle(x+jy), |x + jy|, and |x + jy| 2 . Render these PDFs in the same gure, but on 3 dierent subplots. close all; clear all; rx=randn(10000,1); ry=randn(10000,1); figure(1); subplot(221) [n x]=hist(rx,100); 4 pdfx=n/10000/(x(2)-x(1)); plot(x,pdfx,k); xlabel(x value); ylabel(PDF of x); ang=angle(rx+1i*ry); subplot(222) [n x]=hist(ang,100); pdfx=n/10000/(x(2)-x(1)); plot(x,pdfx,k); xlabel(x value); ylabel(PDF of angle(x+jy)); absol=abs(rx+1i*ry); subplot(223) [n x]=hist(absol,100); pdfx=n/10000/(x(2)-x(1)); plot(x,pdfx,k); xlabel(x value); ylabel(PDF of abs(x+jy)); absol2=(abs(rx+1i*ry)).^2; subplot(224) [n x]=hist(absol2,100); pdfx=n/10000/(x(2)-x(1)); plot(x,pdfx,k); xlabel(x value); ylabel(PDF of abs(x+jy)^2); Output of the above code is shown in Figure 4. Iteration 1. Generate a 100 100 matrix A whose diagonal elements are unity, and element i, j for i = j are given by 1/|i j|. Plot this matrix as an image. A=ones(100,100); for a=1:100 for b = 1:100 if(a~=b) A(a,b)=abs(1/(a-b)); end end end imagesc(log10(A)); colorbar; xlabel(Index i) ylabel(Index j); title(log10(A)) 5 Figure 4: Plots showing the PDFs of x (top-left), angle of (x+jy) (top-right), |x+jy| (bottom-left), and |x + jy| 2 (bottom-right) 6 Figure 5: Plots showing the matrix A whose diagonal elements are unity, and element i, j for i = j are given by 1/|i j| Output of the above code is shown in Figure 5 2. A person starts walking at x = 0 accoding to the following rule. Every second, he rolls a die. If the outcome is more than 2, he takes a step in the positive x direction, else, he takes a step in the negative x direction. Numerically simulate the x co-ordinate of the person as a function of time and plot the result for 100 steps. close all; clear all; xpos=zeros(101,1); for ind=2:101 die=floor(rand(1,1)*7); if(die>2) xpos(ind) = xpos(ind-1)+1; else xpos(ind) = xpos(ind-1)-1; end end figure(1); plot([0:100],xpos,ko-); xlabel(Time in seconds); ylabel(Position in steps); Output of the above code is shown in Figure 6 7 Figure 6: Plot showing the position of the random walker as a function of number of steps (assumed to be seconds in this case) 3. Modify the above program to stop when the person reaches x = 15. How many times did he roll the die to reach this position. Repeat the simulation 1000 times to nd the PDF for the number of die rolls it takes to reach x = 15. close all; clear all; nstep_array=zeros(10000,1); for ind = 1:10000 xpos=0; nsteps=0; while(xpos<15) die=floor(rand(1,1)*7); nsteps=nsteps+1; if(die>2) xpos = xpos+1; else xpos = xpos-1; end end nstep_array(ind)=nsteps; end [n x]=hist(nstep_array,100); pdfx=n/10000/(x(2)-x(1)); figure(1); plot(x,pdfx,k.-); xlabel(Number of steps to reach x=15); ylabel(PDF of nsteps to reach x=15); 8 Figure 7: Plots showing the PDF of the number of die rolls it takes for the random walker to reach x = 15. Output of the above code is shown in Figure 7 9