I'm trying to get Matlab to return the maximum range and angle for a projectile launched from a set height h and initial velocity vO.

9 views (last 30 days)
When I put in a number for theta, my program works. But when I try to use linspace, I get the error message:
??? In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ==> Mech14a at 19
x(n)=vx0*tau*(1-exp(-t(n-1)/tau))+x(n-1); %X position with linear drag
I understand what this error message means. I just have no idea going about fixing it. Thank you in advance for your help! My code is below!
v0=10; theta=linspace(0,90); g=9.8; vterm=50; h=10; %setting initial values
t(1)=0; %setting initial time
y(1)=h; %setting intial height
x(1)=0; %setting initial range
dt=.1;
vx0=v0*cosd(theta); vy0=v0*sind(theta);tau=vterm/g; % calculating constants
for n=2:200
if y>=0
t(n)=t(n-1)+dt;
x(n)=vx0*tau*(1-exp(-t(n-1)/tau))+x(n-1); %X position with linear drag
y(n)=(vy0+vterm)*tau*(1-exp(-t(n-1)/tau))-vterm*t(n-1)+h+y(n-1); %Y position with linear drag
else
break
end
end
MaximumRange=max(x)
hold on
grid on
plot(x,y)
axis([0 inf 0 inf]);
xlabel('X displacement');
ylabel('Y displacement');
title('Trajectory with Linear Drag');

Answers (2)

Walter Roberson
Walter Roberson on 9 Oct 2011
You will need to think more clearly about what you would like the program to do if you have a range of theta.
There is nothing in your current program that calculates a maximum angle as referenced in your description. What do you mean by that? Do you perhaps mean that you want to find the angle at which the projectile would have the maximum range?
I do not know what your tau represents? And I do not see any linear drag term?
Your test
if y>=0
will work, but only accidentally. Remember that y is a vector, so y>=0 will construct the logical array [y(1)>=0, y(2)>=0, ...], and the "if" will be applied against that complete logical array. It so happens that in such a situation supplying an array is equivalent to wrapping the test all() around the array -- the "if" is true if and only if all the y are >= 0. Which happens to be safe for you. I recommend, though, that you consider instead testing if y(n-1)>=0
By the way, if y(n) does become 0 exactly, is there a reason to continue? Your model does not account for "plowing along the ground", so if you have reached the ground there is no point taking another step.
  2 Comments
coppercookie
coppercookie on 9 Oct 2011
Yes, I am trying to find the angle at which the projectile would have the maximum range... Tau is the linear drag. So is the problem where I define my if statement?
coppercookie
coppercookie on 9 Oct 2011
OH! And to answer your question... This simulates a projectile launched from a height h... so by saying y(1)=0, I'm just stating that I'm at sea level rather than on a cliff about to throw my projectile. So depending on the initial angle, I would want to proceed.

Sign in to comment.


Jordan
Jordan on 9 Oct 2011
It seems like you are a bit confused with some syntax. You keep using the '*' for times, which works fine for scalers. When dealing with vectors (like the one you created with the linspace command), matlab uses '*' for matrix multiplication. If you want to do an element by element operation, which I am assuming because you pick a maximum at the end of the program, you want to use the '.*' (and './' for that matter, for essentially the same reason) in stead. hope that helps.
-Jordan

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!