Solving Time Dependent ODEs in MATLAB: Troubleshooting and Parameter Adjustment

In summary, the best approach for solving time dependent ODEs in MATLAB is to use the built-in ODE solvers such as ode45 or ode15s. If an error occurs while trying to solve the ODE, it could be due to inappropriate initial conditions or the stiffness of the ODE system. To troubleshoot, one can plot the results, use debugging tools, or adjust solver options and parameters. Common parameters that can be adjusted to improve the solution include solver tolerances, initial and maximum step sizes, and initial conditions. MATLAB can also handle time dependent ODEs with discontinuities by using the odeevents function to specify their location and type.
  • #1
physchemmath
1
0
Have been struggling with errors galore on this one. I am not too conversant with ODEs in MATLAB. The problem is as follows. I have to solve these two ODEs for A1 and A2.

A1dot = k2*A2 - k1*A1 - k3*A1/(k4 + A1) + R
A2dot = k1*A1 - k2*A2

Here R is a function of time defined as follows:

R = 5, when 7<t<7.2
R=0 otherwise

R is periodic and this pattern repeats. Like R takes the value 5 when 7<t<7.2, when 14<t<14.2, 21<t<21.2 and so on, and is zero everywhere else.

My code for obtaining the vector of differentials is as follows. It takes in t, A, T and R. The T is a time vector for the R parameter. I use the interp1 command as outlines in a sample MATLAB time dependent ODE code.

function dA = trial(t,A,T,R)
Rin = interp1(T,I,t);

V1 = 40; V2 = 100; k1 = 0.2; k2 = 0.2;k3 = 2; k4 = 0.003;

dA(1) = k2*A(2) - k1*A(1) - (k3*A(1))/(k4 + A(1)) + Rin;
dA(2) = k1*A(1) - k2*A(2);
dA = dA(:);

end

I then call this at the command line. I define R suitably using a for loop (using rem and other stuff) and define T to be the same as t0:1:tf as in the ODE tspan.

Then I solve for A.

[t,A]=ode15s(@trial, [to,tf],Anot,[],T,R);

I seem to be getting strange plots. I am concerned if the above method as such works, or the problem is with my parameter values (of which am not too sure as yet). So if the above code and the way of doing it is right, I can go about changing parameters and seeing.

Any help is highly appreciated.

Thanks..
 
Physics news on Phys.org
  • #2

Thank you for sharing your code and the problem you are facing. Solving ODEs in MATLAB can be tricky, especially when there are time-dependent variables involved. From your code, it seems like you have a good understanding of how to set up the ODEs and use the interp1 function to define the time-dependent variable R. However, there are a few things that I noticed that may be causing the strange plots you are getting.

Firstly, in your code, you are using the ode15s solver, which is designed for stiff ODEs. Stiff ODEs are those that have very different time scales for different variables. In your case, the variables A1 and A2 have different rates of change, so using ode15s may not be the best choice. I would recommend trying the ode45 solver, which is better suited for non-stiff ODEs.

Secondly, in your code, you are defining R as a function of time, but in the ODEs, you are using the variable Rin, which is a function of the time vector T. This may be causing some confusion and may be why you are getting strange plots. I would suggest using the same variable for R in your ODEs, and also make sure that it is a function of time and not the T vector.

Lastly, it is always a good idea to check your parameter values and make sure they are reasonable for the system you are modeling. It is possible that some of the strange plots you are getting may be due to unrealistic parameter values.

I hope this helps you in solving your problem. If you continue to face issues, I would suggest seeking help from a colleague or a MATLAB expert who can help you debug your code and find the source of the issue. Good luck!
 

What is the best approach for solving time dependent ODEs in MATLAB?

The best approach for solving time dependent ODEs in MATLAB is to use the built-in ODE solvers such as ode45 or ode15s. These solvers are specifically designed to handle time dependent ODEs and provide accurate solutions.

Why am I getting an error when trying to solve my time dependent ODE in MATLAB?

There could be several reasons for this error. One common reason is that the initial conditions or parameters used in the ODE solver are not appropriate. You may need to adjust these values or check for any typos in your code. Another reason could be that the ODE system is stiff and requires a different solver such as ode15s instead of ode45.

How can I troubleshoot my time dependent ODE in MATLAB?

To troubleshoot your time dependent ODE in MATLAB, you can try plotting the results to visually inspect the behavior of the solution. You can also use the debugging tools in MATLAB to step through your code and identify any potential errors. Additionally, you can try adjusting the solver options or parameters to see if it improves the solution.

What are some common parameters that can be adjusted to improve the solution of a time dependent ODE in MATLAB?

Some common parameters that can be adjusted to improve the solution of a time dependent ODE in MATLAB include the solver tolerances, initial step size, and maximum step size. You can also try using a different solver or adjusting the initial conditions and parameters of your ODE system.

Can I use MATLAB to solve time dependent ODEs with discontinuities?

Yes, MATLAB can handle time dependent ODEs with discontinuities. You can use the odeevents function to specify the location and type of discontinuities in your ODE system. This will allow the solver to accurately handle these discontinuities and provide a smooth solution.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Replies
5
Views
354
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Back
Top