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