How do I solve an equation with an unknown variable?

I have 4 known variables and 1 unknown, but I'm not sure how to solve for it:
v=70
ro=1.225
Sw=5
c=0.5
Here is my equation:
-18==0.5*ro*(v^2)*Sw*c*CM_ac
CM_ac is unknown, how do i solve for it? Are there certain math toolboxes required?

Answers (4)

You can use fzero
v=90; % velocity
ro=1.225; % density at sea level, kg/m^3
Sw=3; % wing area, m^2
c=0.4; % chord, m
%Defining an anonymous function to solve for
%fzero solves for F(x)=0, so we convert out function accordingly
fun = @(x) 0.5*ro*(v^2)*Sw*c*x - (-19)
fun = function_handle with value:
@(x)0.5*ro*(v^2)*Sw*c*x-(-19)
%fzero requires an initial point to start the search for the solution
CM_ac = fzero(fun,0)
CM_ac = -0.0032
You can also use solve, but note that it requires the Symbolic Math Toolbox
syms CM_ac
v=90; % velocity
ro=1.225; % density at sea level, kg/m^3
Sw=3; % wing area, m^2
c=0.4; % chord, m
eqn = -19==0.5*ro*(v^2)*Sw*c*CM_ac;
CM_ac = solve(eqn,CM_ac)
CM_ac = 
The output you get is a symbolic number.
class(CM_ac)
ans = 'sym'
To change into a numeric data type, use a data type -
%Using the default numeric data type of MATLAB
CM_ac = double(CM_ac)
CM_ac = -0.0032
I always use pencil and paper:
v=70 ;
ro=1.225 ;
Sw=5 ;
c=0.5 ;
CM_ac = -18/(0.5*ro*(v^2)*Sw*c)
CM_ac = -0.0024
This isn't a coding problem, but a mathematical problem. Because you have only 1 unknown variable, to solve this all you need to do is rearrange your equation to make CM_ac the subject. To do this, we can divide both sides of the equation by the known values (as each value is multiplied together):
% Initial
-18 = 0.5 * ro * (v^2) * Sw * c * CM_ac
% Divide both sides by ( 0.5 * ro * (v^2) * Sw * c )
-18 / (0.5 * ro * (v^2) * Sw * c ) = CM_ac
% Put the unknown on the left side instead
CM_ac = -18 / ( 0.5 * ro * (v^2) * Sw * c )
Alternatively, you could just manually swap each known letter for its value, and simplify your equation.
Putting the following code into matlab will solve your question. However, this problem is fairly basic formula manipulation they teach in high school.
% State known values
v=70; ro=1.225; Sw=5; c=0.5;
% Find CM_ac
CM_ac = -18 / ( 0.5 * ro * (v^2) * Sw * c )

3 Comments

I understand that of course, but I'm wondering if there is a function where Matlab can do it for me as I have a large series of equartions following this with unknown variables and will need to repeat this process.
If your equations are as easy as the one you posted, you can use "solve". If they are more difficult and "solve" doesn't succeed, you will have to post them here.
That's not the initial question you asked. But as others have suggested, there are ways to solve it in matlab.
Sounds like you have a more interesting problem though, but we need more information. How many equations do you have? Are they always the same equation or different ones? Is there always one unknown variable? What does the data input look like?

Sign in to comment.

Hi @TC
Yes, there is a useful function called 'isolate()' in Symbolic Math Toolbox. If you want to do this iteratively, please post the true problem.
syms r_o v S_w c CM_ac
% write the equation:
eqn = - 18 == 0.5 * r_o * (v^2) * S_w * c * CM_ac
eqn = 
% isolate CM_ac in the equation
CM_ac_Sol = isolate(eqn, CM_ac)
CM_ac_Sol = 
% substitute the symbolic variables with values: ro = 1.225, v = 70, Sw = 5, c = 0.5
CM_ac_Sol = subs(CM_ac_Sol, {r_o, v, S_w, c}, {1.225, 70, 5, 0.5})
CM_ac_Sol = 
% return the solution in floating-point number with 4 significant digits
vpa(CM_ac_Sol, 4)
ans = 

Categories

Products

Release

R2023a

Asked:

TC
on 18 Sep 2023

Commented:

on 18 Sep 2023

Community Treasure Hunt

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

Start Hunting!