Advertisement
parthosutradhor

Noisy Data

Jul 17th, 2019
1,312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. program noisy_data
  2.     implicit none
  3.     integer, parameter:: n=6
  4.     integer:: i, j, k
  5.     real:: x(n), yd(n), f, s, m, p, q, p1, q1
  6.  
  7.     open(11, file="in.txt")
  8.  
  9.     do i=1,n
  10.         read(11,*) x(i)
  11.     end do
  12.  
  13.     do i=1,n
  14.         read(11,*) yd(i)
  15.     end do
  16.  
  17.     close(11)
  18.  
  19.     p1=1.0
  20.     q1=-2.0
  21.     s=0.0
  22.     do i=1,n
  23.         s=s+(f(x(i),p1,q1)-yd(i))**2
  24.     end do
  25.     m=s
  26.  
  27.     do p=1.0,3.0,0.1
  28.         do q=-2.0,-1.0,0.1
  29.             s=0.0
  30.             do i=1,n
  31.                 s=s+(f(x(i),p,q)-yd(i))**2
  32.             end do
  33.             if(s<m) then
  34.                 m=s
  35.                 p1=p
  36.                 q1=q
  37.             end if
  38.         end do
  39.     end do
  40.  
  41.     open(12, file="out.txt")
  42.     write(12,13) m, p1, q1
  43.     13 format("minimum value is ",f7.3," at p = ",f6.3," and q = ",f6.3)
  44.     close(12)
  45.  
  46. end program noisy_data
  47.  
  48. function f(x, p, q) result(y)
  49.     real:: x, y, p, q
  50.     y=p+q*x
  51. end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement