我有一组数据需要为(一阶polyfit
)生成两条线性最佳拟合线,但我不知道如何指定每条线应该适合数据的区域。我需要在最小x值和0之间的区域中有一条线,而另一条线在0.25 <x区域内。
此外,在第二个区域,有两个明确的数据区域,一个在另一个之上,我需要最合适的线路才能安装到较低的区域。
我在Matlab是一个完整的新手所以任何帮助将不胜感激
%load data, force and velocity
load ('exp_6_Force');
load ('exp_6_Velocity');
% Give a name to the title bar.
set(gcf,'name','Experiment 6 velocity','numbertitle','off')
%set variables to x and y
x = Force;
y = Velocity;
%plot the graph
plot(x,y);
%add grid and legend
grid on;
legend ('Velocity');
%add labes and title
xlabel ('Force');
ylabel ('Velcoity');
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
%find coordinates of y min point
[value,index1] = min(y);
yminxcoor = x(index1);
yminycoor = y(index1);
使用logical index获取两个地区的x
和y
数据:
对于区域1:
x_region1 = (x<0).*x
y_region1 = (x<0).*y
对于区域2:
x_region2 = (x>0.25).*x
y_region2 = (x>0.25).*y
那么你可以在这些地区polyfit
对于区域1:
p_region1 = polyfit(x_region1, y_region1, 1)
对于区域2:
p_region2 = polyfit(x_region2, y_region2, 1)