What does the value s do in my code??

4 次查看(过去 30 天)
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)')

更多回答(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 CenterFile Exchange 中查找有关 2-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