Created
February 27, 2019 03:09
-
-
Save lunaluxie/b5fa3c96771d87b91031107c3fc43848 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% options | |
clear_frames_after = 5; | |
segment = "c"; % can be 'a', 'b', or 'c'. | |
%% diff eqs | |
%because r=1 | |
px = @(t,x) cos(t)-x; | |
py = @(t,y) sin(t)-y; | |
p = @(t,x,y) [px(t,x),py(t,y)]; | |
p_hat = @(t,x,y) 1./norm(p(t,x,y)) * p(t,x,y); | |
%% a | |
if segment=="a" | |
q = @(t) 0.3; | |
end | |
%% b | |
if segment=="b" | |
lambda = 30; %[s] | |
q = @(t) 0.50 * exp(-t./lambda); | |
end | |
%% a+b | |
%small dog | |
if segment=="a" || segment=="b" | |
v = @(t,x,y) q(t)*p_hat(t,x,y); | |
vpos = @(t,pos) v(t,pos(1),pos(2)); | |
dpdt = @(t,p) transpose(vpos(t,p)); | |
[ts,ps]=ode45(dpdt,[0,14 * pi],[0,-3]); | |
draw_global_frames(ts, ps, clear_frames_after) | |
end | |
%% c | |
if segment=="c" | |
q = @(t) 0.32; | |
v = @(t,x,y) q(t) ./ norm(p(t,x,y)) * p_hat(t,x,y); | |
%small dog | |
vpos = @(t,pos) v(t,pos(1),pos(2)); | |
dpdt = @(t,p) transpose(vpos(t,p)); | |
events = odeset('Events',@early_stopping); | |
[ts,ps]=ode45(dpdt,[0,14 * pi],[0,-3], events); | |
draw_global_frames(ts, ps, clear_frames_after) | |
end | |
%% drawables | |
function draw_global_frames(ts, ps, clear_after_frames) | |
xs = ps(:,1); | |
ys = ps(:,2); | |
for i=1:length(ts) | |
if clear_after_frames ~= -1 | |
if mod(i,clear_after_frames) == 0 | |
clf | |
end | |
end | |
hold on | |
xlim([-3,3]) | |
ylim([-3,3]) | |
t = ts(i); | |
% draw circle | |
draw_circle(0,0,1); | |
% draw small dog pos | |
draw_point(xs(i), ys(i)); | |
% draw large dog pos | |
draw_cpoint(1,t); | |
hold off | |
pause(0.05) | |
end | |
end | |
function [x,y] = radial_to_linear(r, angle) | |
x = r * cos(angle); | |
y = r * sin(angle); | |
end | |
function fig = draw_cpoint(r,angle) | |
[x,y] = radial_to_linear(r, angle); | |
fig = plot(x,y,'Marker','O',"MarkerFaceColor", "red"); | |
end | |
function fig = draw_point(x,y) | |
fig = plot(x,y,'Marker','O',"MarkerFaceColor", "blue"); | |
end | |
function fig = draw_circle(x,y,r) | |
th = 0:pi/50:2*pi; | |
xunit = r * cos(th) + x; | |
yunit = r * sin(th) + y; | |
fig = plot(xunit, yunit); | |
end | |
%% helper funcs | |
function [value, stop, d] = early_stopping(t,p) | |
% position of the big dog | |
% this exploits r=1. | |
P = [cos(t); sin(t)]; | |
eps = 0.1; | |
value = norm(P-p)-eps; | |
stop = 1; | |
d = 0; | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment