What does the value s do in my code??

4 ビュー (過去 30 日間)
Britney
Britney 2014 年 10 月 16 日
コメント済み: Britney 2014 年 10 月 16 日
clear
clf
f=@(x)((x.^2)-1)./((x.^2)-4);
xa=-10; xb=-2; s=1; 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,hold on
xlabel('x'), ylabel('y'), title('((x.^2)-1)./((x.^2)-4)')
f=@(x)((x.^2)-1)./((x.^2)-4);
xa=-2; xb=2; s=0; 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,
f=@(x)((x.^2)-1)./((x.^2)-4);
xa=2; xb=10; s=1; ya=-5; yb=5;
xv=linspace(xa,-s); xh=linspace(s,xb);
%xv=linspace(xa,xb);
plot(xv,f(xv),'blue',xh,f(xh),'blue','linewidth',2)
axis equal, axis([-5 5 ya yb]), grid on,
Can anyone explain what s does in my equation. When I tried making the graph from the helpful input from my earlier question (thank you everyone) the problem was that the graph had two blue lines where the asymptotes should be. I splitted the x-values in 3 parts and made s=1 instead of s=0 for the two graphs above y=1 and then my graph worked the way it should. This however has not made me any wiser to what s means and does...Can anyone explain?
  1 件のコメント
Britney
Britney 2014 年 10 月 16 日
Also how do you plot the
title('((x.^2)-1)./((x.^2)-4)')
so that it does not have the matlab structure to it. I want the title to look like you write the equation if anyone understand me.

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

採用された回答

Star Strider
Star Strider 2014 年 10 月 16 日
The ‘s’ variable sets the upper limit of ‘xv’ and the lower limit of ‘xh’. It was in your original code, so I assumed you wanted it kept in the revised code I posted. You can set ‘s’ to be whatever you want.
The call to axis:
axis([xa xb ya yb])
sets the axis limits. It is independent of ‘s’.
The title will display as close to ‘normal’ as possible by removing the dot-operators:
title('(x^2-1)/(x^2-4)')
  3 件のコメント
Star Strider
Star Strider 2014 年 10 月 16 日
My pleasure! My polar bear thanks you!
Britney
Britney 2014 年 10 月 16 日
haha

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

その他の回答 (2 件)

Britney
Britney 2014 年 10 月 16 日
Is that it goes from -10 to -1 from
xv=linspace(xa, -s)
?
But why not from -10 to -2? That has to be equated to axis([xa xb ya yb])?

Matt Tearle
Matt Tearle 2014 年 10 月 16 日
This code is... a bit muddled. At the end of the day, the s is not really necessary. The problem you were having before with the line across the asymptotes is that MATLAB simply calculates the values in the x vector and joins the points up, so on one side of the asymptote (x = -2.mumble) y is 10^23 (or whatever) and the next x value on the other side (x = -1.9mumble) y = -10^17. The simplest way to fix that is just split the x vector into three pieces ([-5,-2], [-2,2], [2,5]).
Your code kinda does that in a roundabout way, but you can make it much cleaner by just making three x vectors and plotting the corresponding y values for each:
f=@(x)((x.^2)-1)./((x.^2)-4);
xa=-5; xb=-2;
x1=linspace(xa,xb);
x2=linspace(xb,-xb);
x3=linspace(-xb,-xa);
plot(x1,f(x1),'blue',x2,f(x2),'blue',x3,f(x3),'blue','linewidth',2)
Regarding your second question, the input to title is just a string, so you can put anything you want there:
title('(x^2-1)/(x^2-4)')
MATLAB will interpret the carats as exponent markup, which is nice. But you can also go completely nuts if you know LaTeX:
title('$$\frac{x^2-1}{x^2-4}$$','Interpreter','latex')
  1 件のコメント
Britney
Britney 2014 年 10 月 16 日
編集済み: Britney 2014 年 10 月 16 日
Thank you very much for answering. You didn't tell much about the s variable but I really appreciated your input about my function code.
p.s cute kiddo on avatar

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

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by