Matlab Help:Stop ode45 with OutpuFcn

  • MATLAB
  • Thread starter stevenumber5
  • Start date
  • Tags
    Matlab Ode45
In summary, the conversation is about solving a first-order ODEs system and wanting to stop the solution when a certain variable becomes negative. The speaker is using ode45 as a solver and has an output function that is not working properly. They are advised to use an if statement to check the variable and return the appropriate status value.
  • #1
stevenumber5
1
0
Hi everybody,
I'm trying to solve this first-order ODEs system:and I'd like to stop the solution when y, i.e u(3), becomes negative:

Matlab:
%system
function rhs=particle(t,u)
global k
rhs=[u(2);
    -k*u(1)*sqrt(u(2)*u(2)+u(4)*u(4));
    u(4);
    -9.81-k*abs(u(4))*sqrt(u(2)*u(2)+u(4)*u(4))];

where u(1)=x,u(2)=dx/dt,u(3)=y,u(4)=dy/dt
I use ode45 as solver, and in order to stop it the option OutputFcn.
It stops after few values, when y is already positive.
Here is the script

Matlab:
%ode script

global k

t0=0;tend=10;

k=0.065;

a=20*pi/180;

u0=[0 20*cos(a) 1.5 20sin(a)];

options = odeset('OutputFcn',@output);

[time,result]=ode45('particle',[t0,tend],u0,options);
%output function

function status = output(t,y,status)

[val,pos]=min(y)

status=(val>=0)

if status==1

tend=t(pos);

end
Thank you all
Best Regards
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
It looks like your output function is not properly set up to return an appropriate status value. The output function should return either 1 or 0 depending on whether the integration should continue or not. You can use an if statement to check if y is negative and then return 0 if it is, otherwise return 1.
 
  • #3
,

I would like to suggest a few things to improve the ode45 solver and output function in this case. Firstly, it is important to properly define the variables and parameters used in the system and script. This will make it easier for others to understand and reproduce the results. Additionally, it would be helpful to provide some context or background information on the system being modeled and the significance of stopping the solution at a negative y value.

In terms of the ode45 solver, it may be beneficial to try different integration methods or adjust the tolerances to see if that changes the behavior of the solution. It is also worth considering if the system itself needs to be modified in order to accurately model the desired behavior.

As for the output function, it may be helpful to include more information in the status output, such as the time and corresponding values of the variables at which the solution was stopped. This will provide more insight into the behavior of the system and can aid in further analysis.

Overall, it is important to carefully consider the system and solver being used, and to continuously evaluate and adjust the approach in order to accurately model and understand the behavior of the system.
 

1. How do I use the OutputFcn in Matlab's ode45 function?

To use the OutputFcn in ode45, you need to specify it as an additional input argument when calling the function. The syntax is as follows: [t,y] = ode45(@odefun,tspan,y0,options), where @odefun is the name of your ODE function and options is a structure that contains the OutputFcn.

2. What is the purpose of the OutputFcn in ode45?

The OutputFcn allows you to specify a function that will be called after each time step in the ode45 solver. This function can be used to monitor the solution, perform calculations, or even stop the solver when certain conditions are met.

3. How can I stop the ode45 solver using the OutputFcn?

To stop the ode45 solver using the OutputFcn, you need to define a stopping condition inside the function. This can be done by using the built-in function, ode45set, to set the OutputFcn property of the options structure. Inside the OutputFcn, you can then check for your desired stopping condition and use the ode45set function again to set the Refine property to a negative value, which will stop the solver.

4. Can I access the current solution in the OutputFcn?

Yes, you can access the current solution inside the OutputFcn by using the t and y outputs of the ode45 function. These represent the current time and solution vectors, respectively, and can be used to monitor or manipulate the solution as desired.

5. Are there any other options I can specify in the options structure when using the OutputFcn?

Yes, there are several other options that can be specified in the options structure when using the OutputFcn. These include OutputSel to select specific components of the solution to be passed to the OutputFcn, OutputEvents to define events that will trigger the OutputFcn, and OutputFcnArgs to pass additional arguments to the OutputFcn. For more information, you can refer to the ode45 documentation in Matlab.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
811
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
1
Views
983
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
0
Views
1K
  • Advanced Physics Homework Help
Replies
6
Views
2K
Back
Top