Lecture4: Plotting Lecture4: Plotting 1
Plotting in MATLAB 2D Plots Plotting Scalar functions Plot f ( x ) = x 2 on [ − 2 π, 2 π ]. Define a discrete set of values uniformly distributed on the domain of f . 1 Evaluate f at each point in the discrete domain. 2 Plot! 3 %step 1 : define discrete domain 1 x=linspace(-2*pi,2*pi,100); 2 3 %step2: Evaluate each element of the vector x 4 %component-wise as: 5 yvals = x.ˆ2; 6 7 %step 3: call plot with an option to specify line width 8 plot(x,yvals,'LineWidth',3); 9 title('$y=xˆ2$ \ quad ($n=100$)', 'Interpreter','latex') 10 xlabel('x'); ylabel('y'); 11 Lecture4: Plotting 2
Plotting in MATLAB 2D Plots Plotting Scalar functions Plot of f ( x ) = x 2 on [ − 2 π, 2 π ]. Lecture4: Plotting 3
Plotting in MATLAB 2D Plots Plotting Scalar functions: axis tight %step 1 : define discrete domain 1 x=linspace(-2*pi,2*pi,100); 2 3 %step2: Evaluate each element of the vector x 4 %component-wise as: 5 yvals = x.ˆ2; 6 7 %step 3: call plot with an option to specify line width 8 plot(x,yvals,'LineWidth',3); 9 title('$y=xˆ2$ \ quad ($n=100$)', 'Interpreter','latex') 10 xlabel('x') ylabel('y') 11 axis tight; 12 Lecture4: Plotting 4
Plotting in MATLAB 2D Plots Plotting Scalar function: axis tight Plot of f ( x ) = x 2 on [ − 2 π, 2 π ]. Lecture4: Plotting 5
Plotting in MATLAB 2D Plots Caution It is important to choose a large enough number of sample points in the domain. If n is too small, e.g. n = 10, the quality of the plots is poor. Lecture4: Plotting 6
Plotting in MATLAB 2D Plots Multiple plots Plot y 1 = sin ( x ) , y 2 = 2 cos ( x ) , y 3 = 3 cos (2 x ) on the same axis y1 = sin(x); 1 y2 = 2*cos(x); 2 y3 = 3*cos(2*x); 3 %pass the "domain" and "range" for each function to plot 4 plot(x,y1,x,y2,x,y3,'LineWidth',2) 5 title('Mutiple plots') 6 xlabel('x') 7 ylabel('y') 8 %add legend 9 legend('sin(x)','2cos(x)','3cos(2x)') 10 axis tight 11 Lecture4: Plotting 7
Plotting in MATLAB 2D Plots Multiple plots Plot of y 1 = sin ( x ) y 2 = 2 cos ( x ) y 3 = 3 cos (2 x ) Lecture4: Plotting 8
Plotting in MATLAB 2D Plots Multiple plots: hold function Plot of y 1 = sin ( x ) y 2 = 2 cos ( x ) y 3 = 3 cos (2 x ) y1 = sin(x); 1 y2 = 2*cos(x); 2 y3 = 3*cos(2*x); 3 plot(x,y1,'LineWidth',2) %plot 1 4 hold on % retains the current axis and to add more plots 5 plot(x,y2,'LineWidth',2) % plot 2 6 plot(x,y3,'LineWidth',2) % plot 3 7 title('Mutiple plots') 8 xlabel('x') 9 ylabel('y') 10 %add legend with option to specify its location 11 legend('sin(x)','2cos(x)','3cos(2x)', ... 12 'Location','SouthWest') %make the plot box fit tightly around the data 13 axis tight 14 hold off 15 Lecture4: Plotting 9
Plotting in MATLAB 2D Plots Multiple plots Plot of y 1 = sin ( x ) y 2 = 2 cos ( x ) y 3 = 3 cos (2 x ) Lecture4: Plotting 10
Plotting in MATLAB 2D Plots Customizing plots 2D Plot options y1 = sin(x); 1 y2 = 2*cos(x); 2 y3 = 3*cos(2*x); 3 plot(x,y1,'rv','LineWidth',2) % red triangles (no line) 4 hold on % retains the current axis and to add more plots 5 plot(x,y2,'g--*','LineWidth',2) % plot 2: green dashed + * 6 plot(x,y3,'b:d','LineWidth',2) %plot 3: blue dotted + ... 7 diamond title('Mutiple plots') 8 xlabel('x') 9 ylabel('y') 10 %add legend with option to specify its location 11 legend('sin(x)','2cos(x)','3cos(2x)', ... 12 'Location','SouthWest') %make the plot box fit tightly around the data 13 axis tight; 14 hold off 15 Lecture4: Plotting 11
Plotting in MATLAB 2D Plots Multiple plots - customized Plot of y 1 = sin ( x ) y 2 = 2 cos ( x ) y 3 = 3 cos (2 x ) Lecture4: Plotting 12
Plotting in MATLAB 2D Plots subplot - creates array of plots x=linspace(-5*pi,5*pi,200); 1 % Evaluate your functions on x 2 y1 = sin(x); y2 = 2*sin(x); y3 = sin(x/2); y4 = sin(2*x); 3 4 % We want to plot 4 functions on a 2 by 2 grid 5 subplot(2,2,1) % subplot 1 6 plot(x,y1) 7 title('Subplot 1: sin(x)'); 8 9 subplot(2,2,2) 10 plot(x,y2,'g') % subplot2 11 title('Subplot 2: 2sin(x)'); 12 13 subplot(2,2,3) % subplot 3 14 plot(x,y3,'k') 15 title('Subplot 3: sin(x/2)'); 16 17 subplot(2,2,4) %subplot 4 18 plot(x,y4,'r') 19 title('Subplot 4: 2sin(2x)'); 20 Lecture4: Plotting 13
Plotting in MATLAB 2D Plots subplot - creates array of plots Lecture4: Plotting 14
Plotting in MATLAB 2D Plots subplot - axis properties ax1=subplot(2,2,1) % subplot 1 1 plot(x,y1) 2 title('Subplot 1: sin(x)'); 3 4 ax2=subplot(2,2,2) 5 plot(x,y2,'g') % subplot2 6 title('Subplot 2: 2sin(x)'); 7 8 ax3=subplot(2,2,3) % subplot 3 9 plot(x,y3,'k') 10 title('Subplot 3: sin(x/2)'); 11 12 ax4=subplot(2,2,4) %subplot 4 13 plot(x,y4,'r') 14 title('Subplot 4: 2sin(2x)'); 15 16 %I can then spacify common axis properties 17 axis([ax1 ax2 ax3 ax4], [-5*pi, 5*pi, -2, 2]) 18 Lecture4: Plotting 15
Plotting in MATLAB 2D Plots subplot - axis properties Lecture4: Plotting 16
Plotting in MATLAB 2D Plots Other plots Parametric plot: x = 5 cos ( t ) , y = 3 sin ( t ) , t ∈ [0 , 2 π ]. t = linspace(0,2*pi); 1 x = 5*cos(t); 2 y = 3*sin(t); 3 plot(x,y,'LineWidth',2) 4 xlabel('x') 5 ylabel('y') 6 title('Parametric plot'); 7 axis equal % same length for the units along axis 8 axis([-6 6 -6 6]) 9 Polar curves of the from r , θ - use polarplot(theta, r) . Lecture4: Plotting 17
Plotting in MATLAB 2D Plots Parametric plot Lecture4: Plotting 18
Plotting in MATLAB 2D Plots Parmateric plot - customize axis properties axis properties t = linspace(0,2*pi); 1 x = 5*cos(t); 2 y = 3*sin(t); 3 ax=gca; % returns current axis 4 plot(x,y,'LineWidth',2) 5 xlabel('x') 6 ylabel('y') 7 title('Parametric plot'); 8 axis equal % use the same length for the data units ... 9 along each axis axis([-6 6 -6 6]) 10 %set other axis properties 11 ax.FontSize =15; 12 ax.LineWidth =2; 13 ax.XTick =-6:2:6; 14 ax.YTick = -6:2:6; 15 ax.XMinorTick='on'; 16 ax.YMinorTick='on'; 17 Lecture4: Plotting 19
Plotting in MATLAB 2D Plots Parametric plot Lecture4: Plotting 20
Plotting in MATLAB 3D Plots Vector functions and Space curves A vector-valued function or vector function is a function whose domain is the set of real numbers and whose range is a set of vectors. If we let t be in the domain and f ( t ) , g ( t ) and h ( t ) be scalar functions, we can write a vector valued function r ( t ) = � f ( t ) , g ( t ) , h ( t ) � = f ( t ) i + g ( t ) j + h ( t ) k Suppose that f , g and h are continuous real-valued functions on an interval I . The set C of all points ( x , y , z ) where x = f ( t ) y = g ( t ) z = h ( t ) and t varies on I is called a space curve. Lecture4: Plotting 21
Plotting in MATLAB 3D Plots Space curve Example x = t cos t , y = t , z = t sin t , t ∈ [0 , 10 π ] t =linspace(0,10*pi,1000); 1 %define x, y, z (component-wise) 2 x= t.*cos(t); 3 y= t; 4 z = t.*sin(t); 5 plot3(x,y,z,'LineWidth',2); 6 xlabel('x'),ylabel('y'),zlabel('z') 7 title('Space curve'); 8 grid on 9 Lecture4: Plotting 22
Plotting in MATLAB 3D Plots Space curve Lecture4: Plotting 23
Plotting in MATLAB 3D Plots Plotting surfaces ( z = f ( x , y )) Step 1: Define two vectors containing the x and y coordinates of the discrete domain. Step 2: Create the 2D grid coordinates X and Y using mesh grid [X,Y] = meshgrid(x,y) . Step 3: Evaluate z = f ( x , y ) at all points on the mesh grid. Step 4: Plot the surface using mesh or surf . Lecture4: Plotting 24
Plotting in MATLAB 3D Plots meshgrid > x=1:4 1 > x = 2 1 2 3 4 3 > y=2:6 4 > y = 5 2 3 4 5 6 6 > [X,Y]=meshgrid(x,y) 7 > X = 8 1 2 3 4 9 1 2 3 4 10 1 2 3 4 11 1 2 3 4 12 1 2 3 4 13 Y = 14 2 2 2 2 15 3 3 3 3 16 4 4 4 4 17 5 5 5 5 18 6 6 6 6 19 Lecture4: Plotting 25
Plotting in MATLAB 3D Plots 2D domain >> plot(X,Y,'r*') Lecture4: Plotting 26
Plotting in MATLAB 3D Plots Plot a plane z = x + y x=1:4; 1 y=2:6; 2 [X,Y]=meshgrid(x,y); %create discrete domain 3 Z =X+Y; % evaluate f(x,y) 4 mesh(X,Y,Z); %plot 5 title('z=x+y'); 6 xlabel('x') 7 ylabel('y') 8 zlabel('z') 9 Lecture4: Plotting 27
Plotting in MATLAB 3D Plots f ( x , y ) = x + y Lecture4: Plotting 28
Plotting in MATLAB 3D Plots f ( x , y ) = sin ( x ) − sin ( y ) [X,Y]=meshgrid(linspace(-10,10,200)); %create square grid 1 Z = sin(X)-sin(Y); % evaluate z 2 surf(X,Y,Z); %plot 3 Lecture4: Plotting 29
Plotting in MATLAB 3D Plots f ( x , y ) = sin ( x ) − sin ( y ) axis equal [X,Y]=meshgrid(linspace(-10,10,200)); %create square grid 1 Z = sin(X)-sin(Y); % evaluate z 2 surf(X,Y,Z); %plot 3 axis equal 4 This gives a better perception of the 3D plot Lecture4: Plotting 30
Plotting in MATLAB 3D Plots surf vs mesh surf - turns face coloring on by default and uses black edges mesh - turns off face coloring and uses colored edges Lecture4: Plotting 31
Plotting in MATLAB 3D Plots f ( x , y ) = sin ( x ) − sin ( y ) mesh [X,Y]=meshgrid(linspace(-10,10,200)); %create square grid 1 Z = sin(X)-sin(Y); % evaluate z 2 mesh(X,Y,Z); %plot 3 axis equal 4 Lecture4: Plotting 32
Recommend
More recommend