Return only the positive values to be used in subsequent eqns

2 views (last 30 days)
Hello all,
I am trying to get matlab to only display, and assign the positive value attained from solving the syms of eqn... to be used in subsequent equations. It keeps displaying both. I have tried a couple of other ways Please help
Here is copy paste code if you are feeling super nice.
g= -32.3;
syms v t
eqn3 = v > 0;
eqn4 = t > 0;
eqn1 = 3 == v * cos (45) *t;
eqn2 = t == (2 * v * sind(45))/g ;
eqns =[eqn1,eqn2]
sol = solve([eqns],[v,t]);
vS = vpa(sol.v)
tS = vpa(sol.t)
sol = solve([eqn3,eqn4],[v,t])
vpos = vS(vS>=0);
tpos = tS(tS>=0);
vpos
tpos
I have also tried it this way
g= -32.3;
syms v t
eqn3 = v > 0;
eqn4 = t > 0;
eqn1 = 3 == v * cos (45) *t;
eqn2 = t == (2 * v * sind(45))/g ;
eqns =[eqn1,eqn2]
sol = solve([eqns],[v,t]);
vS = vpa(sol.v)
tS = vpa(sol.t)
if vS == vSpos(vS>=0)
vSpos = vS(vSol>=0);
vS= vSpos
end
vS
tS

Answers (1)

Alan Stevens
Alan Stevens on 6 Feb 2021
Why not use
g = 32.3;
v = sqrt(3*g/(2*cosd(45)*sind(45)));
t = 2*v*sind(45)/g;
which will give you positive values immediately?
Incidentally, you probably want cosd(45), not cos(45).
  2 Comments
Susan Chestnutt
Susan Chestnutt on 6 Feb 2021
The idea is to be able to have the user input a target distance.... where I'm using the value 3 and have the program calculate the v and t. I just started using matlab 1 week ago so I am doing peicewise to make sure I understand the different parts of the code.
This is a more complete idea of what I was going for...and the code was retuning errors surrounding solving the system ...so I started with the basics of sys solving in a new live script.
Projectile mini 1
projectileImage1.png
Set up the parameters
y_o = 0;
x_o = 0;
dt=.001;
t = 0:dt:1.1;
g = -32.2;
a = zeros(15,75)
y = zeros(1,length(t))
x = zeros(1,length(t))
vy = zeros(1,length(t))
x_t =
x (1) = x_o;
y(1) = y_o;
syms v a t
S = solve (x_t == x + v * cos (a) *t, y == v *t *sind (a) +1/2 *g*t^2, t == (2 * v * sind(a))/g)
Simulate the trajectory
vy(1:end) = v * sind(a);
for i = 2 :length(t)
vy (i) = vy(i-1) + g * dt;
x (i) = x (i-1) + v * cosd(a) * dt;
y (i) = y(i-1) + vy(i) * dt + 1/2 * g * dt.^2;
end
Plotting the balls path
plot (t,y)
title ("Height of the ball")
Susan Chestnutt
Susan Chestnutt on 6 Feb 2021
Nevermind, I figured it out......
If I get points for answering my own question let me know and I will post. If you are curious and can contact me directly I would be happy to post ans

Sign in to comment.

Categories

Find more on Mathematics 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!