Trouble graphing. My function is not appearing.

 Respuesta aceptada

Sam Chak
Sam Chak el 7 de Mayo de 2022
If you want to see the asymptote, perhaps use fplot() instead.
fplot(@(x) (2+x)./(2-x), [0 4])

Más respuestas (1)

Voss
Voss el 7 de Mayo de 2022
Editada: Voss el 7 de Mayo de 2022
Use element-wise division (./).
With matrix division (/) y is a scalar, and plotting a scalar y vs a vector x creates one line for each element of x, but each line contains only one point, so you can't see them without a data marker:
x = linspace(0,10,100);
y = (2+x)/(2-x) % y is a scalar: plot makes a line for each x value
y = -1.6856
plot (x, y, '.') % plot with a marker to see the lines
Using element-wise division makes y a vector the same size as x, so you get one line, as intended:
figure()
y = (2+x)./(2-x) % y is a vector
y = 1×100
1.0000 1.1064 1.2247 1.3571 1.5063 1.6757 1.8696 2.0938 2.3559 2.6667 3.0408 3.5000 4.0769 4.8235 5.8276 7.2500 9.4211 13.1429 21.0000 48.5000 -199.0000 -34.0000 -19.0000 -13.3750 -10.4286 -8.6154 -7.3871 -6.5000 -5.8293 -5.3043
plot (x, y)

2 comentarios

Alex Chen
Alex Chen el 7 de Mayo de 2022
Thanks, that makes sense! But it doesn't seem to match with the same function graphed on desmos as it shows an x=2 asymptote, and the matlab veresion has a line joing the function and making it continuous?
Image Analyst
Image Analyst el 7 de Mayo de 2022
Editada: Image Analyst el 7 de Mayo de 2022
Change your x range
x = linspace(-15, 15, 1980);
y = (2+x)./(2-x) % y is a vector
% Make middle invisible.
[~, index] = min(abs(x-2));
y(index) = nan;
ylim([-15, 10]);

Iniciar sesión para comentar.

Categorías

Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 7 de Mayo de 2022

Editada:

el 7 de Mayo de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by