How can I plot y=mx+b in Matlab?
38,983
Solution 1
There are two way that immediately come to mind. The first is with FPLOT:
>> m = 2; b = 1; >> fplot(@(x)m*x+b, [0 10]);
The second is to calculate the y values directly in the call to the PLOT command:
>> m = 2; b = 1; x = 1:10; >> plot(x, m*x+b);
Solution 2
There is REFLINE function in Statistical Toolbox. Probably the easiest for your task:
refline(m,b)
or if you want to change line properties:
hr = refline(m,b);
set(hr,'Color','r')
It uses limits from the current axes. So if you change the limits later, it probably would be easier to delete it (delete(hr)
) and draw again.
Author by
Alex Nichols
Updated on February 01, 2020Comments
-
Alex Nichols over 4 years
I was wondering if it is possible to plot a line of the form
y = mx+b
in Matlab? I usedpolyfit
to get a 1x2 array that contains the slope and intercept.Here is what I have so far:
lineFit = polyfit(tauBin, a5array, 1); plot((lineFit(1)*x + lineFit(2)))
How can I plot this?