I don't understand why this script is outputting imaginary values
Show older comments
The code is trying to solve 8 nonlinear functions for some fluids homework I am doing. fsolve runs but the output is wrong and I think it has something to do with the equations in the function. I can post the equations I want if that helps but something is wrong with one of the equations because I am getting complex values back.
format long g
xguess = [.0001,.0001,.0001,.0001,.0001,.0001,.0001,.0001];
f = @sub1;
xroot = fsolve(f,xguess);
xroot
froot = sub1(xroot); %check answer
froot
function f = sub1(x)
f = x*0;
pipeDA = .3;
pipeDB = .45;
pipeLA = 500;
pipeLB = 800;
epsilon = .000045;
density = 720;
viscosity = .00029;
gravity = 9.8;
f(1) = (4*x(2)/(pi*pipeDA^2))-x(1);
f(2) = (4*x(4)/(pi*pipeDB^2))-x(3);
f(3) = x(2)+x(4)-3;
f(4) = (x(6)*(pipeLB/pipeDB)*(x(3)^2/(2*gravity)))-(x(5)*(pipeLA/pipeDA)*(x(1)^2/(2*gravity)));
f(5) = ((density*x(1)*pipeDA)/viscosity) - x(7);
f(6) = ((density*x(3)*pipeDB)/viscosity) - x(8);
f(7) = 1.737*log(.269*(epsilon/pipeDA)+1.257/(x(7)*sqrt(x(5))))+(1/sqrt(x(5)));
f(8) = 1.737*log(.269*(epsilon/pipeDB)+1.257/(x(8)*sqrt(x(6))))+(1/sqrt(x(6)));
end
Accepted Answer
More Answers (1)
John D'Errico
on 26 Oct 2023
Edited: John D'Errico
on 26 Oct 2023
1 vote
Hint: the log of a negative number is imaginary.
Hint: The sqrt of a negative number is imaginary.
Do you take logs in there? (Yes.) Do you take square roots? (Yes.)
Does fsolve assure that none of the numbers returned will not cause a problem? (No.)
Categories
Find more on Structural Mechanics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!