I was testing a silly idea on how to solve a difeq.
#f(x+Δx) ≈ a + bΔx + cΔx^2 + dΔx^3
#f'(x+Δx) ≈ b + 2cΔx + 3dΔx^2
#a = f(x)
#b = f'(x)
# cΔx^2 + dΔx^3 = f(x+Δx) - a - bΔx
# 2cΔx + 3dΔx^2 = f'(x+Δx) - b
# (c,d) = [Δx^2 Δx^3; 2Δx 3Δx^2]\ [f(x+Δx) - a - bx, f'(x+Δx) - b]
function solve(f,initial_value = 0, start=0,finish = 10,step_size = 0.001, initial_step_size = 0.000001)
A = initial_value
B = A
for i in start:initial_step_size:start+step_size
if (i == start+step_size) break end
B += f(i,B)*initial_step_size
end
#println(B)
for i in start:step_size:finish
if (i== finish) break end
a = A
b = f(i,A)
Δx = step_size
println([B - a - b*Δx, f(i+step_size,B) - b])
(c,d) = [Δx Δx^2; 2Δx 3Δx^2]\ [(B - a)/Δx - b, f(i+step_size,B) - b] #Change the equation a bit to get the matrices to be of the same scale.
println(c," ",d)
(A,B) = (B,a+2b*Δx+4c*Δx^2+8d*Δx^3)
println(A," ",B," ", exp(i+step_size))
end
return B
end
println(solve((t,y)->y,1,0,0.002))
Now, it obviously doesn’t work. Can anybody explain why it doesn’t?
What’s the differential equation you’re trying to solve? I don’t see an equation involving an unknown function of a variable and its derivatives of various orders with respect to that variable. For example f(x+Δx) ≈ a + bΔx + cΔx^2 + dΔx^3 isn’t one.
It appears to be an equation involving a differential if Δx is intended as an infinitesimal change in x, but it doesn’t make sense when there’s no derivatives relating differentials to each other or integrals of an expression with one. Differentials can’t be treated as independent terms rigorously, for example it wouldn’t make sense to differentiate f with respect to a differential Δx like this
#f(x+Δx) ≈ a + bΔx + cΔx^2 + dΔx^3
#f'(x+Δx) ≈ b + 2cΔx + 3dΔx^2
Solving a differential equation is supposed to find the unknown function of a variable, how are you approximating it if it’s not known? It sounds vaguely like a truncated Taylor series, but f(x+Δx) ≈ a + bΔx + cΔx^2 + dΔx^3 isn’t consistent with that, even putting aside the invalid use of differentials.
Can you delineate your knowns and unknowns (functions, equations, constants, etc) at the beginning of the problem? At this point it’s not even clear if you should be working with a differential equation.