How to stop ODE45 using event location
Show older comments
I'm trying to stop ODE45 solver once the solution intersects with the circle of radius 3. Below is the main code with the functions below that. I'm trying to stop the computation of the ODE45 with yV(:,3) = 3(i.e. when it reaches the circle)
import circle.*
import intersections.*
%%%%%%%%%%%%%%%%%%%%%Seting the globla variables%%%%%%%%%%%%%%%%%%%%%%
global X1 Y1 x q R0 G mu H0 cv initial_t initial_dtds initial_r initial_drds initial_theta initial_dthetads initial_phi initial_dphids
global initial_t_CS initial_dtds_CS initial_r_CS initial_drds_CS initial_phi_CS initial_dphids_CS initial_z_CS initial_dzds_CS
s = 0:1:10000; % time scale in FLRW
q = 0;
%%%%%%%%%%%%%%%%%%%%%Setting the constants %%%%%%%%%%%%%%%%%%%%%%%%%%%%
R0=1;
H0=71000;
cv=3*10^(8);
mu = 1*10^7;
G = 6.67*10^-11;
%mu = 1;
%G = 1;
%%%%%%%%%%%%%%%%initial conditions for the RK4 for FLRW%%%%%%%%%%%%%%%%
initial_t = 1;
initial_dtds = 1;
initial_r = 5;
initial_drds = -1;
initial_theta = pi;
initial_dthetads = .05;
initial_phi = 0;
initial_dphids = 0;
%draw 1st circle at (0,0) radius .5 and get X and Y data
H1=circle([0 0],3,30);
X1=get(H1,'XData');
Y1=get(H1,'YData');
%plotting the circle with dots
plot(X1,Y1, ':','linewidth',1)
hold on
xlim([-1 1]);
ylim([-1 1]);
%%%%%%%%%%%%%%%%the initial condition matrix for FLRW%%%%%%%%%%%%%%%%%%
v0 = [initial_t, initial_dtds, initial_r, initial_drds, ...
initial_theta, initial_dthetads, initial_phi, initial_dphids];
%%%%%%%%%%%%%%%%%%%%%Solving the rhsflrw equations%%%%%%%%%%%%%%%%%%%%%
%[s,v] = ode45( @(s,y) rhsflrw(s,y), s, v0);
options = odeset('Events',@events);
[sV, yV] = ode45(@rhsflrw, s, v0,options);
%[x,q]=intersections(X1,Y1,yV(:,3).*cos(yV(:,5)),(yV(:,3).*sin(yV(:,5))),0);
function dxds=rhsflrw(s,y)
global R0 H0 cv initial_t initial_dtds initial_r initial_drds initial_theta initial_dthetads initial_phi initial_dphids
dxds_1 = y(2);
dxds_2 = -((2/3)*R0^(2)*((3*H0/(2*cv))^(4/3))*y(1)^(1/3))*(y(4)^(2)+abs(y(3))^(2)*y(6)^(2)+abs(y(3))^(2)*(sin(y(5)))^(2)*y(8)^(2));
dxds_3 = y(4);
dxds_4 = -(4/(3*y(1)))*y(2)*y(4)+abs(y(3))*(y(6)^(2)+(sin(y(5)))^(2)*y(8)^(2));
dxds_5 = y(6);
dxds_6 = -(2/abs(y(3)))*y(4)*y(6)-(4/(3*y(1)))*y(2)*y(6)+((sin(2*y(5)))*(y(8)^(2))/2);
dxds_7 = y(8);
dxds_8 = -2*y(8)*((2/(3*y(1)))*y(2)+(cot(y(5)))*y(6)+(1/abs(y(3)))*y(4));
dxds=[dxds_1; dxds_2; dxds_3; dxds_4; dxds_5; dxds_6; dxds_7; dxds_8];
end
function [yV,isterminal,direction] = events(sV,yV);
global X1 Y1 x q R0 G mu H0 cv initial_t initial_dtds initial_r initial_drds initial_theta initial_dthetads initial_phi initial_dphids
yV(:3) = r; % Boundary is r = 3
isterminal = 1; % Stop the integration
direction = 0; % You're always approaching from outside
end
Accepted Answer
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!