Filter löschen
Filter löschen

Can't get graph to work properly

2 Ansichten (letzte 30 Tage)
manta24
manta24 am 8 Jun. 2018
Kommentiert: manta24 am 8 Jun. 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 Kommentar
Nate Ellingson
Nate Ellingson am 8 Jun. 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Nate Ellingson
Nate Ellingson am 8 Jun. 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)]);

Weitere Antworten (0)

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by