看板 Fortran 關於我們 聯絡資訊
※ 引述《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
qlman: 哈…超強 06/02 06:16