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
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
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 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!