Please help with fsolve, i keep getting this error

2 visualizaciones (últimos 30 días)
Colby
Colby el 20 de Mzo. de 2023
Editada: Walter Roberson el 21 de Mzo. de 2023
%% Question 2
% lengths given in the tables
L1 = 20;
L2 = 10;
L3 = 30;
% diameter given in the tables
D1 = .167;
D2 = .2083;
D3 = .125;
% Given values of ro, mu, and e from table
ro = .0631;
mu = 0.0000205;
e = .00085;
% converting the diameter to area
A1 = pi*(D1/2)^2;
A2 = pi*(D2/2)^2;
A3 = pi*(D3/2)^2;
% equation to solve for the velocites
V1 = Q1/A1;
V2 = Q2/A2;
V3 = Q3/A3;
% if statement for reynolds number
Re1 = 2000;
if Re1 <= 2300
f = 64/Re1;
elseif Re1>2300
f=0.25*(log(((e/D1)/3.7)+5.74/Re1^0.9))^-2;
end
% function for fsolve
fun = @(Q) [Q(1)+Q(2)+Q(3)-750; f*(L1/D1)*((Q(1)/A1)^2/2)-deltaP/ro; f*(L2/D2)*((Q(2)/A2)^2/2)-deltaP/ro; f*(L3/D3)*((Q(3)/A3)^2/2)-deltaP/ro];
Q0 = [120;21;24;23];
Q_2 = fsolve(fun,Q0);
fprintf('The value of Q1 is %d\n',Q1)
fprintf('The value of Q2 is %d\n',Q2)
fprintf('The value of Q3 is %d\n',Q3)
fprintf('The value of Q is %d\n',Q)
fprintf('The value of deltaP is %d\n',deltaP)
this is the error i keep getting
Array indices must be positive integers or logical values.
Error in Project_2_updated (line 100)
Q(0) = [120;21;24;23];
PLEASE HELP PLEASE
  1 comentario
John D'Errico
John D'Errico el 20 de Mzo. de 2023
When you get an error, give the COMPLETE error. Not just part of it. EVERYTHING IN RED.
Next, we don't actually have enough information to even show you what is wrong, since you don't tell us the value of deltaP. As well, Q1 is undefined.
We cannot see inside your computer. Sorry.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 21 de Mzo. de 2023
Editada: Walter Roberson el 21 de Mzo. de 2023
Your posted code has
Q0 = [120;21;24;23];
which is fine.
But your error message is from running some other code -- code that has
Q(0) = [120;21;24;23];
That other code is attempting to index element #0 of variable Q. MATLAB indices start at 1 -- and if you had used Q(1) = [120;21;24;23]; then you would have received an error from trying to store 4 elements into a single location.
MATLAB does not have any notation for defining initial conditions of a variable in terms of index 0.
The posted code that uses Q0 instead of Q(0) is fine, at least with regards to that part of the code.
Q_2 = fsolve(fun,Q0);
fprintf('The value of Q1 is %d\n',Q1)
fprintf('The value of Q2 is %d\n',Q2)
fprintf('The value of Q3 is %d\n',Q3)
fprintf('The value of Q is %d\n',Q)
Variables Q1, Q2, Q3, and Q are not defined in that code.
Reminder: giving a variable name followed by a number does not mean that you want to index the variable. Q1 does not mean that you want to index variable Q at the first location. And besides, you assigned to Q_2 not to Q. Use () for indexing, such as
fprintf('The value of Q1 is %d\n',Q_2(1))

Categorías

Más información sobre Simulink en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by