How EXACTLY doe the solve function work?

2 views (last 30 days)
Dylan
Dylan on 17 Sep 2013
I'm trying to solve a simple projectile motion problem using the solve command, but it does not recognize already-assigned variables, and outputs an answer that is a function of itself. Am I misunderstanding how solve works? Apologizing in advanced for the bad formatting; I can't figure out how to work it.
if true
% code
vel=100;
theta=50;
height=150;
v_vert=vel*sind(theta); %Y-velocity (m/s)
g=9.81; %gravity constant (m/s^2)
y_f=-height;
t=0
time=solve('y_f=v_vert*t-g/2*t^2',t)
end
% code
end
and it returns this:
t =
0
time =
t*v_vert - (g*t^2)/2 %code

Answers (1)

Walter Roberson
Walter Roberson on 17 Sep 2013
When you pass a quoted string to solve(), existing variables are not used (not unless they were defined within the MuPAD environment, which is not the case for your code).
You should pass in symbolic expressions instead.
syms t
vel=100;
theta=50;
height=150;
v_vert=vel*sind(theta); %Y-velocity (m/s)
g=9.81; %gravity constant (m/s^2)
y_f=-height;
time = solve(y_f == v_vert*t-g/2*t^2, t)
Note: if you are using a version of MATLAB that is a couple of years older, it might complain about the == being unable to compare symbolic objects. If that happens, then use the usual trick:
solve( A == B )
is the same as
solve( (A) - (B) )

Tags

Community Treasure Hunt

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

Start Hunting!