Baseball

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

回答(1 个)

Matt Tearle
Matt Tearle 2011-10-25
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 个评论

fzero could probably find a solution in the number of viewers watching the current world series...
Sorry, you'll have to explain that -- I've been too busy watching the Rugby WC. Or is that kinda the point...?
That was kinda the point!
Jan
Jan 2011-10-25
"World" series? Sounds like a US-event.
@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
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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Graphics Performance 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by