# missing/incorrect use of math operators?

1 view (last 30 days)
Jocelyn on 12 Nov 2020
Answered: Peter Perkins on 19 Nov 2020
Good evening,
I am new to matlab, and was wondering if someone could look at my code to see if I am incorrectly using/missing some sort of math operator (i.e. I've noticed sometimes you need a ./ verses a / for divison).
My code produces a table however, the time of flight column should be consistantly increasing in a value, and my striking velocity values are too large. I am wondering if I am missing something in my code.
Below is my code:
k3 = 0.5;
Vx_o = 2296;
t = 0;
for i = 1:6
x(i) = (i-1)*200*3; % range in feet
Vx(i) = (sqrt(Vx_o) - ((k3/2)*x(i)))^2; % x velocity
t(i) = (x(i)/Vx_o)*sqrt(Vx_o/Vx(i)); % time
end
T = table(x(:)/3, x(:), Vx(:), t(:), 'VariableNames',{'yards', 'feet', 'striking velocity', 'time of flight'})

Geoff Hayes on 13 Nov 2020
Jocelyn - why should the time of flight value be increasing in value? Is it because it is dependent upon the previous value? Do you want add the previous value to this current like
for i = 1:6
x(i) = (i-1)*200*3; % range in feet
Vx(i) = (sqrt(Vx_o) - ((k3/2)*x(i)))^2; % x velocity
t(i) = (x(i)/Vx_o)*sqrt(Vx_o/Vx(i)); % time
if i > 1
t(i) = t(i) + t(i-1); % is this what you want?
end
end
As for the striking velocity being too large, consider the line
Vx(i) = (sqrt(Vx_o) - ((k3/2)*x(i)))^2;
You are taking sqrt(Vx_o) (so the square root of some unit) and subtracting ((k3/2)*x(i)) (in some unit) I think that this is incorrect as you are subtracting values with different units. You will want to ensure that both have the same units before you subtract one from the other.

Peter Perkins on 19 Nov 2020
Jocelyn, modulo Geoff's concerns, I think your whole loop can be done as
x = 200*3*(0:5)';
Vx = (sqrt(Vx_o) - ((k3/2).*x))^2;
t = (x./Vx_o).*sqrt(Vx_o./Vx);
and then I guess
t = cumsum(t);
Also, if time should be increasing, that suggests that maybe T should be a timetable rather than a table.