如何在Matlab中画y=mx+b ?

时间:2021-06-14 04:16:40

I was wondering if it is possible to plot a line of the form y = mx+b in Matlab? I used polyfit to get a 1x2 array that contains the slope and intercept.

我想知道是否可以在Matlab中画出y = mx+b的直线?我使用了polyfit来得到一个包含斜率和截距的1x2数组。

Here is what I have so far:

以下是我目前所拥有的:

lineFit = polyfit(tauBin, a5array, 1);
plot((lineFit(1)*x + lineFit(2)))

How can I plot this?

怎么画呢?

2 个解决方案

#1


13  

There are two way that immediately come to mind. The first is with FPLOT:

有两种方法立刻浮现在脑海中。第一个是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:

第二种方法是在调用PLOT命令时直接计算y值:

>> m = 2; b = 1; x = 1:10;
>> plot(x, m*x+b);

#2


4  

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.

它利用了当前轴的极限。因此,如果您稍后更改限制,可能会更容易删除它(删除(hr))并再次绘制。

#1


13  

There are two way that immediately come to mind. The first is with FPLOT:

有两种方法立刻浮现在脑海中。第一个是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:

第二种方法是在调用PLOT命令时直接计算y值:

>> m = 2; b = 1; x = 1:10;
>> plot(x, m*x+b);

#2


4  

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.

它利用了当前轴的极限。因此,如果您稍后更改限制,可能会更容易删除它(删除(hr))并再次绘制。