Solving a Nonlinear Equation

7 次查看(过去 30 天)
David Oswalt
David Oswalt 2019-4-16
回答: Ayush 2024-11-6,6:09
I have the following equation:
p41 = p21*(1 - ((g4 - 1)*(a1/a4)*(p21 - 1))/(sqrt((2*g1)*(2*g1 + (g1 + 1)*(p21 -1 )))))^((-2*g4)/(g4 - 1));
where p21 is the only unknown and the rest are all constants. This equation is difficult (or perhaps impossible, not sure) to solve analytically. When solving problems by hand using this equation, we were taught to "guess and check" using trial and error by iteration. If I were to attempt to solve this equation for p41 using Matlab 2016b, what would be the best way to do so?
I have attempted:
syms p21
eqn = p21*(1 - ((g4 - 1)*(a1/a4)*(p21 - 1))/(sqrt((2*g1)*(2*g1 + ...
(g1 + 1)*(p21 -1 )))))^((-2*g4)/(g4 - 1)) == p41;
solx = solve(eqn,p21)
which returns a crazy 11x1 sym full of polynomials in terms of z. Is this equation not solveable analytically using Matlab? If so, what would be the best way to solve it iteratively for p21, given all other values are constant?
Thanks a lot!
  1 个评论
Walter Roberson
Walter Roberson 2019-4-16
solx = solve(eqn, p21, 'returnconditions', true);
and then check whether solx.Parameters is empty or not. If it is not empty, then solx.p21 will refer to the variables named in solx.Parameters, and solx.Conditions will define the restrictions on those variables.

请先登录,再进行评论。

回答(1 个)

Ayush
Ayush 2024-11-6,6:09
You can use fsolve to solve such complex equation as it allows you to solve nonlinear equations numerically.
Here is the sample code for the same:
% Define constants
g1 = ...; % define your value
g4 = ...; % define your value
a1 = ...; % define your value
a4 = ...; % define your value
p41 = ...; % define your value
% Define the function to solve
equation = @(p21) p21 * (1 - ((g4 - 1) * (a1 / a4) * (p21 - 1)) / ...
sqrt((2 * g1) * (2 * g1 + (g1 + 1) * (p21 - 1))))^((-2 * g4) / (g4 - 1)) - p41;
% Initial guess for p21
initial_guess = 1;
% Solve using fsolve
options = optimoptions('fsolve', 'Display', 'iter');
[p21_solution, fval, exitflag] = fsolve(equation, initial_guess, options);
You can read more about fsolve from here: https://www.mathworks.com/help/optim/ug/fsolve.html

类别

Help CenterFile Exchange 中查找有关 Systems of Nonlinear Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by