How can I force a gradient for a linaar fit

1 次查看(过去 30 天)
Hi, Im am using the following code to perform a fit on my data x,y1.
P = polyfit(x,y1,1)
m=P(1);
c=P(2)
yfit = m*x+c;
hold on;
plot(x,yfit,'b-.');
However, I want to be able to force the gradient to be 0.5.
How can I do this?

采纳的回答

John D'Errico
John D'Errico 2016-6-2
编辑:John D'Errico 2016-6-2
You want to force the slope to be 0.5? Then the least squares estimate for the constant term will come most simply from mean.
m = 0.5;
c = mean(y - m*x);
Ok, if you really want to use polyfit, then
c = polyfit(x,y-m*x,0);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Curve Fitting Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by