Passing computations from ODE45

  • Thread starter adschutte
  • Start date
  • Tags
    Ode45
In summary, ODE45 is a solver that uses a combination of Runge-Kutta 4th and 5th order methods to solve differential equations. It allows for the passing of additional parameters through a function handle and is known for its versatility, robustness, and efficiency. However, it may not be suitable for stiff systems of differential equations. The results of ODE45 can be interpreted by analyzing the solution vector and accuracy/efficiency information.
  • #1
adschutte
1
0
Does anyone know a trick to pass variables computed in the user defined function called by ODE45 in MATLAB?

For example, imagine you have the following code:

%=======================================
[t,y] = ode45('DiffEQ',dt,Initial,options);

function dy = DiffEQ(t,y)
Fc = y(1)*y(2);
dy(1) = y(2);
dy(2) = -k*y(1)-c*y(2)+Fc;
%=======================================

How can we pass Fc as an output? I don't want to re-compute Fc with the solutions. Thanks for any tips.
 
Computer science news on Phys.org
  • #2
Save it in a file or embed the code in a bigger one. Is not very consuming to do an ode45.
 
  • #3


One possible solution to pass variables computed in the user-defined function called by ODE45 is to use the global keyword. This allows you to declare a variable as global within the function and then assign its value outside of the function. For example, in your code, you could declare Fc as global within the DiffEQ function and then assign its value outside of the function after the ode45 call. This way, the value of Fc will be retained and accessible after the function has finished computing. However, it is important to note that using global variables can lead to potential issues with code readability and debugging, so it should be used with caution. Another option would be to use a nested function within the DiffEQ function to compute and store the value of Fc, which can then be accessed outside of the function. This approach may be more organized and easier to debug compared to using global variables.
 

FAQ: Passing computations from ODE45

How does ODE45 handle passing computations?

ODE45 uses a combination of Runge-Kutta 4th and 5th order methods to solve the differential equations and passes the computations between each step to ensure accuracy and efficiency.

Can I pass additional parameters to ODE45?

Yes, ODE45 allows for the passing of additional parameters to the differential equations through the use of a function handle. These parameters can be constants, variables, or arrays.

What are the advantages of using ODE45 for passing computations?

ODE45 is a versatile and robust solver that can handle a wide range of differential equations. It is also efficient and provides accurate solutions, making it a popular choice for many scientists and engineers.

Are there any limitations to passing computations with ODE45?

One limitation of ODE45 is that it cannot handle stiff systems of differential equations. In these cases, a different solver, such as ODE15s, may be more suitable.

How do I interpret the results of passing computations with ODE45?

ODE45 returns a solution vector which contains the values of the variables at each time step. This vector can be plotted or further analyzed to understand the behavior of the system. Additionally, ODE45 also provides information on the accuracy and efficiency of the solution.

Back
Top