Re: [問題] 有沒有二維數據的迴歸直線的斜率的函數

看板Fortran作者 (老怪物)時間1年前 (2022/05/20 09:37), 編輯推噓1(100)
留言1則, 1人參與, 1年前最新討論串2/2 (看更多)
※ 引述《BanPeeBan (踢屁屁)》之銘言: : 手上有二維(x,y)的數據 : 想求回歸方程式(主要是斜率) : 請問有沒有函數可以直接使用呢? : 估狗都只找到高中數學的推導理論QQ : 謝謝大家 subroutine linereg(n,x,y,a,b) implicit none integer (kind=4), intent(in) :: n ! 4 bytes integer real (kind=8), intent(in) :: x(n), y(n) ! 8 bytes real real (kind=8), intent(out) :: a, b real (kind=8) :: fn, xbar, ybar, sxx, sxy fn = n xbar = sum(x)/fn ybar = sum(y)/fn sxx = sum(x*x)/fn - xbar**2 sxy = sum(x*y)/fn - xbar*ybar b = sxy/sxx a = ybar-b*xbar return end subroutine linereg program main implicit none integer (kind=4), parameter :: n=10 real (kind=8) :: x(n), y(n), a, b ! 主程式 dimension 宣告必須是常數, 或者用 allocatable. integer :: i do i=1,n print*, '請輸入第 ',i,' 對資料:' read(*,*) x(i),y(i) end do call linereg(n,x,y,a,b) write(*,*) '迴歸直線函數:', a,' +',b,'x' stop end -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.224.173.34 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Fortran/M.1653010667.A.222.html

06/02 06:16, 1年前 , 1F
哈…超強
06/02 06:16, 1F
文章代碼(AID): #1YXl3h8Y (Fortran)
文章代碼(AID): #1YXl3h8Y (Fortran)