Trouble graphing. My function is not appearing.
Mostrar comentarios más antiguos
x = linspace(0,10,100)
y = (2+x)/(2-x)
plot (x, y)
Respuesta aceptada
Más respuestas (1)
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
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
plot (x, y)
2 comentarios
Alex Chen
el 7 de Mayo de 2022
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]);
Categorías
Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



