What does the value s do in my code??
5 views (last 30 days)
Show older comments
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?
Accepted Answer
Star Strider
on 16 Oct 2014
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 Comments
More Answers (2)
Matt Tearle
on 16 Oct 2014
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')
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!