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

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

I misspoke... Fy should come out to equal the weight of the aircraft which is 77008.5.
when you pass 1:5 as the second parameter to fzero then what do you intend that to mean?
At first I put just one value of Mn=3 but got an error about matrix dimensions so I tried a vector but got the error about length of 2.
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".
1:5 is not a vector with two elements .

Sign in to comment.

Answers (2)

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

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.

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

Yes, I would like to do fzero for each q individually to give an Mn value for each q. Is this what you meant when saying not to use 1/? Because it gives an "invalid use of operator" error.
fun=@(Mn) (Ptn*sqrt(gamma/R)*(/sqrt(Ttn))*Mn*Ae*((1+((gamma-1)/2)*Mn.^2)^(-(gamma+1)/(2*(gamma-1))))-m_dot);
Mn0=3;
Mn=fzero(fun,Mn0);
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
So I think what I need to do is somehow use a for loop in order to feed the fzero 1 value from the vector (q) at a time to find Mn for each value of q? Do you know how I could write this?
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=3;
Mn=fzero(fun,Mn0);
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
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

Asked:

on 7 Feb 2019

Commented:

on 7 Feb 2019

Community Treasure Hunt

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

Start Hunting!