Last active
October 8, 2017 21:11
-
-
Save efruchter/7bd55825dc01da6d85ecb6bf8199ce67 to your computer and use it in GitHub Desktop.
Complex numerical solution for a simple geometry problem
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
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