Euler method: Difference between revisions
Content added Content deleted
(→{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details) |
(MInor fixes) |
||
Line 1,507: | Line 1,507: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{works with|Julia|0.6}} |
|||
<lang Julia>euler(f,T,t0,t1,h)=[(T,T+=h*f(T))[1] for t=t0:h:t1] |
|||
<lang julia>euler(f::Function, T::Number, t0::Int, t1::Int, h::Int) = collect(begin T += h * f(T); T end for t in t0:h:t1) |
|||
# Prints a series of arbitrary values in a tabular form, left aligned in cells with a given width |
# Prints a series of arbitrary values in a tabular form, left aligned in cells with a given width |
||
tabular(width,cells...)=println(join(map(s->rpad(s,width),cells) |
tabular(width, cells...) = println(join(map(s -> rpad(s, width), cells))) |
||
# prints the table according to the task description for h=5 and 10 sec |
# prints the table according to the task description for h=5 and 10 sec |
||
for h |
for h in (5, 10) |
||
print("Step $h:\n\n") |
print("Step $h:\n\n") |
||
tabular(15,"Time","Euler","Analytic") |
tabular(15, "Time", "Euler", "Analytic") |
||
t=0 |
t = 0 |
||
for T |
for T in euler(y -> -0.07 * (y - 20.0), 100.0, 0, 100, h) |
||
tabular(15,t,round(T,6),round(20.0+80.0*exp(-0. |
tabular(15, t, round(T,6), round(20.0 + 80.0 * exp(-0.07t), 6)) |
||
t+=h |
t += h |
||
end |
end |
||
println() |
println() |
||
end</lang> |
end</lang> |
||
{{out}} |
|||
<pre>Step 5: |
|||
Time Euler Analytic |
|||
0 72.0 100.0 |
|||
5 53.8 76.375047 |
|||
10 41.97 59.726824 |
|||
15 34.2805 47.99502 |
|||
20 29.282325 39.727757 |
|||
25 26.033511 33.901915 |
|||
30 23.921782 29.796514 |
|||
35 22.549159 26.903487 |
|||
40 21.656953 24.864805 |
|||
45 21.077019 23.42817 |
|||
50 20.700063 22.415791 |
|||
55 20.455041 21.702379 |
|||
60 20.295776 21.199646 |
|||
65 20.192255 20.845376 |
|||
70 20.124966 20.595727 |
|||
75 20.081228 20.419801 |
|||
80 20.052798 20.295829 |
|||
85 20.034319 20.208467 |
|||
90 20.022307 20.146904 |
|||
95 20.0145 20.103522 |
|||
100 20.009425 20.072951 |
|||
Step 10: |
|||
Time Euler Analytic |
|||
0 44.0 100.0 |
|||
10 27.2 59.726824 |
|||
20 22.16 39.727757 |
|||
30 20.648 29.796514 |
|||
40 20.1944 24.864805 |
|||
50 20.05832 22.415791 |
|||
60 20.017496 21.199646 |
|||
70 20.005249 20.595727 |
|||
80 20.001575 20.295829 |
|||
90 20.000472 20.146904 |
|||
100 20.000142 20.072951 </pre> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |