70 likes | 210 Views
Problems and solutions Session 2. Problems. Write your solutions to m-files Check how matrix A = a) [2 0; b) [2 0; c) [-1 0; d) [ 1 1; e) [1 -1; f) [2 1; 0 1]; 0 -1]; 0 1]; -1 1]; 1 1]; 0 2]; maps the points
E N D
Problems Write your solutions to m-files • Check how matrix A = a) [2 0; b) [2 0; c) [-1 0; d) [ 1 1; e) [1 -1; f) [2 1; 0 1]; 0 -1]; 0 1]; -1 1]; 1 1]; 0 2]; maps the points P = [0, 4, 4, 3, 3, 2.5, 2.5, 2, 0, 0; 0, 0, 3, 4, 5, 5, 4.5, 5, 3, 0]; by plotting P and points A*P. To plot P you can use plot(P(1,:),P(2,:)). • Plot functions y=sin(x) and y = cos(x) on interval [0,4p] in the same figure but with different colors. Introduction to MATLAB - Solutions 2
Problems • Draw the unit circle in R2. • Draw the unit circle so that the line is green for x>0 and black for x<0. • Map the unit circle to the ellipse with major axes u = [2;1], minor axes v = [-1/2;1], and center (1,1). Draw the ellipse in the same picture with the unit circle. • Hint: Map linearly and transport. • Draw the image of the mapping f: 1 + [-i,i] C, • f(z) = log(z), • f(z) = z^2, in the complex plane. Hint: Real plane. Introduction to MATLAB - Solutions 2
Mortality fitting • In this exercise we consider mortality in Finland at 2007 (data loaded from Tilastokeskus website). • Copy kuolleisuus.xls (at the wikipage of the course) to your working directory. Load it to MATLAB (start your m-file with M = xlsread(’kuolleisuus.xls’);). The file contains matrix M with • M(:,1) = age • M(j,2) = mortality for males at age(j) [1/1000] • M(j,3) = mortality for females at age(j) • Fit polynomials of degree 2 and 3 to the mortality data. • Fit an exponential function to the mortality data, i.e., fit a polynomial of degree 1 to the log(mortality) –data. • Present your fit graphically. Use subplots, colors, titles, legends, and axis labels. Introduction to MATLAB - Solutions 2
Computing area with random points • Compute the area of the unit triangle T = span((0,0),(1,0),(0,1)) with uniformly distributed random numbers as follows: • Generate N uniformly distributed random points x =(x1,x2) in the unit square • Find the fraction of the points falling in T. Illustrate this graphically, plot the random points and T. Plot the points in T and the points out T with different colors. • Approximate area of T. Test the accuracy with different number of points N. Introduction to MATLAB - Solutions 2
A = [2 0; 0 1]; Q = A*P; subplot(2,3,1) plot(P(1,:),P(2,:),’b’,Q(1,:),Q(2,:),’r’) A = [2 0; 0 -1]; Q = A*P; subplot(2,3,2) plot(P(1,:),P(2,:),’b’,Q(1,:),Q(2,:),’r’) etc. x = (0:.01:(4*pi))’; plot(x,cos(x),’b’,x,sin(x),’g’) OR plot(x,[cos(x),sin(x)]) t = 0:.01:(2*pi); x = cos(t); y = sin(t); plot(x(x>0),y(x>0),’b’,... x(x<=0),y(x<=0),’r’) A = [2 -.5; 1 1]; t = 0:.01:(2*pi); x = [cos(t);sin(t)]; y = A*x; plot(x(1,:),x(2,:),’b’,… y(1,:)+1,y(2,:)+1,’r’) z = 1 + i*((-1):.01:1); fz = log(z); gz = z.^2; plot(real(fz),imag(fz),’b’,… real(gz),imag(gz),’r’) Some solutions Introduction to MATLAB - Solutions 2
subplot(1,2,2) plot(a,morm,’b.’,a,morf,’r.’,… a,exp(Lfits(:,1)),’b’,… a,exp(Lfits(:,2)),’r’) N = 5000; x = rand(2,N); T = (x(2,:) < (1-x(1,:))); inT = find(T); outT = find(~T); plot(x(1,inT),x(2,inT),’k.’,… x(1,outT),x(2,outT),’g.’) areaTapprx = length(inT)/N a = M(:,1); morm = M(:,2); % male mortality morf = M(:,3); p2m = polyfit(a,morm,2); p2f = polyfit(a,morf,2); fits = [p2m(1)*a.^2+p2m(2)*a+p2m(3),… p2f(1)*a.^2+p2f(2)*a+p2f(3)]; figure(1) plot(a,morm,’b.’,a,morf,’r.’,… a,fits(:,1),’b’,a,fits(:,2),’r’) % exponential fit: Lmorm = log(max(morm,.05)); Lmorf = log(max(morf,.05)); % max for not taking log(0) Lpm = polyfit(a,Lmorm,1); Lpf = polyfit(a,Lmorf,1); Lfits = [Lpm(1)*a+Lpm(2),Lpf(1)*a+Lpf(2)]; figure(2) subplot(1,2,1) plot(a,Lmorm,’b.’,a,Lmorf,’r.’,… a,Lfits(:,1),’b’,a,Lfits(:,2),’r’) Some solutions Introduction to MATLAB - Solutions 2