How to Graph a Function

11 views (last 30 days)
Bob Donte
Bob Donte on 5 Jun 2015
Answered: Nikhil Verghese on 5 Oct 2018
I was wondering if anyone could write this code. Im having an issue understanding what they want me to write. The question is "Plot the function f(x)=x^2-5x-12/x^2-x-6 in the domain -1<x<7. Notice the function had a vertical asymptote at x = 3. Plot the function by creating two vectors for the domain of x. The first vector (name it x1) includes elements from -1 to 2.9, and the second vector (x2) includes elements from 3.1 to 7. For each x vector create a y vector (name them y1 and y2) with the corresponding values of y according to the function. To plot the function make two curves in the same plot (y1 vs. x1 and y2 vs x2) Format th plot such that the y-axis ranged from -20 to 20

Answers (3)

Joseph Cheng
Joseph Cheng on 5 Jun 2015
Edited: Joseph Cheng on 5 Jun 2015
As its homework probably not, but like any problem break it down into sections.
you have a function f(x) = x^2-5x-12/(x^2-x-6). **note your cut and paste of the question is missing the () around the denominator and possibly the numerator like in Star's anonymous function. you will not get the vertical asymptote at x=3 otherwise.
  1. calculate y1=f(x1) with x1=-1 to 2.9 (using -1:dx:2.9 or linspace())
  2. calculate y2=f(x2)with x2=3.1 to 7 (using -1:dx:2.9 or linspace())
  3. plot using the function plot() *read the documentation on plot*
  4. change the y axis limits using the function ylim()

Star Strider
Star Strider on 5 Jun 2015
You can make this a bit easier by creating an anonymous function from your function:
f = @(x) (x.^2-5*x-12)./(x.^2-x-6);
It works the way you would expect a function to work. Note the vectorisation with (.^) and the rest, to do element-wise operations rather than vector or matrix operations.
For the rest, the linspace, axis, and plot functions are your friends.

Nikhil Verghese
Nikhil Verghese on 5 Oct 2018
f=@(x) sin(x)

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!