forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch2ex2.m
35 lines (34 loc) · 1.04 KB
/
ch2ex2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function ch2ex2
opts = odeset('Events',@events,'RelTol',1e-6,'AbsTol',1e-10);
[t,x,te,xe,ie] = ode45(@odes,[0 65],[5; 5; 5; 5],opts);
plot3(x(:,1),x(:,2),x(:,3));
xlabel('x_1(t)'), ylabel('x_2(t)'), zlabel('x_3(t)')
if isempty(ie)
fprintf('There were no events.\n');
else
event1 = find(ie == 1);
if isempty(event1)
fprintf('Event 1 did not occur.\n');
else
fprintf('Event 1 occurred %i times.\n',length(event1));
figure
plot(xe(event1,1),xe(event1,3),'*');
end
event2 = find(ie == 2);
if isempty(event2)
fprintf('Event 2 did not occur.\n');
else
fprintf('Event 2 occurred %i times.\n',length(event2));
figure
plot(xe(event2,1),xe(event2,2),'*');
end
end
%==============================================================
function dxdt = odes(t,x)
a = 3.12121212;
b = 2.11111111;
dxdt = [a*x(3); b*x(4); -a*x(1); -b*x(2)];
function [value,isterminal,direction] = events(t,x)
value = [x(2); x(3)];
isterminal = [0; 0];
direction = [0; 0];