Oliver Range
Oliver Range

Reputation: 365

Discretizing a differential equation and find the value of a parameter in each time step

I want to find the value of f(t) by using for loop on t. The equation governing f is:

df/dt = cos(f) g(x,y,t)

x and y are coordinate in 2D. I wrote the below code:

dt=1; a=2;
f(1,1)= a*rand(1,1);
for t = 2:100
    f(1,t)= cos(f(1,t-1)) g(x,y,t) dt;
end

but I'm not sure if this method is correct or not, or if one could write it better. Could anyone help me to know if it is correct or not?

Upvotes: 0

Views: 65

Answers (1)

jman
jman

Reputation: 703

You are essentially trying to implement a Euler method with a unit step size ($h = 1$). The formulas to advance $f$ are $f_{n+1} = f_{n} + h cos(f_{n}) g(x,y,t_{n})$. Here $t_n = t_0 + n h$. So your code in the loop needs to change to:

 f(1,t) = f(1,t-1) + cos(f(1,t-1)) g(x,y,t-1) dt

Does this method work for this system? Frankly, it's not likely. The euler method tends to work best with a smooth function that does not vary much relative to the step size. Given the rapid variation of the $cos$ function the euler method will most likely fail for large step sizes.

How can the system be solved? There are a few things you could do.

  1. use a smaller step size (it would have to be quite small).
  2. use another solution method such as Runge-Kutta.
  3. solve the analytical solution.

If I were you I would do the analytical route. Good luck

Upvotes: 1

Related Questions