How do I solve an equation with an unknown variable?
조회 수: 68 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
답변 (4개)
Dyuman Joshi
2023년 9월 18일
편집: Dyuman Joshi
2023년 9월 18일
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)
%fzero requires an initial point to start the search for the solution
CM_ac = fzero(fun,0)
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)
The output you get is a symbolic number.
class(CM_ac)
To change into a numeric data type, use a data type -
%Using the default numeric data type of MATLAB
CM_ac = double(CM_ac)
댓글 수: 0
Harry
2023년 9월 18일
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
Torsten
2023년 9월 18일
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.
Harry
2023년 9월 18일
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?
Sam Chak
2023년 9월 18일
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
% isolate CM_ac in the equation
CM_ac_Sol = isolate(eqn, CM_ac)
% 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})
% return the solution in floating-point number with 4 significant digits
vpa(CM_ac_Sol, 4)
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!