Second argument must be a scalar or vector of length 2. (fzero)

24 views (last 30 days)
So this is the section of my code that is having issues. Essentially I am trying to find the q value that will give an Fy=0. My error comes from solving for Mn using the fzero function saying "Second argument must be a scalar or vector of length 2." I am a MATLAB beginner, so is there a fix to the error or is there a better way to iterate through q?
%Combustor
fci=((1+((gamma-1)/2)*M2^2)/((1+gamma*M2^2)^2))*M2^2;
Ttci=Tt2;
Mchoke=1;
fchoke=((1+((gamma-1)/2)*Mchoke^2)/((1+gamma*Mchoke^2)^2))*Mchoke^2;
qchoke=((fchoke/fci)*cp*Ttci)-1;
q=1:qchoke;
Ttce=Ttci*(1+(q/(cp*Ttci)));
fce=fci*(Ttce/Ttci);
Mce=sqrt((2*fce)/(1-2*gamma*fce-(1-2*(gamma+1)*fce).^.5));
Pce=P2*((1+gamma*M2^2)/(1+gamma*Mce^2));
Tce=((Pce/(R*m_dot))*Mce*Aci*sqrt(gamma*R))^2;
Ptce=m_dot./(sqrt(gamma/R)*(1./sqrt(Ttce))*Mce*Aci*((1+((gamma-1)/2)*Mce.^2)^(-(gamma+1)/(2*(gamma-1)))));
rhoce=Pce/(R*Tce);
Vce=Mce*sqrt(gamma*R*Tce);
uce=Vce*cos(alpha);
vce=-Vce*sin(alpha);
Fxce=-(rhoce*uce*Aci*Vce-rho2*u2*Aci*V2+Pce*cos(alpha)*Aci+P2*-cos(alpha)*Aci);
Fyce=-(rhoce*vce*Aci*Vce-rho2*v2*Aci*V2+Pce*-sin(alpha)*Aci+P2*sin(alpha)*Aci);
%Nozzle
Ttn=Ttce;
Ptn=Ptce;
fun=@(Mn) (Ptn*sqrt(gamma/R)*(1/sqrt(Ttn))*Mn*Ae*((1+((gamma-1)/2)*Mn.^2)^(-(gamma+1)/(2*(gamma-1))))-m_dot);
Mn0=1:5;
Mn=fzero(fun,Mn0);
Tn=Ttn/(1+((gamma-1)/2)*Mn^2);
Pn=Ptn/((1+((gamma-1)/2)*Mn^2)^(gamma/(gamma-1)));
rhon=Pn/(R*Tn);
Vn=Mn*sqrt(gamma*R*Tn);
un=Vn*cos(alpha);
vn=Vn*-sin(alpha);
Fxn=-(rhon*un*Vn*Ae-rhoce*uce*Vce*Aci+Pn*cos(alpha)*Ae+Pce*-cos(alpha)*Aci);
Fyn=-(rhon*vn*Vn*Ae-rhoce*vce*Vce*Aci+Pn*-sin(alpha)*Ae+Pce*sin(alpha)*Aci);
%Sum of Forces
Fx=Fxi+Fxce+Fxts+Fxcb+Fxwt+Fxwb
Fy=Fyi+Fyce+Fyts+Fycb+Fywt+Fywb
  5 Comments
Torsten
Torsten on 7 Feb 2019
Your function "fun" depends on Ttn which is a vector. So either you get problems evaluating "fun" for an argument Mn or your output from "fun" is a vector which is not allowed for "fzero".

Sign in to comment.

Answers (2)

Bjorn Gustavsson
Bjorn Gustavsson on 7 Feb 2019
Either you want a range for Mn0 where you want fzero to search for a minimum, or you want to run fzero with multiple starting points. If case 1: then set
Mn0 = [1 5];
in the second:
Mn0 = 1:5,
for i0 = 1:numel(Mn0),
Mn(i0) = fzero(fun,Mn0(i0));
end
...and then you'll have to decide what to do with the possibly large number of different Mn-values for the rest of your program
HTH
  1 Comment
Jacey Allen
Jacey Allen on 7 Feb 2019
Thanks for the answer. I tried both of these solutions and came up with an error that matrix dimensions must agree. I even get that error when just guessing a scalar value. I believe it is giving me that error because the q value is a vector and is being used in the Ptn and Ttn variables of the fun equation. Is there not a way for for Mn to end up being a range coordinating with the different q values?

Sign in to comment.


Walter Roberson
Walter Roberson on 7 Feb 2019
Your q is a vector. That makes Ttce and Ptce vectors which makes Ptn and Ttn vectors. Your function does not account for that.
You should never try to calculate a pseudoinverse of a vector by using 1/ the vector and multiplying by that: if you must do least squared fitting between two vectors then use the / operation directly without the 1/ . And be careful about the size of the result .
I suspect you want to fzero for each q value individually rather than doing some dubious matrix fitting to get a single solution .
  5 Comments
Bjorn Gustavsson
Bjorn Gustavsson on 7 Feb 2019
Well, just try to plug in the correct values for Ptn, gamma, R, Ttn, Ae and m_dot for each iteration in a for-loop. Just combine your code-snippet with mine above and you should be fine. You can try to just do it for one or components first an see that it works OK.
HTH
Walter Roberson
Walter Roberson on 7 Feb 2019
It might make sense to define Ttce and Ptce as anonymous functions of q rather than in the vector q, and then to loop over the q values feeding in one at a time.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!