# What does the value s do in my code??

조회 수: 4(최근 30일)
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 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 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표시숨기기 이전 댓글 수: 2
Britney 2014년 10월 16일
haha

댓글을 달려면 로그인하십시오.

### 추가 답변(2개)

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])?
##### 댓글 수: 0표시숨기기 이전 댓글 수: -1

댓글을 달려면 로그인하십시오.

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 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

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by