Baseball
2 views (last 30 days)
Show older comments
I have to create a function that calculates the initial angle at which the baseball is thrown and then plot it against distance for different velocities. the function file is as follows-
function [angle] = basb( y0,y,x,g,v0)
%This function file computes the initial angle using fzero function.
%Inputs are :
%y0=initial height
%y=final height
%x=range
%g=gravitational accelaration
%vo=initial speed
%Output is the initial angle
f = @(ang0) (tand(ang0)).*x-((g/(2*(v0.^2)*(cosd(ang0)^2))).*(x.^2))+y0-y;
guess=5;
angle=fzero(f,guess);
end
The script file to call the function is -
%Baseball driver file to plot range vs initial angle
clc
g=9.81;
y0=2;
y=1;
%color vector to differentiate between the three graphs
color=['r','g','b','m'];
count=0;
for vo=25:10:45
count=count+1;
for x=20:120
angle(x)= baseb(y0,y,x,g,vo);
end
angle=angle(10:130);
x=(20:120);
ylim([0,40])
%Plots all the three graphs with different colors on the same plot
plot(x,angle,color(count))
hold on
end
legend('15 m/s','25m/s','35m/s','45m/s')
xlabel('x')
ylabel('angle')
title('Plot of range versus angle')
......it gives me the plot, but it also gives this error in the command window multiple times-
Exiting fzero: aborting search for an interval containing a sign change because NaN or Inf function value encountered during search.
(Function value at -1.61409e+017 is -Inf.)
Check function or try again with a different starting value.
I can't understand what's wrong with my code
0 Comments
Answers (1)
Matt Tearle
on 25 Oct 2011
The problem is really in the math. fzero can't find a zero from the given initial guess because your function has a nasty singularity in it. Try this to see what's happening:
g=9.81;
y0=2;
y=1;
v0 = 25;
x = 50;
f = @(ang0) (tand(ang0)).*x-((g./(2*(v0.^2)*(cosd(ang0).^2))).*(x.^2))+y0-y;
plot(1:85,f(1:85))
max(f(1:85))
Note that there's a zero, so your function will work fine for x = 50. Now change x to 100, and run that code again. No zero, so fzero is screwed.
As a side issue, I don't get the plot, because of the incorrect indexing command angle = angle(10:130). But the real problem, I think, stems from angle(x) = .... x is the angle, not an index. I suspect you want angle(count) = .... You should also preallocate angle.
6 Comments
Sean de Wolski
on 25 Oct 2011
@Jan, I'd wondered the same thing. Apparently others type it into google a lot since the goog finished the question for me:
http://www.snopes.com/business/names/worldseries.asp
Matt Tearle
on 26 Oct 2011
I thought it was because Canada was (theoretically) involved. Either way, I've been too busy with a real* world championship (with which the US was (theoretically) involved!).
* 20 nations from 6 continents, as opposed to 2(ish) from 1.
See Also
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!