Drawing a graph from a rational function. What is the problem in my code?

I want to draw the graph of function f(x)=x^2-1/x^2-4
This is my code:
clear
clf
f=@(x)((x.^2)-1)./((x.^2)-4);
xa=-10; xb=10; s=0.05; ya=-5; yb=5;
xv=linspace(xa,-s); xh=linspace(s,xb);
plot(xv,f(xv),'blue',xh,f(xh),'blue','linewidth',2)
axis equal, axis([xa xb ya yb]), grid on
xlabel('x'), ylabel('y'), title('function')
hold on
However when draw it, it comes out wrong. It has two blue lines where the x asymptotes should be.

 採用された回答

Star Strider
Star Strider 2014 年 10 月 10 日
It’s necessary to not plot the singularities, then (if you want to), plot the asymptotes there instead. I had to add six lines to replace the singularities with ‘NaN’ values, move the hold line to just below the first plot call, and another two to plot the asymptotes:
f=@(x)((x.^2)-1)./((x.^2)-4);
xa=-10; xb=10; s=0.05; ya=-5; yb=5;
xv=linspace(xa,-s); xh=linspace(s,xb);
yv = f(xv); % Evaluate Function
yh = f(xh);
[mv,vi] = max(yv); % Find Maxima
[mh,hi] = max(yh);
yv(vi) = NaN; % Don’t Connect Singularities
yh(hi) = NaN;
plot(xv,yv,'blue',xh,yh,'blue','linewidth',2)
hold on
plot(xv(vi)*[1 1]', [ya yb]', '--r') % Plot X-Asymptote
plot(xh(hi)*[1 1]', [ya yb]', '--r') % Plot X-Asymptote
axis equal, axis([xa xb ya yb]), grid on
xlabel('x'), ylabel('y'), title('function')
This looks like it does what you want. If you don’t want the asymptotes, don’t include those two lines.

9 件のコメント

Britney
Britney 2014 年 10 月 11 日
Star strider!
That was a beautiful code you made. I do have some questions about the code. The noob I am.
In finding maxima you wrote [mv,vi] and [mh,hi], what variables are they?
What is also, the NaN is new to me. I looked it up in help. It basically doesn't connect undefined numerical values? Am I right?
When you are plotting the asymptotes, you wrote the matrix *[1 1] in it. Why? Is it to make the resulting x values as a [1 1] matrix?
Britney
Britney 2014 年 10 月 11 日
Also, maybe this is me but I think the graph has a small hole in x=0?
Star Strider
Star Strider 2014 年 10 月 11 日
編集済み: Star Strider 2014 年 10 月 11 日
Thank you!
In the maxima, the first of the outputs are the values themselves, the second are the indices of the maximum value in the vector. It makes it easy to find (and if necessary, change) the maximum, and its corresponding value in matching arrays.
NaN is interesting. It actually defined to be the result of (0/0) which is undefined mathematically and is ‘Not a Number’ by convention. (Computers have to have agreed-upon ways of dealing with such operations.) It therefore is ignored by most functions in MATLAB and other languages. Here, I used it to not plot the discontinuities. There are other ways of dealing with discontinuous functions at their discontinuities; setting them to NaN here was the most convenient.
Correct. The plot function has to have a beginning and an end point to draw a line. So to draw a straight line at some single value of x or y, the constant is defined by a two-element vector of that value, and a two-element vector of the range you want to plot it between. Here, I plotted a vertical line (a single value of x) at the value of the asymptotes, at the maximum and minimum values of y.
It’s not you. I thought you wanted the functions not to be evaluated between x=-0.05 and x=+0.05, the reason you defined ‘s’ as you did. To fill the gap, define s=0.
Britney
Britney 2014 年 10 月 12 日
Alright! That answers all my questions.
xoxo
Star Strider
Star Strider 2014 年 10 月 12 日
My pleasure!
Have fun in your Chemistry studies. I was an undergraduate Chemistry major (albeit back when phlogiston was still part of the curriculum).
Britney
Britney 2014 年 10 月 13 日
ha! wow that is way back. Chemistry had to be really fun then I take it.
Stephen23
Stephen23 2014 年 10 月 13 日
編集済み: Stephen23 2014 年 10 月 13 日
They took phlogiston out of the curriculum!? Bah, I bet it was replaced by some newfangled ideas!
Star Strider
Star Strider 2014 年 10 月 13 日
They replaced phlogiston with something called ‘radium’. Never could get comfortable with all that stuff glowing by itself.
Britney
Britney 2014 年 10 月 14 日
haha

サインインしてコメントする。

その他の回答 (2 件)

SK
SK 2014 年 10 月 10 日
編集済み: SK 2014 年 10 月 10 日

0 投票

Since it is a discrete sequence of points, Matlab just connects the last point before -2 with the first point after -2. It has no idea that you want the graph plotted on the entire real line. Same for the asymptote at +2.
The linspace() function takes 100 equally spaced points in the specified interval. So in your case, the function is evaluated at [-10.0000, -9.9005, -9.8010, -9.7015 ...]. The closest it gets to -2 is at -2.0400 and -1.9405.
If you want it to try to hit -2 exactly try the linspace() function with the third argument, N, which tells it how many points you want. See what happens.

1 件のコメント

Britney
Britney 2014 年 10 月 11 日
Thank you for that explanation it was very insightful. I didn't know that that was how Matlab "thinks".

サインインしてコメントする。

SK
SK 2014 年 10 月 10 日
編集済み: SK 2014 年 10 月 10 日

0 投票

Star Strider has a point too. However with the values you have, linspace does not actually pass very close to -2 or 2. (Try linspace(-10, -0.05)). At least on my system there are no out of bounds values involved. However if I use:
f(-10 : 0.1 : -0.05) and f(0.05 : 0.1 : 10)
then I just get two straight lines where the asymptotes are. The reason is that f(-c) and f(c) where c is very-very-very close to 2, is a huge number (but not infinity) so all the other values look very-very-very small -effectively 0 on the graph which explains why the graph looks like that.
Try printing the values f(-10 : 0.1 : -0.05) and you will see that none of them are really 0 or infinity.

1 件のコメント

Britney
Britney 2014 年 10 月 11 日
I like your passion for this question.
Ok! That explains things.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

質問済み:

2014 年 10 月 10 日

コメント済み:

2014 年 10 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by