MATLAB Answers

Solving set of 5 non-linear simultaneous equations using fsolve

13 ビュー (過去 30 日間)
Anahita Piri
Anahita Piri 2020 年 5 月 5 日
編集済み: Anahita Piri 2020 年 5 月 14 日
Hi all, I've been trying to use matlab's fsolve function to solve a fluid pipe problem where I have narrowed down the unknown variables into 5. I have run into the error that 'fsolve stopped because it exceeded the function evaluation limit' and gives me complex answers. Can anyone see something wrong with the code itself? Assuming equations are right?
I have used a function handle, and called it in another script. (I do not have much programming background in advance)
function F=fluidssolver(X)
dV = X(1);
V1 = X(2);
f1 = X(3);
f2 = X(4);
f3 = X(5);
rho = 999.1;
mu = 1.138 * 10^-3;
g = 9.81;
Za = 2.1;
Zb = 9.1;
L1 = 25.1;
L2 = 25.1;
L3 = 25.1;
D1 = 3/100;
D2 = 4/100;
D3 = 5/100;
n = 0.68;
dW = 8000;
epsilon = 0;
A1 = pi*D1^2*0.25;
A2 = pi*D2^2*0.25;
A3 = pi*D3^2*0.25;
Re1 = (rho*V1*D1)/mu;
V2 = sqrt((f1*L1*D2)/(f2*L2*D1))*V1;
V3 = sqrt((f1*L1*D3)/(f3*L3*D1))*V1;
Re2 = (rho*V2*D2)/mu;
Re3 = (rho*V3*D3)/mu;
F(1) = dW*n - rho * g * dV * ((Zb-Za) + f1 * (L1/D1) * (V1^2/(2*g))) ;
F(2) = 1/(sqrt(f1)) + 2 * log10 (((epsilon/D1)/3.7) + (2.51/((Re1*sqrt(f1)))));
F(3) = 1/(sqrt(f2)) + 2 * log10 (((epsilon/D2)/3.7) + (2.51/((Re2*sqrt(f2)))));
F(4) = 1/(sqrt(f2)) + 2 * log10 (((epsilon/D3)/3.7) + (2.51/((Re3*sqrt(f3)))));
F(5) = dV - V1*A1 - V2*A2 - V3*A3;
% F(5) = dV - V1*A1 - (sqrt((V1*f1*L1*D2)/f2*L2*D1) * V1)*A2 - (sqrt((V1*f1*L1*D3)/f3*L3*D1) * V1)*A3
end
and as for the function, I have initial guesses in the following different script:
fun = @fluidssolver
fsolve(fun,[0.5, 5, 0.12, 0.12, 0.12])

  3 件のコメント

Rik
Rik 2020 年 5 月 6 日
Why did you remove your question content? That is a very rude this to do. Several people went to the trouble of reading your question, understanding it, solving the question, and writing a response. Now you have made all that effort a lot less useful for anyone in the future that might have a similar question.
Anahita Piri
Anahita Piri 2020 年 5 月 6 日
If you'd give me a moment, I was trying to upload a copy of the code that couldn't be copied by my classmates. I mistakenly uploaded the full code, and some people in my class found it for the assignment which is borderline plagiarism, and so I removed it until I could reupload a more suitable line of code that cannot be copied for the assignment.
Rena Berman
Rena Berman 2020 年 5 月 14 日
(Answers Dev) Restored edit

サインインしてコメントする。

採用された回答

Alex Sha
Alex Sha 2020 年 5 月 6 日
Hi, Anahita, if you don't want complex answers, then there are problems in your eauqtions, here taking second equation as an example:
1/(sqrt(f1)) + 2 * log(((epsilon/D1)/3.7) + (2.51/((Re1*sqrt(f1)))))=0
the above equation can simpliy written as:
1/(sqrt(f1)) + 2 * log(s)=0
where s=((epsilon/D1)/3.7) + (2.51/((Re1*sqrt(f1))))
it is:
1/(sqrt(f1)) = -2 * log(s)
obviously, both "1/(sqrt(f1))" and "2 * log(s)" are impossible to be negetive values, this would make the equation untenable.
Same problems for your third and fourth equations。

  0 件のコメント

サインインしてコメントする。

その他の回答 (3 件)

Matt J
Matt J 2020 年 5 月 5 日
編集済み: Matt J 2020 年 5 月 5 日
You should get rid of all the square roots on the unknown parameters. Write the equations in terms of new variables like z1=sqrt(f1), z2=sqrt(f2), so that the computations don't involve any square roots.
Also, if you know that some of the parameters are supposed to be non-negative, you should also use lsqnonlin, instead of fsolve, which would alllow you to put lower bounds (and upper bounds, if desired) on the unknowns.

  2 件のコメント

Anahita Piri
Anahita Piri 2020 年 5 月 5 日
So are you saying that possibly putting lower bounds so that the values can't go below 0 may help? I believe none of the variables should be negative.
Matt J
Matt J 2020 年 5 月 5 日
That is part of it - if the values go below 0 during the search, the sqrt and log operations will generate complex values.
There is an additional problem, however. Even if you bound the parameters to be positive, the sqrt(f) operations are non-differentiable at f=0, whereas fsolve assumes that your objective function is smooth. Therefore, the change of variables that I mentioned is important as well.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2020 年 5 月 5 日
your variables are going negative as fsolve makes projections to calculate gradients. Your objective expects several variables to be positive and gives complex results if not.
You cannot proceed with fsolve.
If you have the symbolic toolbox then vpasolve permits bounds.
consider forming the sum of squares of your functions, and minimizing using fmincon with lb.

  2 件のコメント

Anahita Piri
Anahita Piri 2020 年 5 月 5 日
That makes much more sense now, thank you for explaining how fsolve is working!
If using vpasolve, would I still have to form the sum of squares and minimise it (similar to Matt's idea of using lsqnonlin)? Or would vpasolve fix the issue of fsolve failiing to work when the variables get negative in an iteration?
Walter Roberson
Walter Roberson 2020 年 5 月 5 日
vpasolve can handle bounds better (relatively). There are still some cases where it does not bounce off of a boundary and instead just gives up when it notices a boundary.

サインインしてコメントする。


Anahita Piri
Anahita Piri 2020 年 5 月 6 日
Thank you all, while these solutions would have made better code it ended up working when I changed my initial guess for one of the values closer to its true value.
Thank you for the time and help!

  0 件のコメント

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by