Download presentation
Presentation is loading. Please wait.
Published byParker Dorey Modified over 9 years ago
1
Problems and solutions Session 3
2
Introduction to MATLAB - Solutions 3 Problems 1. Write function Xn = mspolygon(X,x0,a) that scales the INPUT polygon by a (a>0) and moves its center to point x0, and draws both polygons in one image. The polygon is given by matrix X whose columns are the nodes (corner points) of the polygon. The output Xn is the nodes of new polygon. Define the centerpoint to be the average of the nodes. Test your function with P of Exercise 1/Session 2.
3
Introduction to MATLAB - Solutions 3 Problems 2. Write a function Xt = roundt(X,t) that rounds real numbers to grid tZ = (…,-2t,-t,0,t,2t,…) and complex numbers to grid tC = tZ+itZ. The input X can be a matrix and t>0. Test your function (real case) with X = -5:.01:5 and t=sqrt(2)/2. Draw a picture. Test your function (complex case) with X = randn(1,5)+2*i*randn(1,5) and t=0.5. Draw a picture. Write both test cases in one m-file.
4
Introduction to MATLAB - Solutions 3 Problems 3. Continue the Triangle Exercise 7/Session 2. a) Write a function xn = Qpoints(n) where the input argument n is a vector n(j) = number of random points in [0,1]x[0,1] (e.g. n = 1000:1000:10000) and xn is a cell array with xn{j} = n(j) random points. b) Call Qpoints many times to find an approximative error when computing the area of T with different n’s. Represent the results graphically.
5
Introduction to MATLAB - Solutions 3 Some solutions 1. function Xn = mspolygon(X,x0,a); Xsc = a*X; Xn = [Xsc(1,:)+x0(1);Xsc(2,:)+x0(2)]; CALL Xn = mspolygon(P,[2;1],0.75); plot(P(1,:),P(2,:),’b’,Xn(1,:),Xn(2,:),’r’) 2. function Xt = roundt(X,t); Xt = t*round(X/t); CALL X = -5:.01:5; Xt = roundt(X,sqrt(2)/2); plot(X,Xt,’.’) Z = round(1,5)+2*i*round(1,5); Zt = roundt(Z,0.5); plot(Z(1,:),Z(2,:),’o’,Zt(1,:),Zt(2,:),’rx’)
6
Introduction to MATLAB - Solutions 3 Some solutions 3. function xn = Qpoints(n); nn=length(n); xn = cell(n,1); for j = 1:nn xn{j} = rand(2,n(nn)); end --- A routine to compute the area of T: --- function aT = areaT(xn) % area of T computed with xn (cell) nn = length(xn); aT = zeros(nn,1); for j = 1:nn x = xn{j}; aT(j) = sum(x(2,:)<(1-x(1,:)))/size(x,2); end Main m-file: % number of points –vector: n = [1, 10, 100,1000,10000,100000]; nn = length(n); % number of rounds: N = 1000; aTall = zeros(n,N); for k = 1:N % compute the areas N times xn = Qpoints(n); aTall(:,k) = areaT(xn); end deviations = std(aTall’) % histograms, nn even for k = 1:nn subplot(2,nn/2,k) hist(aTall(k,:)) end
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.