MATLAB: Making a Best Fit line with 0,0

interceptplotslope

So, I am in Engineering Physics 2. My lab partners and I have complied some data. Data is as follows
Capacitance = 1.0e-07 * [
0.1077
0.0669
0.0485
0.0379
0.0309
0.0272
0.0225
0.0194
0.0185
0.0164
0.0110
0.0081
0.0069]
and
InvDistance = [
20000
10000
6666.666667
5000
4000
3333.333333
2857.142857
2500
2222.222222
2000
1333.333333
1000
400]
So we plot it and then we tried doing a best fit like but our professor said the slope is incorrect and it needs have the intercept at (0,0). How do I make this best fit line change it's intercept to (0,0) so we can have a slope the professor agrees with?

Best Answer

  • If you want it to go through the origin, do not use an intercept term in your model. In this instance, Capacitance = InvDistance * B, so solve for parameter vector (here a scalar with only the slope) ‘B’.
    See if this does what you want:
    B = InvDistance\Capacitance;
    Fit = InvDistance * B;
    figure(1)
    plot(InvDistance, Capacitance, 'pb')
    hold on
    plot(InvDistance, Fit, '-r')
    hold off
    grid
    xlabel('InvDistance')
    ylabel('Capacitance')
    Your data have to be column vectors for this to work, but since they already are, you don’t need to do anything with them.