RosettaCodeData/Task/Euler-method/DuckDB/euler-method.duckdb

27 lines
749 B
Plaintext

# Declaration for the sake of euler_table()
create or replace function cooling(x,y) as NULL;
# Euler method assuming cooling/2 is available
create or replace function euler_table(x_start, y_start, x2, h) as table (
with recursive cte as (
select x_start::DOUBLE as x, y_start::DOUBLE as y
union all
select x + h as x,
y + h*cooling(x, y)
from cte
where (x < x2 and x_start < x2) or
(x > x2 and x_start > x2)
)
from cte
);
create or replace function euler(x_start, y_start, x2, h) as (
select last(y order by x) from euler_method(x_start, y_start, x2, h)
);
## Example:
create or replace function cooling(x,y) as
-0.07 * (y - 20);
select h, euler(0,100, 100, h) from unnest([1,2,5,10,20]) _(h);