On the lines where you initialize k and l to [0 0 0 0 0 0], that is an 
array of integers by default. Then in the for loops where you try to assign 
a floating-point result into that array, you get an InexactError because a 
general floating-point number can't be exactly represented as an integer. 
If you change those to float64([0 0 0 0 0 0]) or Float64[0 0 0 0 0 0] or 
[0.0 0.0 0.0 0.0 0.0 0.0], and likewise with t0, v0, and w0, it should work.


On Sunday, May 4, 2014 2:40:17 PM UTC-7, Paulo Castro wrote:
>
> I was trying to port a function from a previous Python code of mine, and 
> ended with this:
>
> # Rubge-Kutta-Fehlberg
> function RKF45(f, g, h, t0, tf, v0, w0)
>     # Initial values
>     t = t0
>     v5 = v4 = v = v0
>     w = w0
>
>     tolerance = 10^(-5.0)
>
>     # Create list for future plots
>     t_list = [t]
>     v_list = [v]
>     w_list = [w]
>
>     # Values for a, b and c (as on Butcher's tableau)
>     a = [    0          0           0          0        0    0;
>             1/4         0           0          0        0    0;
>             3/32       9/32         0          0        0    0;
>          1932/2197 -7200/2197   7296/2197      0        0    0;
>           439/216      -8       3680/513   -845/4104    0    0;
>            -8/27        2      -3544/2565  1859/4104 -11/40  0]
>
>     b = [16/135  0   6656/12825  28561/56430  -9/50  2/55;
>          25/216  0   1408/2565   2197/4104     -1/5   0]
>
>     c = [0 1/4 3/8 12/13 1 1/2]
>
>     # Relative to y
>     k = [0 0 0 0 0 0]
>
>     # Relative to z
>     l = [0 0 0 0 0 0]
>
>     # Compute the next terms
>     for i in t0:h:tf
>         # Compute the next values of K and L
>         for j in 1:6
>             k[j] = f(t + c[j] * h, v + (a[j,:] * k')[1], w + (a[j,:] * l'
> )[1])*h
>             l[j] = g(t + c[j] * h, v + (a[j,:] * k')[1], w + (a[j,:] * l'
> )[1])*h
>         end
>
>
>         
> ############################################################################
>         # Compute the next value of V                                     
>          #
>         # Here we implemented a tolerance test                           
>           #
>         
> ############################################################################
>         v4 = v + (b[2] * k')[1]                                           
>             #
>         v5 = v + (b[1] * k')[1]                                           
>             #
>                                                                           
>          #
>         error = abs(v5 - v4)                                             
>           #
>         if error > tolerance                                             
>           #
>                                                                           
>          #
>             h = 0.9 * h * ((tolerance/error) ^ (0.25))                   
>           #
>                                                                           
>          #
>             for j in 1:6                                                 
>           #
>                 k[j] = f(t + c[j] * h, v + (a[j] * k')[1], w + (a[j] * l'
> )[1])*h   #
>                 l[j] = g(t + c[j] * h, v + (a[j] * k')[1], w + (a[j] * l'
> )[1])*h   #
>             end                                                           
>          #
>                                                                           
>          #
>             v5 = v + (b[1,:] * k')[1]                                     
>          #
>         end                                                               
>          #
>                                                                           
>          #
>         v = v5                                                             
>         #
>         
> ############################################################################
>        
>         # Compute T and W with the right values of H and L, obtained after 
> the tolerance test 
>         t += h
>         w += (b[1,:] * l')[1]
>
>         # Append new values to the lists
>         push!(t_list,t)
>         push!(v_list,v)
>         push!(w_list,w)
>     end
>
>     return t_list, v_list, w_list
> end
>
> After running this code, I got a mysterious error message:
>
>
>
> *ERROR: InexactError() in setindex! at array.jl:346while loading 
> /home/paulo/Documents/Working/ex1.jl, in expression starting on line 28*
>
> ex1.jl is the file I used to test my function:
>
> include("rungekutta.jl")
>
> function f(t, v, w)
>     return (v*(v-a)*(1-v) - w + I)/ε
> end
>
> function g(t, v, w)
>     return (v - p*w - b)
> end
>
> ε = 0.005
> a = 0.5
> b = 0.15
> p = 1.0
> I = 0.0
> h = 0.005
>
> # Initial and final values of time
> t0 =  0
> tf =  8
>
> t, v, w = RKF45(f, g, h, t0, tf, 0, 0) # v0 = w0 = 0
>
> Can someone help me with finding what's the problem? I tried a lot of 
> things, but always end with this error.
>
> Thanks!
>
>

Reply via email to