Pattern matching and floating point representation

I have a problem using numerical methods to solve a differential equation, and thought it might be a good opportunity to learn some basic Haskell. I have the following recurrence relation:

recursive attitude

and the initial condition u(x, 0) = x^2. I translated them into Haskell like this (adding the appropriate values ​​for a, b, c, h and k from a specific problem and noting that u_ij is defined as u(i*h, j*k)):

u :: (Floating a, Eq a) => a -> a -> a
u x 0 = x*x
u x t = a*k / b*h * (u (x-h) (t-k)) 
      + (1 - (3*k/2*h))*(u x (t-k)) 
      + k/b * cos x
    where 
        a = 3
        b = 2
        k = 0.1
        h = 0.2

main = putStrLn (show (u 1 0.5))

, . , , u x 0 . , , - , epsilon, , , . , , . , , ?

+4
1

, ; :

u :: (Floating a, Ord a) => a -> a -> a
u x t 
    | abs t <= 0.1 = x*x
    | otherwise =  a*k / b*h * (u (x-h) (t-k))
                   + (1 - (3*k/2*h))*(u x (t-k))
                   + k/b * cos x
      where
          a = 3
          b = 2
          k = 0.1
          h = 0.2

main = putStrLn (show (u 1 0.5))

.

+3

Source: https://habr.com/ru/post/1611152/


All Articles