MATLAB Answers

Display output of ODE45 after error

1 view (last 30 days)
Telema Harry
Telema Harry on 1 Jul 2021
Commented: Telema Harry on 2 Jul 2021
Hello Programmers,
I have a model that predicts the altitude (Z) of a UAV.
The model is a sets of ODE and I solved it using ODE45.
The issue is that the UAV altitude (Z) decreases to a negative value and the program terminate as Z cannot be negative.
If Z = 0, it means that the UAV is on the surface of the earth.
Instead of giving error when Z is negative. I will like to display a message that "The object has successfully landed at location, Longitude = .. and Latitude = " and return the output of ODE45. So, I can plot all the output variables.
Please I will appreciate any help.
Thank you.

Answers (2)

Amit Bhowmick
Amit Bhowmick on 1 Jul 2021
I dont know your equation but you can use similar trick
tspan = [0 5];
y0 = 20;
[t,y] = ode45(@fun, tspan, y0);
plot(t,y,'-o')
function dydt=fun(t,y)
if(y<=0)
dydt=0;
else
dydt=-2*t;
end
end
  8 Comments
Telema Harry
Telema Harry on 2 Jul 2021
Thank you for the contribution. But my model is slightly different

Sign in to comment.


Walter Roberson
Walter Roberson on 1 Jul 2021
Edited: Walter Roberson on 1 Jul 2021
You should use an event function to terminate integration. See the ballode example.
Note that in order to be able to detect height being a particular value, you would probably need to have height be one of the boundary conditions. You might not need its integral on output, but you need it for the steering calculations. For example, the UAV is going to make different decisions about how much lift it needs to generate if the UAV is 50m off the ground than if the UAV is 50cm off the ground.
  8 Comments
Telema Harry
Telema Harry on 2 Jul 2021
woooow. thank you so much @JanJan. The code now runs without the error.
However, I have not been able to get my desired result. The Z is still decreasing below 0 and P_air can only be calculated when Z > 0. So the program terminates.
Thank you for the help.
options = odeset('Events',@stopevents);
In0 = [lat_0; lon_0; T_gas_0; T_film_0; M_gas_0; Uz_0; Z_0]';
[t,output] = ode45(@BalloonODE,tspan,In0,options);
function [value,isterminal,direction] = stopevents(t,y)
% Locate the time when height passes through zero in a decreasing direction
% and stop integration.
value = y(7); % detect height = 0
isterminal = 1; % stop the integration
direction = -1; % negative direction
end

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!