Can't get graph to work properly
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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
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
Respuesta aceptada
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)]);
Más respuestas (0)
Ver también
Categorías
Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!