% This m-file draws mapping diagrams. Before running it, you need to enter, % at the MatLab prompt, a "string" s which gives the function you want to % study. This is very simple. Suppose you want to study x*x - sin(x). At % the >> prompt in the command window you type in: % % s = 'x*x - sin(x)' % % (Don't forget the single quotes!); then you run the program mapdiag. The % whole transaction looks like: % % >> s = 'x*x - sin(x)' % s = % x*x - sin(x) % >> mapdiag % % Once you have entered the function s = 'x*x-sin(x)' say, each time you % run mapdiag it will plot this function. To change functions, simply % enter a new "function string" at the main command prompt >>; for example: % >> s = '3*x^3 - 2*x^2 -sqrt(x) + 10' (note that powers are entered using % the "^" sign (above the 6 on the top keyboard row). % % Mapdiag draws the "axes" as the vertical lines at x=0 (input % axis) and x=1 (output axis), and draws mapping lines connecting x (on % input axis) with f(x) (on the output axis). % Mapdiag allows a larger view of these lines. This is set by prompts for % the left and right boundaries of the viewing "window". If these are set % to 0 and 1 respectively, you get just the region between the axes. If you % set them to, say, -1 and 2 respectively, then you'll see more of the % mapping lines to the left and right of the vertical input and output % axes. % % Next, you enter the starting value (x) on the input axis and ending % value. The lines start and end at these x values. The next prompt asks % for N, the number of lines to be drawn. Mapdiag actually takes this many % SUBDIVISIONS of (end x) - (start x), so draws N + 1 lines. % % Finally, when the plot is on the screen, you can zoom in or out by left or % right clicking, or dragging a region. disp(['Plotting: ', s]) ff=inline(s,'x'); % This creates the function from the string LeftWin = input('Enter window left: '); RightWin= input('Enter window right: '); FirstX = input('Enter starting x: '); LastX = input('Enter ending x: '); NumLines = input('Enter number of lines: '); hold on for LL = FirstX:(LastX - FirstX)/NumLines:LastX % Actually draws NumLines + 1 x = [LeftWin,RightWin]; % Begin and end x coordinates y=(feval(ff,LL) - LL)*x + LL; % Begin and end y coordinates plot(x,y,'b') end % xlim=get(gca,'XLim'); ylim=get(gca,'YLim'); x=[0,1;0,1]; y=[ylim(1),ylim(2)]; plot(x,y,'r') %linePQ(0,ylim(1),0,ylim(2),'r') %linePQ(1,ylim(1),1,ylim(2),'r') title(['Mapping Diagram for \it',s]) zoom on