Using Fsolve to find value of 2 variables with different functions

조회 수: 1 (최근 30일)
Ruth
Ruth 2020년 5월 8일
댓글: Rena Berman 2020년 10월 12일
I am trying to solve a system of equations with variables xp and xq. Each is defined by a different function and has a different value at 4 stages: xp(1), xp(2), xp(3), xp(4) ect.
I tried to find a solution by writing out all the equations and using vpasolve but there were 16 equations and no solution was found after 20 minutes. I have found similar examples using fsolve but I don't know how to adapt this code to solve for two variables (xp and xq). Any help or suggestions of an alternative approach would be appreciated.
function x =LLESolver(xp,xq)
N=4
x0=ones(N*2,1);
[x,fval]=fsolve(@LLE,x0,xp,xq)
function P=LLE(xp,xq)
xpa = 1.2; % kg/m3, conc. of solute in entering aqueous stream
ypb = 0.0;
L = 2.0; % m3/s
V = 1.0; % m3/s
N = 4;
xqa = 1.6; % kg/m3, conc. of solute in entering aqueous stream
yqb = 0.0;% kg/m3, conc. of solute in entering organic stream
for i=1:4
if i==1
P(i)=L*(xpa-x(i))-V*(EquRel_P(i)-EquRel_P(i+1))
elseif i==N
P(i)=L*(xp(i-1)-x(i))-V*(EquRel_P(i)-ypb)
else
P(i)=L*(xp(i-1)-x(i))-V*(EquRel_P(i)-EquRel_P(i+1))
end
end
for i=5:8
if i==5
P(i)=L(xqa-x(i))-V*(EquRel_Q(i)-EquRel_Q(i+1))
elseif i==8
P(i)=L*(x(i-1)-x(i))-V*(EquRel_Q(i)-yqb )
else
P(i)=L(xqa-x(i))-V*(EquRel_Q(i)-EquRel_Q(i+1))
end
end

답변 (1개)

Walter Roberson
Walter Roberson 2020년 5월 8일
function x =LLESolver(xp,xq)
N = 4;
x0 = ones(N*2,1);
[x,fval]=fsolve(@(x) LLE(x, xp, xq), x0)
function P = LLE(x, xp, xq)
  댓글 수: 3
Walter Roberson
Walter Roberson 2020년 5월 9일
[x,fval]=fsolve(@(xp) LLE(xp, xq), x0)
with
function P = LLE(xp, xq)
However, what do you want to do with the xp that was passed into LLESolver ?
Ruth
Ruth 2020년 5월 9일
I want to solve the simultaneous equations for both xp and xq. Can fsolve do that?

댓글을 달려면 로그인하십시오.

카테고리

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