MATLAB: How is the right method to declare a differential equation

differential equationsodeode45

Hi!
I want to know if is there difference between this two examples in declaring the equation:
Example 1:
dx(1)=x(2);
dx(2)=k*x(1)+c*x(2)+dx(4);
dx(3)=x(4);
dx(4)=x(1)+2*x(2);
Example 2:
dx(1)=x(2);
dx(2)=k*x(1)+c*x(2)+x(1)+2*x(2);
dx(3)=x(4);
dx(4)=x(1)+2*x(2);
I want to know if calling a Dx function before she is expressed in the group of equations will change the results?
What is the correct method?

Best Answer

The correct method for dx(2) is method 2. You need to have values defined before they are used. Method 1 will give the wrong result (using a past value that happens to be defined) or generate an error (if the value isn't yet defined). There is no such thing as "forward looking for values" in MATLAB.
Maybe reorder your equations like this if you want to make your equations look more like what you are using:
dx(4)=x(1)+2*x(2);
dx(1)=x(2);
dx(2)=k*x(1)+c*x(2)+dx(4);
dx(3)=x(4);