I am attempting to simulate the trajectory of a table tennis ball using a system of differential equations and the algorithm works correctly, but I need the system to terminate at the time when the ball hits the ground. I am using fzero to solve this, but it keeps iterating even after the zero is found. How can I resolve this problem?
Here is my code in its entirety:
syms x(t) y(t) z(t) k w1 w2 vx_0 x_0 vy_0 y_0 vz_0 z_0k = 0.25;w1 = -0.4;w2 = 0.3;x_0 = 0;y_0 = 0;z_0 = 0;vx_0 = 10;vy_0 = 15;vz_0 = 20;eqn_x = diff(x, 2) == -k*diff(x, t) + w2*diff(z, t);eqn_y = diff(y, 2) == -k*diff(y, t) + w1*diff(z, t);eqn_z = diff(z, 2) == -k*diff(z, t) + w1*diff(y, t) - w2*diff(x, t) - 9.81;odes = [eqn_x; eqn_y; eqn_z];Dx = diff(x, t);Dy = diff(y, t);Dz = diff(z, t);cond1 = [x(0) == x_0, Dx(0) == vx_0];cond2 = [y(0) == y_0, Dy(0) == vy_0];cond3 = [z(0) == z_0, Dz(0) == vz_0];conds = [cond1; cond2; cond3];[xSol(t), ySol(t), zSol(t)] = dsolve(odes, conds);options = optimset('Display','iter');endp = fzero(zSol, [2 2.5], options)fplot3(xSol, ySol, zSol, [0 endp])
The last part returns the following output:
Func-count x f(x) Procedure 2 2 2.44469 initial 3 2.14832 0.291887 interpolation 4 2.16747 -0.00448884 interpolation 5 2.33373 -2.7506 bisection 6 2.41687 -4.23685 bisection 7 2.45843 -5.00757 bisection 8 2.47922 -5.39974 bisection 9 2.48961 -5.59752 bisection 10 2.4948 -5.69683 bisection 11 2.4974 -5.74659 bisection 12 2.4987 -5.7715 bisection 13 2.49935 -5.78396 bisection 14 2.49968 -5.79019 bisection 15 2.49984 -5.79331 bisection 16 2.49992 -5.79487 bisection 17 2.49996 -5.79565 bisection 18 2.49998 -5.79604 bisection 19 2.49999 -5.79623 bisection 20 2.49999 -5.79633 bisection 21 2.5 -5.79638 bisection 22 2.5 -5.7964 bisection 23 2.5 -5.79641 bisection 24 2.5 -5.79642 bisection
Is there a way for the program to terminate at iteration four while still keeping it general for when I change the input factors?
Best Answer