semilogy([15 25 55 115 144],'sm','markersize',10,'linewidth',4); xlabel('year') ylabel('number of students') grid axis tight % Part 2: Bar graph bar(rand(5,1),'r'); title('Bar graph of 5 random values') % Part 3: Interpolation and surface plots Z0=rand(5); [X0,Y0]=meshgrid(1:5,1:5); [X1,Y1]=meshgrid(1:0.1:5,1:0.1:5); Z1=interp2(X0,Y0,Z0,X1,Y1,'cubic'); surf(X1,Y1,Z1); colormap(hsv); shading interp hold on contour(X1,Y1,Z1); colorbar; caxis([0 1]); zlim([0 1.2]); % Part 6: Fun with find function ind=findNearest(x,desiredVal) differences=abs(x-desiredVal); minDiff=min(differences(:)); ind=find(differences==minDiff); % Part 5: Loops and flow control function loopTest(N)
for n = 1:N if rem(n,2) == 0 && rem(n,3) == 0 disp([num2str(n) ' is divisible by 2 and 3.']) elseif rem(n,2) == 0 && rem(n,3) ~= 0 disp([num2str(n) ' is divisible by 2.']) elseif rem(n,2) ~= 0 && rem(n,3) == 0 disp([num2str(n) ' is divisible by 3.']) else disp([num2str(n) ' is not divisible by 2 or 3.']) end end % Part 6: Smoothing filter function smoothed=rectFilt(x,width)
if rem(width,2) == 0; width = width + 1; warning(['input width even; new width = ',num2str(width)]) end
len_x = length(x); smoothed = zeros(len_x,1); floor_width = floor(width/2); for i = 1:len_x if (i <= floor_width) smoothed(i) = mean(x(1:2*i-1)); elseif (i <= len_x - floor_width) smoothed(i) = mean(x(i-floor_width:i+floor_width)); else smoothed(i) = mean(x(2*i-end:end)); end end
figure(1); clf; plot(x,'.b'); hold on plot(smoothed,'r'); grid end (Figure plotted with a window size of 11) % Part 7 % [x,y]=getCircle(center,r) % returns the x and y coordinates of a circle with its center at % (2 element vector) and radius r function [x,y]=getCircle(center,r) t=linspace(0,2*pi,100); x=center(1)+r*cos(t); y=center(2)+r*sin(t); % concentric % plots 5 concentric circles with 5 different colors % make the 5 colors using a jet colormap colors=jet(5); figure for n=1:5 [x,y]=getCircle([0 0],n); plot(x,y,'linewidth',14-2*n,'color',colors(n,:)); hold on; end axis equal % olympic % plots the olympic logo r=0.45; figure [x1,y1]=getCircle([-1 0],r); plot(x1,y1,'linewidth',4,'color','b'); hold on [x2,y2]=getCircle([0 0],r); plot(x2,y2,'linewidth',4,'color','k'); [x3,y3]=getCircle([1 0],r); plot(x3,y3,'linewidth',4,'color','r'); [x4,y4]=getCircle([-.5 -r],r); plot(x4,y4,'linewidth',4,'color','y'); [x5,y5]=getCircle([.5 -r],r); plot(x5,y5,'linewidth',4,'color','g'); axis equal