### Translated by

이 페이지의 내용은 영어에서 자동 번역되었습니다. 자동 번역 기능을 해제하려면 "<a class="turn_off_mt" href="#">여기</a>"를 클릭하십시오.

## Getting the correct y-intercept and gradient for a trend line using 'fit'

### P_L (view profile)

on 5 Apr 2019
Latest activity Edited by dpb

on 6 Apr 2019

### dpb (view profile)

Hi there,
I have this code and I would like to get the correct y-intercept (and gradient) for my slope. You can see from the image generated that this function gives me the wrong intercept value. I have tried switching lat and long and I still cannot get the correct value. Does anyone know how I can amend this so it works?
I would also like to measure the fir of my data- Please can someone help me with that?
Many thanks,
lat = [30.4056270000000;30.3991500000000;30.4000740000000;30.4046990000000;30.4102510000000;30.4093280000000;30.3889660000000;30.3982300000000;30.4074760000000;30.4195050000000;30.4139500000000;30.4120980000000;30.4083980000000;30.3936010000000;30.3926810000000];
lon = [40.7602680000000;40.7609660000000;40.7616710000000;40.7623780000000;40.7623830000000;40.7602710000000;40.7644750000000;40.7574480000000;40.7616770000000;40.7609820000000;40.7637930000000;40.7651980000000;40.7637880000000;40.7595540000000;40.7560360000000];
f=fit(lat, lon,'poly1')
plot(f,lat,lon,'o')
xlabel longitude
ylabel latitude
The output of the function is:
f =
Linear model Poly1:
f(x) = p1*x + p2
Coefficients (with 95% confidence bounds):
p1 = 0.1116 (-0.04946, 0.2726)
p2 = 37.37 (32.47, 42.27)

on 5 Apr 2019
Edited by dpb

### dpb (view profile)

on 6 Apr 2019

Looks quite right to me...the intercept is where the fitted line will cross (intercept) the y-axis at x=0...that point is way and gone to the end of the earth (so to speak) to the left of your axis and with a negative slope of 0.11/degree latitude,
0.11*30 --> -3.3 and 40.76 - 3.3 --> 37.46
which is really close to what the interecept says it is exactly
As for the other question, read
doc fit
and look at the second optional output, gof, the goodness of fit structure.