fsolve with multiple parameters

조회 수: 2 (최근 30일)
Andrew Selvadoss
Andrew Selvadoss 2019년 10월 16일
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개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by