Question

I have the following function:

function F = F(x,L,Kc1,Kc2,Kc3,Kc4)
F=[(2*L*x(1)^2)/Kc1 + L*x(1)*x(2)/Kc3 + x(1)+ (x(1)*Kc4/L)^0.5 - (2*(0.235)); (2*L*(x(2)^2))/Kc2 + x(2)+ L*x(1)*x(2)/Kc3 -2*(0.765)];
end

Here, L,Kc1,Kc2,Kc3 and Kc4 are arrays which already have 99 numerical values stored in them. Thus, I need to solve for x(1) and x(2) 99 times and store them in the arrays O2 and N2. For this, the code is:

x0=[-5 -5];
O2=zeros(1,99);
N2=zeros(1,99);
for i=1:1:99
    x=fsolve(@(x)F(x,L(i),Kc1(i),Kc2(i),Kc3(i),Kc4(i)),x0);
    O2(i)=x(1);
    N2(i)=x(2);
end

Ideally, i should have gotten the arrays O2 and N2 by solving these equations. However, when I run my program, I get the following error:

??? Error using ==> vertcat
CAT arguments dimensions are not consistent.

Error in ==> F at 2
F=[(2*L*(x(1)^2))/Kc1 + L*x(1)*x(2)/Kc3 + x(1)+ (x(1)*Kc4/L)^0.5 - (2*(0.235)); (2*L*(x(2)^2))/Kc2 +
x(2)+ L*x(1)*x(2)/Kc3 -2*(0.765)];

Error in ==> @(x)F(x,L(i),Kc1(i),Kc2(i),Kc3(i),Kc4(i))


Error in ==> fsolve at 254
            fuser = feval(funfcn{3},x,varargin{:});

Error in ==> Air_equilibriuw at 76
   x=fsolve(@(x)F(x,L(i),Kc1(i),Kc2(i),Kc3(i),Kc4(i)),x0);

Caused by:
    Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.

Does anyone know what's wrong? Thanks in advance.

Was it helpful?

Solution

Change

function F = F(x,L,Kc1,Kc2,Kc3,Kc4)
    F=[(2*L*x(1)^2)/Kc1 + L*x(1)*x(2)/Kc3 + x(1)+ (x(1)*Kc4/L)^0.5 - (2*(0.235)); (2*L*(x(2)^2))/Kc2 + x(2)+ L*x(1)*x(2)/Kc3 -2*(0.765)];
end

to

function F = F(x,L,Kc1,Kc2,Kc3,Kc4)
    F=[(2*L*x(1)^2)/Kc1 + L*x(1)*x(2)/Kc3 + x(1)+ (x(1)*Kc4/L)^0.5 - (2*(0.235)), (2*L*(x(2)^2))/Kc2 + x(2)+ L*x(1)*x(2)/Kc3 -2*(0.765)];
end

Now F will be of size [2 x 1] instead of [1 x 2], and your vertcat error will go away.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top