% A MATLAB guide to graph functions and curves from a data set. clear; % Grahing functions previously defined in a file named "f.m" % and "f2.m" both located in the same directory where this main % file is located. The approach is to obtain a set of points in the plane % (x,y) by evaluating the function at the given set of points x. % Interval [A,B], where the functions are going to be graphed. A=-5; B=5; % Number of points to be used in the plot command to % obtain the graphs. N=100; % Step size. Distance between points in x-axis for a uniform % partition of the interval [A,B]. step=(B-A)/(N-1); % Uniform partition of the inteval [A,B] with stepsize step. % It creates a row vector (x(1),....x(n)) of N components. x = A:step:B; % With the next command we will create a Row vector "y" whose % components are y(i)=f(x(i)) y = f1(x); % Once the two vectors x and y have been created the command plot % will make a graph in the x-y plane using these N (x(i),y(i)) % points by joining them with straight segment between % consecutive points. plot(x,y); % There is an easier way to create a graph once the function has been % defined without creating vectors x and y. We only need to specify the % ends of the interval where the function is going to be graphed. MATLAB % will use a default number of points to construct the graph. % We will use a separate coordinate plane for this graph by defining it % just before writing the ezplot command. figure; ezplot('f1',-5,5); % Now, we want to plot the two graphs of f(x) and f2(x) in the % same coordinate plane. % First we will create a new coordinate plane. figure; % Then, we generate the z vector containing the values of z(i)=f2(x(i)). z=f2(x); % Now we use the command plot with some more parameters. plot(x,y,x,z); % We can especify the colors of the graphs figure; plot(x,y,'r',x,z,'b'); % We can draw the graphs using noncontinuous curves, just ploting the % points where the functions have been evaluated. figure; plot(x,y,'--',x,z,'*'); % Now, we will draw a curve passing through the points defined % by (x,y) and at the same time mark the points in the graph figure; x=[1,3,5,6,7,9,10]; y=[5;-10;9;-1;-3;4;8]; plot(x,y,x,y,'*'); % Now some fun this is how I create an animated plot. % I start with 10 interpolation points and increase them to 100 by adding % one point per iteration. If we do not create a new coordinate plane using % the "figure" command the graphs will be superimposed and this is how the % animation results. You can graduate the speed of the animation by % increasing or decreasing the number inside the "pause( )" command % OK HAVE FUN!!!!! N=10; Nmax=100; for NN=N:Nmax step=(B-A)/(NN-1); x=A:step:B; y=f1(x); plot(x,y,x,y,'*'); pause(0.001); end % Here we will show how to draw curves by pieces in the same graph. % It is useful for Bezier curves construction. % Consider the vectors x1, y1, x2, y2. x1=[1,3,4,8,10]; y1=[-2;4;9;3;-6]; x2=[10,12,13,14,15]; y2=[-6;3;5;2;7]; %Here we we will create a graph where a first piece is obtained % then a "hold on" command is given, and then a second portion % in another part of the domain is graphed. % 'g-' means use a green continuous line to joint neighbor points. % 'm-' same for magenta color. % '*' means mark the point (x1,y1) with a star in the plane. figure; plot(x1,y1,'g-'); hold on; plot(x1,y1,'*'); hold on; plot(x2,y2,'m-',x2,y2,'*');