I don't understand why this script is outputting imaginary values
조회 수: 2 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Walter Roberson
2023년 10월 26일
이동: Walter Roberson
2023년 10월 27일
There is one real solution, and three complex solutions.
format long g
syms x [1 8]
eqn = sub1(x)
char(eqn.')
solv = vpasolve(eqn)
partsol = solve(eqn(1:end-1), x(2:end))
lasteqn = subs(eqn(end), partsol);
residue = lasteqn;
%{
tiledlayout('flow')
X = linspace(0, 40);
for K = 1 : length(lasteqn)
F = matlabFunction(residue(K));
Y = F(X);
nexttile; plot(X, real(Y), X, imag(Y)); title(string(K));
end
%}
solparts = subs(x(2:end), partsol);
for K = 1 : length(lasteqn)
lastsol(K) = vpasolve(lasteqn(K), 10);
var = symvar(lasteqn(K));
if isempty(var)
fprintf('no var for eqn %d\n', K);
vpa(lasteqn(K), 10)
elseif length(var) > 1
fprintf('multiple var for eqn %d\n', K)
vpa(lasteqn(K), 10)
else
fullsol(K,:) = [lastsol(K), subs(solparts(K,:), var, lastsol(K))];
end
end
double(fullsol)
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
댓글 수: 0
추가 답변 (1개)
John D'Errico
2023년 10월 26일
편집: John D'Errico
2023년 10월 26일
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.)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Systems of Nonlinear Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!