How to Graph a Function

조회 수: 11 (최근 30일)
Bob Donte
Bob Donte 2015년 6월 5일
답변: Nikhil Verghese 2018년 10월 5일
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

답변 (3개)

Joseph Cheng
Joseph Cheng 2015년 6월 5일
편집: Joseph Cheng 2015년 6월 5일
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 2015년 6월 5일
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 2018년 10월 5일
f=@(x) sin(x)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by