Borrar filtros
Borrar filtros

Baseball

3 visualizaciones (últimos 30 días)
Nishant Nain
Nishant Nain el 25 de Oct. de 2011
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

Respuestas (1)

Matt Tearle
Matt Tearle el 25 de Oct. de 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 comentarios
Sean de Wolski
Sean de Wolski el 25 de Oct. de 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
Matt Tearle el 26 de Oct. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Import and Analysis en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by