I don't understand why this script is outputting imaginary values
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Matthew
am 26 Okt. 2023
Verschoben: Walter Roberson
am 27 Okt. 2023
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 Kommentare
Akzeptierte Antwort
Walter Roberson
am 26 Okt. 2023
Verschoben: Walter Roberson
am 27 Okt. 2023
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 Kommentare
Weitere Antworten (1)
John D'Errico
am 26 Okt. 2023
Bearbeitet: John D'Errico
am 26 Okt. 2023
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 Kommentare
Siehe auch
Kategorien
Mehr zu Systems of Nonlinear Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!