Can't get graph to work properly

1 次查看(过去 30 天)
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 个评论
Nate Ellingson
Nate Ellingson 2018-6-8
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

请先登录,再进行评论。

采纳的回答

Nate Ellingson
Nate Ellingson 2018-6-8
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)]);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by