How do i pass atrendline through zero?

4 次查看(过去 30 天)
I have a data when fit gives rise to an equation y=1.09x+0.23. However i want to pass the trend line through zero and get an equation y=1.234x. Can anyone please help how to do that?
  1 个评论
KSSV
KSSV 2016-3-15
Your question sounds funny. The first equation is a straight line with slope 1.09 and intersecting y-axis at 0.23. Second equation is a straight line with slope 1.234 and passing through origin. What exactly are you trying?

请先登录,再进行评论。

回答(1 个)

Ced
Ced 2016-3-15
编辑:Ced 2016-3-15
You need to set up your regression accordingly. If you don't want a y offset, you could just normalize your data, i.e. remove the mean on both sides, and then not add it back during the prediction. Be aware that you are introducing an error though!
Alternatively, you can just remove the bias from the optimization.
As Dr. Siva Srinivas Kolukula correctly pointed out though, I don't know why you would want to do this though.
Here is a small example:
x = linspace(-4,4,20)';
N = length(x);
y = 1.09*x+2 + 1*randn(N,1);
% estimate with offset: y = a1*x + b1 = x*beta1
Phi1 = [ x ones(N,1) ];
beta1 = (Phi1'*Phi1)\Phi1'*y;
% estimate without offset: y = a2*x = x*beta2
Phi2 = x;
beta2 = (Phi2'*Phi2)\Phi2'*y;
% estimate without offset: y = a2*x = x*beta2
Phi2 = x;
beta2 = (Phi2'*Phi2)\Phi2'*y;
% estimate with normalized data
% y_norm = a2*x_norm = x_norm*beta2
xmean = mean(x);
ymean = mean(y);
Phi3 = x-xmean;
beta3 = (Phi2'*Phi2)\Phi2'*(y-ymean);
figure()
plot(x,y,'x');
hold on
plot(x,Phi1*beta1)
plot(x,Phi2*beta2);
% NOTE: not adding offset!!
plot(x-xmean,Phi3*beta3,'--');
grid on
xlabel('x')
ylabel('y')
legend('samples','affine fit','linear fit','fit without offset')

类别

Help CenterFile Exchange 中查找有关 Fit Postprocessing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by