fsolve with multiple parameters
6 views (last 30 days)
Show older comments
I have defined a function with 3 inputs that finds the trajectory of a baseball using Euler's method.
The three inputs are the initial velocity, intial angle, and flag which determines the return.
What I mean by flag is that if it is 0, the function returns the height of the baseball over home plate.
If flag is 1, the function returns the entire trajectory of the baseball (A matrix containing index, position (x,y), and time).
function ret = xyt(v0,theta0,flag)
m = 0.328;
k = 0.01;
g = 32.174;
xhome = 60.5;
spin = 0;
Vx = v0*cosd(theta0);
Vy = v0*sind(theta0);
Traj = [0 0.0 8 0];
for i = 1:(xhome*2)
dVx = (-k/m)*sqrt((Vx.^2)+(Vy.^2));
dVy = ((-k/m)*Vy*sqrt((Vx.^2)+(Vy.^2))-g+(spin/m))/Vx;
Traj(i+1,1) = i;
Traj(i+1,2) = 0.5*i;
Traj(i+1,3) = (0.5*(Vy/Vx))+Traj(i,3);
Traj(i+1,4) = (0.5/Vx)+Traj(i,4);
Vx = Vx+dVx*0.5;
Vy = Vy+dVy*0.5;
end
if flag == 0
ret = Traj((2*xhome)+1,3);
else
ret = Traj;
end
end
However, I want to try and find the initial angle given that the initial velocity is 147 and flag is 0 and the return is 3 (3 feet above homeplate).
I tried this code
v0 = 147;
x0 = 3;
flag = 0;
[x,fval] = fsolve(@(theta) xyt(v0,theta,flag), x0);
However, the return of this
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
The return is no where near the expected answer which should be around 9.2. Instead the return for x is 6.1420 and the return for fval is extremely small.
Please advise what to do. I don't fully understand the syntax of fsolve with user defined functions. Thank you.
0 Comments
Answers (1)
Deepak
on 17 Jan 2025
We can determine the initial angle required for a baseball to reach a specific height above home plate by using numerical root-finding techniques such as "fsolve" in MATLAB. By defining an objective function that calculates the difference between the computed height from the trajectory function and the desired height (3 feet), "fsolve" iteratively adjusts the angle to minimize this difference to zero. Starting with a reasonable initial guess for the angle, "fsolve" efficiently converges to the solution, providing us with the angle that achieves the desired trajectory height.
Below is the modified MATLAB code to achieve the same:
% Define the objective function
objectiveFunction = @(theta) xyt(v0, theta, flag) - 3;
% Initial guess for theta
theta_guess = 9.0; % A reasonable guess close to the expected result
% Use fsolve to find the angle
options = optimoptions('fsolve', 'Display', 'iter'); % Optional: Display iterations
[theta_solution, fval, exitflag, output] = fsolve(objectiveFunction, theta_guess, options);
% Display the result
fprintf('The initial angle that results in a height of 3 feet is approximately %.4f degrees.\n', theta_solution);
Please find attached the documentation of functions used for reference:
optimoptions: www.mathworks.com/help/optim/ug/optim.problemdef.optimizationproblem.optimoptions.html
I hope this helps in resolving the issue.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!