Reputation: 21573
The equation I want to solve is
syms w v;
rho_air = 1.25;
equ = w == 0.5 * rho_air * v^2
The problem is that sometimes, I want to solve w from v, an sometimes from v to w.
How can I do it?
I only know to do it like this(This is now not working, and I don't know why):
syms v;
rho_air = 1.25;
w = 1;
equ = w == 0.5 * rho_air * v^2;
But then I have to change it to solve v, like
syms w;
rho_air = 1.25;
v = 1;
equ = w == 0.5 * rho_air * v^2;
which is quite repetative. Is there anyway to solve it more elegantly?
Upvotes: 1
Views: 439
Reputation: 112759
In old Matlab versions you need to define the equation as a string; then you can apply solve
:
>> syms w v;
rho_air = 1.25;
>> equ = 'w = 0.5 * rho_air * v^2';
>> solve(equ, w)
ans =
0.5*rho_air*v^2
>> solve(equ, v)
ans =
(2^(1/2)*w^(1/2))/rho_air^(1/2)
-(2^(1/2)*w^(1/2))/rho_air^(1/2)
Upvotes: 1
Reputation: 13945
Here is a way using a function, in which the inputs are the variable to solve for (as a string) and rho_air. It's quite self-explanatory. And I don't know why your above code does not work; you are just missing the call to solve and you're good to go :)
function [ out ] = SolveFor(Var2Solve,rho_air)
if strcmp(Var2Solve,'v')
syms Var2Solve
v = Var2Solve
w = 1;
equ = w == 0.5 * rho_air * v^2;
out = solve(equ,v)
elseif strcmp(Var2Solve,'w')
syms Var2Solve
w = Var2Solve
v = 1;
equ = w == 0.5 * rho_air * v^2;
out = solve(equ,w)
end
end
TESTS (in command window):
SolveSym('v',1.25)
out =
(2*10^(1/2))/5
-(2*10^(1/2))/5
and
SolveSym('w',1.25)
out =
5/8
Upvotes: 1