## Plotting Mathematical Functions Using "fplot"

Plot the following mathematical functions using the "fplot" and “subplot” functions of MATLAB for MATLAB domains -2π < x < 2π (on the top of the figure) and -30π < x < 30π (on the bottom of the figure). The title of the top plot should include the mathematical function and type of function on the top line and the MATLAB domain (as a string) on a second line (as shown in the example, below). The title of the bottom plot should just include the MATLAB domain. Please ensure the x and y axes are properly labelled exactly as shown in the example. The drawing of the plots for a mathematical function should be done in the plot2domains function, in the plot2domains.m file. The function should return an array of 2 function line objects, one for each call to fplot. Call the plot2domains function for each mathematical function provided in MATLAB symbolic form, and think about what you see. The expected output figure is given for Question 1a. (15/100)

## Using "fplot" Function in MATLAB

Using the "fplot" function of MATLAB, plot the following mathematical function, its first and second derivatives in one figure and in that order. x limits for the plot should be 2 to 8, and vertical (y) limits for the plot should be -15 to 30. Identify all local maxima, local minima, inflection points, and vertical asymptotes of the mathematical function. After plotting the three lines, circle all the local maxima in red, all local minima in green, and all the inflection points in blue. Vertical asymptotes should be indicated with black ‘x’s on the bottom and top borders as shown (the fplot function of MATLAB draws dashed lines for the vertical asymptotes automatically). Each type of point must be drawn in the order identified above. For each type, all points of that type must be plotted all at once except for the ‘x’s for the vertical asymptotes, for which the bottom ‘x’s should be plotted before the top ‘x’s. Please include the title, x axis label and legend in the plot that are identical to those shown in the Figure below. The implementation of these requirements should be done in a function in a file Q2.m. Function Q2 takes no input and returns the following:

Output = {fplotH; lmaxPlot; lminPlot; inflPlot; verAsymPlotBottom};

wherefplotH returns the function line object returned for the plot of f(x), and the other objects are the lineseries/line objects returned for the plots of the local maxima, local minima, inflection points, and thebottom markers for vertical asymptotes. (40/100)

rational function: (2*x^4 - 40*x^3 + 299*x^2 - 989*x + 1219)/(x^2 - 10*x + 23)

## Creating a MATLAB Function that Plots with "fPlot"

Create a MATLAB function that plots with fplot a mathematical function with limits for x determined according to the instructions below. The function should be implemented in the auto_fplot.m file provided.

Output = auto_fplot(func)

Inputs: a mathematical function in x in symbolic form (func)

Output: a cell array as described below.

This auto_fplot function should meet the following requirements.

1) After the mathematical function has been plotted, the auto_fplot function should be able to plot a set of interesting points (i.e. local maxima, local minima, inflection points, and two points for each vertical asymptote) for as many types of functions as possible. These should work for any polynomial equation. Vertical asymptotes should work for any rational function. All local maxima should be indicated with red circles, all local minima should be indicated with green circles and all inflection points should be indicated with blue circles. Vertical asymptotes should be indicated with black ‘x’s on the bottom and top borders (the fplot function of MATLAB draws dashed lines for the vertical asymptotes automatically). Each type of point must be drawn in that order if that type of point exists for the mathematical function. For each type, all points of that type must be plotted all at once and only if at least one point of that type exists. The “region of interest” or “domain of interest” (i.e. portion between the two extreme “interesting” points if

there is a finite number of such points) should be in the middle and occupy 70% of the x limits of the plot.

then set the limits of x to be

If there is no “interesting” point found (or an infinite number of interesting points1), plot the mathematical

function and set the limits of x to be from -20 to 20 . And if there is only one interesting point, xinteresting,

(xinteresting − 20 ) (xinteresting + 20 ).

Function auto_fplot returns the following:

Output = {fplotH; lmaxPlot; lminPlot; inflPlot; verAsymPlotBottom};

wherefplotH returns the function line object returned for the plot of f(x), and the other objects are the lineseries/line objects returned for the plots of the local maxima, local minima, inflection points, and thebottom markers for vertical asymptotes.

2) The mathematical function as a title, axes labels (i.e., ‘x’ for x axis and ‘f(x)’ for y axis) and legend (i.e., ‘f(x)’, ‘Local Maxima’, ‘Local Minima’, 'Inflection Points', and ‘Vert. Asymptotes’ as needed) should be included in the plot. Please make sure all your strings match these instructions. Only include in the legend those types of interesting points that exist for the mathematical function being plotted. These legend entries should be shown as plural even if there is only one point of a certain type.

3) Please complete the help comments at the top of the file. Do not modify the characters

“%output = AUTO_FPLOT (f” before the “…”

Bonus requirements:

4) Can you include the location of any horizontal asymptotes? A left horizontal asymptote can be plotted as a green ‘x’ on the left border, and a right horizontal asymptote can be plotted as a red ‘x’ on the right border. Plot the left horizontal asymptote value, if it exists, after all points mentioned in paragraph 1 above, and plot the right horizontal asymptote value, if it exists, last. Depending on which horizontal asymptote(s) may or may not exist, the legend can have entries “Left Asymptote” and/or “Right Asymptote” (note that these are grammatically singular).

5) (a) If the input is a periodic function, plot 2 periods of the function including the point f(0) (b) try to plot maxima, minima, inflection points, and vertical asymptotes in the 2 periods as described in paragraph 1 and

(c) hopefully with a periodic maximum at the left and right borders of the plot.

6) If you believe that some “interesting” points exist, but you cannot solve for them symbolically, just remove those interesting points from the collection that you are considering and then apply the rules to any remaining interesting points to determine the x limits of the figure.

Your implementation will likely be evaluated using a set of test functions, and some mathematical test functions may be provided. In any case, write your own mathematical functions to test your auto_fplot function against. Your auto_fplot function should work for any reasonable mathematical function, especially simple functions. You have been warned! For now, you can test your auto_ fplot function with mathematical functions including those shown in Question 1 and Question 2. Students in the class are highly encouraged to share their mathematical testing functions on Piazza. You can also include your output figures for other students to compare with.

(45/100 + 15 bonus)

## Auto FPlot

%Output = AUTO_FPLOT (f... *** finish the help comments ***

% Student Name 1: student1

% Student 1 #: 123456781

% Student 1 userid (email): stu1 (stu1@sfu.ca)

% Student Name 2: student2

% Student 2 #: 123456782

% Student 2 userid (email): stu2 (stu2@sfu.ca)

% Below, edit to list any people who helped you with the assignment,

% or put ‘none’ if nobody helped (the two of) you.

% Helpers: _everybody helped us/me with the assignment (list names or put ‘none’)__

function Output = auto_fplot (func)

figure;

syms x;

lmaxPlot=[];

lminPlot=[];

inflPlot=[];

verAsymPlotBottom=[];

% place your work here

lastwarn(''); %clear warning message

[resultDy, paramDy, ~] = solve(func, x, 'ReturnConditions', true);

% test if the function someFunction is symbolically solvable

isFuncSymSolvable = ~(strcmp(lastwarn, 'Cannot find explicit solution.'));

fplotH = fplot(func); % this line might need to be modified

Output = {fplotH; lmaxPlot; lminPlot; inflPlot; verAsymPlotBottom};

end