% % Homework #4 - Sample Solutions % 3-D "Sombrero" Plotting & Working with Polar Coordinates % % % PART 1 - 3D Plotting and Manipulations % % Sombrero Set-up Equations dim1 = [ -8 : 0.5 : 8 ]; dim2 = dim1; [x,y] = meshgrid(dim1, dim2); r = sqrt(x.^2 + y.^2); r(find(r==0))=eps; % Lose the divide by 0 error hat = sin(r)./r; % Mesh plot of sombrero mesh(hat); title('Sombrero'); pause % Interesting views - Lots of acceptable combinations view(0, 0); % Straight on title('Sombrero view - Straight on'); pause view(-37.5, -30); % From the bottom title('Sombrero view - From below'); pause view(0, 60); % From above title('Sombrero view - From above'); pause view(-37.5, 30) % Restore default % Sombrero with a contour meshc(hat); title('Sombrero with contour'); pause % Sombrero with a reference plane meshz(hat); title('Sombrero with a reference plane (on a pedastal)'); pause % Cap the Sombrero peak(s) to 70% of their value hat(find(hat>=0.7))=0.7; mesh(hat); title('Sombrero - Capped at 70% of it''s peak'); pause % % PART 2 - Plotting with Coordinates % clear all; theta = [0:pi/180:2*pi]; % Full circle. Theta in RADIANS!!! % Equations rho1 = 1 + cos(theta); rho2 = theta / (2*pi); % Display the plots polar(theta, rho1); hold on; polar(theta, rho2); hold off; % What position are the curves the closest? (Ans: 130 degrees) theta(find(abs(rho1-rho2) == min(abs(rho1-rho2)))) * 360 / (2*pi) % Note: The two curves never truely intersect (in continuous time % they do, but not using discretized increments of pi/180 % radians in Matlab). Although the polar plot makes it look % like there are two points of intersection, there is no % intersection in the actual data. % The theta(...) * 360 / (2*pi) just converts a theta value to degrees.