Borrar filtros
Borrar filtros

Can't get graph to work properly

2 visualizaciones (últimos 30 días)
manta24
manta24 el 8 de Jun. de 2018
Comentada: manta24 el 8 de Jun. de 2018
Hi, I'm new to building general functions on Matlab and am having trouble getting this one to work. My goal is to write a simple general function where the user can input two points and have a line drawn through them, as well as have the slope displayed. However, my points are not being graphed correctly and I cannot figure out why. If anyone could take a look and see where I went wrong, I would appreciate it.
My code is:
function simple_plot(t0,tf,r,t) %A simple plotting function, where t0 is initial point, tf is final point, %r is desired range and t is title. %Please enter t0,tf, range and title. plot([t0],[tf]) a=t0; b=tf; x1=a(1,1); x2=b(1,1); y1=a(1,2); y2=b(1,2); m = ((y2-y1)./(x2-x1)); slope = ['The slope of the line is: ',num2str(m)]; disp(slope) axis([r]); title(t) end
  1 comentario
Nate Ellingson
Nate Ellingson el 8 de Jun. de 2018
function simple_plot(t0,tf,r,t)
%A simple plotting function, where t0 is initial point, tf is final point,
%r is desired range and t is title.
%Please enter t0,tf, range and title.
plot([t0],[tf])
a=t0;
b=tf;
x1=a(1,1);
x2=b(1,1);
y1=a(1,2);
y2=b(1,2);
m = ((y2-y1)./(x2-x1));
slope = ['The slope of the line is: ',num2str(m)];
disp(slope)
axis([r]);
title(t)
end

Iniciar sesión para comentar.

Respuesta aceptada

Nate Ellingson
Nate Ellingson el 8 de Jun. de 2018
plot needs an x vector and a y vector, not two points. so you'ld need to do something like this
plot([t0(1) tf(1)], [t0(2) tf(2)]);
  1 comentario
manta24
manta24 el 8 de Jun. de 2018
That worked perfectly, thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by