Second argument must be a scalar or vector of length 2. (fzero)
Show older comments
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
Jacey Allen
on 7 Feb 2019
Walter Roberson
on 7 Feb 2019
when you pass 1:5 as the second parameter to fzero then what do you intend that to mean?
Jacey Allen
on 7 Feb 2019
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".
Walter Roberson
on 7 Feb 2019
1:5 is not a vector with two elements .
Answers (2)
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
on 7 Feb 2019
Walter Roberson
on 7 Feb 2019
0 votes
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
Jacey Allen
on 7 Feb 2019
Bjorn Gustavsson
on 7 Feb 2019
Well then it seems you should put the definition of your function fun into the loop too. Perhaps something along:
for i1 = 1:5,
fun=@(Mn) (Ptn(i1)*sqrt(gamma(i1)/R(i1))*(/sqrt(Ttn(i1)))*Mn*Ae(i1)*((1+((gamma(i1)-1)/2)*Mn.^2)^(-(gamma(i1)+1)/(2*(gamma(i1)-1))))-m_dot(i1));
Mn(i1) = fzero(fun,Mn0(i1));
end
Here I've happily not bothered about which of your othe parameters are vectors or matrices - to decide that I leave up to you to do. Just run your script up to the line where you have the definition of fun in the first place, look at the sizes of all parameters that goes into fun and check that they have the dimensions you expect...
HTH
Jacey Allen
on 7 Feb 2019
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
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.
Categories
Find more on Image Arithmetic 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!