Skip to content

Instantly share code, notes, and snippets.

@efruchter
Last active October 8, 2017 21:11
Show Gist options
  • Save efruchter/7bd55825dc01da6d85ecb6bf8199ce67 to your computer and use it in GitHub Desktop.
Save efruchter/7bd55825dc01da6d85ecb6bf8199ce67 to your computer and use it in GitHub Desktop.
Complex numerical solution for a simple geometry problem
pkg load linear-algebra
# Initial Conditions
pc = [2,3]; #target pos
pb = [12,-2]; #bullet pos
vc = [7,3].*(1/3); #target vel
sb = 2.8480; #bullet speed
tolerance = 0.1; #search tolerance
# Routines
function n = calculate_n(p_c, p_b, v_c, s_b, t)
if (s_b * t == 0)
n = ones(1, 2) * flintmax();
else
n = (p_c.-p_b.+(v_c.*t))./(s_b.*t);
endif
endfunction
score = @(t) abs(1.0 - norm(calculate_n(pc, pb, vc, sb, t)));
# Minimize the score over time space
end_time = fminsearch(score, [0]);
end_score = score(end_time);
if (abs(end_score) <= tolerance)
printf("Time: %d\n", end_time);
calculate_n(pc, pb, vc, sb, end_time)
else
printf("No Solution!");
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment