MATLAB Answers

- You will see updates in your activity feed.
- You may receive emails, depending on your notification preferences.

17 views (last 30 days)

Hi MATLAb guys,

I am stucking at some point and need your help.

Do you know how it is possible to find a relationship between two variables in one equations? for example y = ax+b, (a,b) are given, (x,y) are variables. I would like matlab give me x = (y-b)/a.

I have wrote the following code, but it doesn't work out well. Any idea how to fix it? Thanks in advance

Input: n_W, n_L, W_net1, m_net1, m_net2, beta

Output: W_net2

funX=@(W, W_net1, m_net1) (2*(1 - 2*W))/((1 - 2*W)*(1 + W_net1) + W*W_net1*(1 - (2*W)^m_net1));

funY=@(Z, W_net2, m_net2) (2*(1 - 2*Z))/((1 - 2*Z)*(1 + W_net2) + Z*W_net2*(1 - (2*Z)^m_net2));

funW=@(X,Y,n_W,n_L) (1 - ((1 - X)^(n_W - 1))*(1 - Y)^n_L);

funZ=@(X,Y,n_W,n_L)(1 - ((1 - X)^n_W)*(1 - Y)^(n_L - 1));

funS=@(X,Y,n_W,n_L,beta)(Y*((1 - Y)^(n_L - 1))*(1 - X)^(n_W))-beta;

fun1=@(X,Y,W,Z,n_W,n_L,W_net2, m_net2, W_net1, m_net1, beta) ([funX(W,W_net1,m_net1); funY(Z,W_net2,m_net2) ; funW(X,Y,n_W,n_L); funZ(X,Y,n_W,n_L) ; funS(X,Y,n_W,n_L,beta)]);

n_W = 3;

n_L = 4;

beta = 0.7;

m_net2 = 4;

W_net1 = 8;

m_net1= 4;

fun2=@(P) (P-fun1(P(1),P(2),P(3),P(4),n_W,n_L,beta,m_net1,W_net1,m_net2));

InitialGuess=[0;0;0;0];

fsolve(fun2,InitialGuess)

P.S. The approach is

1) knowing beta, from S ===> relationship between X and Y can be found.

2) from X and W ===> X and Y can be found numerically

3) based on Y, W_net2 can be derived

Sign in to answer this question.

John D'Errico
on 10 Apr 2019

syms a x b y

EQ = y == a*x + b;

isolate(EQ,x)

ans =

x == -(b - y)/a

Susan
on 10 Apr 2019

Thank you so much John for your reply. I have got the following error:

Undefined function 'isolate' for input arguments of type 'sym'.

Any idea?

If you have any idea to how to deal with the second part of my question, I would love to hear. Thanks in advance

John D'Errico
on 10 Apr 2019

Are you saying that you tried exactly the code I wrote, and it complained about isolate?

What version of MATLAB do you have? isolate was introduced before R2006a, so I think it unlikely that you do not have isolate. And if it recognizes that sym is a valid class, then you should have the symbolic toolbox. Do you?

If you do not have the symbolic TB, then you cannot have MATLAB do a symbolic computation as you asked. Of course, you have written this all using the fsolve as a target. So I need to know what you have. Are you really looking for a symbolic solution as you initially say, or are you looking for a numerial solution, or do you care as long as you find a solution?

I cannot easily go further to help you unless you can at least verify what you have, so I might know what to suggest.

Susan
on 10 Apr 2019

Yes,

I use R2014a and don't know why I get this error.

I tried the following code and get the same error

syms x a b c

eqn = a*x^2 + b*x + c == 0;

xSol = isolate(eqn, x)

Update: Interesting! I tried the code on R2016b and get the same error. What's wrong?

>> syms a x b y

EQ = y == a*x + b;

isolate(EQ,x)

Undefined function or variable 'isolate'.

John D'Errico
on 10 Apr 2019

Strange. R2018b here. In that quadratic equation, I get this:

syms x a b c

eqn = a*x^2 + b*x + c == 0;

xSol = isolate(eqn, x)

xSol =

x == -(b + (b^2 - 4*a*c)^(1/2))/(2*a)

Yet, doc isolate tells me this:

"Introduced before R2006a"

So it clearly has been around sufficiently long, and it seems unlikely that isolate has changed functionality.

Is the symbolc toolbox working for you otherwise? For example, does solve work?

solve(EQ,x)

ans =

-(b - y)/a

or

solve(eqn,x)

ans =

-(b + (b^2 - 4*a*c)^(1/2))/(2*a)

-(b - (b^2 - 4*a*c)^(1/2))/(2*a)

Have you got some other m-file that you named isolate?

which isolate -all

/Applications/MATLAB_R2018b.app/toolbox/symbolic/symbolic/@sym/isolate.m % sym method

Walter Roberson
on 10 Apr 2019

Is it possible that you have Maple installed on your system?

Susan
on 11 Apr 2019

@John, thanks for your reply. I appreciate your help. I don't have any other m-file that named isolate. And regarding your question on symbolc toolbax, yes it works fine.

@Walter, How much is it going to help me on solving this problem? If it is really needed, I can buy that. Thanks again.

Walter Roberson
on 11 Apr 2019

Su san: Maple offers a symbolic toolbox interface for MATLAB that differs in the available commands. When we see people with some commands working abut not other commands, sometimes it turns out that they are using the Maple interface in MATLAB instead of MATLAB's own Symbolic Toolbox. (But that turns out not to be the case this time.)

John: When I "doc isolate" I am given the documentation for syms . It takes more work to find the documentation for isolate. It turns out that it was introduced in R2017a as a MATLAB interface, which is after Su san's R2014a release.

Susan
on 13 Apr 2019

I would like to extend the above equation to the following ones. Any idea how I can implement them. Thanks in advance.

for (k = 1: K), (i = 1 : Nw), and (j = 1: Nl)

X(i, k) = 2*(1 - 2*W(i,k))/((1 - 2*W(i,k))*(1 + W_net1(i,k)) + W(i,k)*W_net1(i,k)*(1 - (2*W(i,k))^m_net1(i,k)));

W(i,k) = 1 - prod_{ii = 1, ii ~= i}^{Nw} (1 - X(ii, k) ) * prod_{j = 1}^{Nl} (1 - Y(j, k));

Y(j, k) = 2*(1 - 2*Z(j,k))/((1 - 2*Z(j,k))*(1 + W_net2(j,k)) + Z(j,k)*W_net2(j,k)*(1 - (2*Z(j,k))^m_net2(j,k)));

Z(j,k) = 1 - prod_{jj = 1, jj ~= j}^{Nl} (1 - Y(jj, k) ) * prod_{i = 1}^{Nw} (1 - X(i, k));

S(j,k) = Y(j, k) * prod_{jj = 1, jj ~= j}^{Nl} (1 - Y(jj, k) ) * prod_{i = 1}^{Nw} (1 - X(i, k));

Walter Roberson
on 13 Apr 2019

ivec = 1 : Nw;

jvec = 1 : Nl;

X = zeros(Nw, K);

Y = zeros(Nl, K);

W = zeros(Nw, K);

Z = zeros(Nl, K);

S = zeros(Nl, K);

for k = 1 : K

for i = ivec

X(i, k) = 2*(1 - 2*W(i,k))/((1 - 2*W(i,k))*(1 + W_net1(i,k)) + W(i,k)*W_net1(i,k)*(1 - (2*W(i,k))^m_net1(i,k)));

ii = setdiff(ivec, i);

tW1 = prod( 1 - X(ii, k) );

tW2 = prod( 1 - Y(jvec, k) );

W(i,k) = 1 - tW1 * tW2;

end

for j = jvec

Y(j, k) = 2*(1 - 2*Z(j,k))/((1 - 2*Z(j,k))*(1 + W_net2(j,k)) + Z(j,k)*W_net2(j,k)*(1 - (2*Z(j,k))^m_net2(j,k)));

i = ivec;

tZ1 = prod(1 - X(i, k));

jj = setdiff(jvec, j);

tZ2 = prod(1 - Y(jj, k));

tZ3 = tZ2 * tZ1;

Z(j,k) = 1 - tZ3;

S(j,k) = Y(j, k) * tZ3;

end

end

Susan
on 15 Apr 2019

Thank you so much Walter! I appreciate your help.

Could you please kindly tell me how I can define fun1 anf fun2 in this case?

Now the parameters that I am looking for are X (Nw, K), Y(Nl, K), W(Nw, K), Z(Nl, K) and W_net2 (Nl, K). In this case S (Nl,K) is given and fixed.

Thank you in advance. I appreciate your time.

Walter Roberson
on 15 Apr 2019

Could you please kindly tell me how I can define fun1 and fun2 in this case?

.... ummm, No?

Everything before used scalar X, Y, W, Z and what you would mean with arrays for those is not clear.

You could vectorize all of those previous functions, changing all of the * to .* and all of the ^ to .^ to make calculations on entire arrays at once. On the other hand your arrays are not all the same size, so some of those * should perhaps be left as * in order to invoke algebraic matrix multiplication ??

But then when you get to the fsolve stage, it is not clear what you would be solving. If you were to ask to fsolve an array of Nw x Nl x 4 values, then fsolve would be trying to find parameters that would work for all of the entries simultaneously, which probably is not what would be desired.

Susan
on 15 Apr 2019

Thanks for your reply and my appologies for not being clear.

Accualty, everything now use scalar too, as before. The only diference is now these scalar depends on k, ivec, jvec. For example, the previus fun1 and fun 2 were only for one k and the X for all Nw (Y for all Nl) was equal. Now, the value of Y for each n_l (\in jvec) would be diferent from the value of Y for n_lprime (\in jvec). The same for X. Does it make sence?

Assuming all the X (Y) are equal for ivec = 1 : Nw (jvec = 1 : Nl;), I think if we put the functions in a for loop that goes from 1 to K, we are able to find X(:,1: K), Y(:, 1:K) ,.... Am I right?

The dificult part for me is writhing Nw+Nl functions, since fun1 and fun2 are a functions of this Nw+Nl functions.

Is it clear now? Any suggestions would be greatly appreciated.

Thanks again.

Walter Roberson
on 16 Apr 2019

Yes you can use nested for loops.

You might perhaps also be able to use ndgrid() to construct the input values, and then use arrayfun() to do the fsolve()

Walter Roberson
on 16 Apr 2019

I will not get to this right away; I have something else I need to do tonight.

Sign in to comment.

Susan
on 12 Apr 2019

I have got some silly questions. Sorry in advance if they are super simple. Could anyone kindly help me to figure them out?

1) As for a variable that I am looking for, MATLAB gives me

W_net2 = 1665452806855272103430873159617883565696871742382633107303767067803714653521912534085508640394758232435502244395851440048832512/13020781553015561664280552453950494568153170161082649667490158510277680133267873702693313295722419712822602179610396291511542125

How is it possible that I simply get 0.1279 instead of this very long result? Why doesn't MATLAB do the division?

2) When I use solve(), there is a "z" in results. According to my underestanding, by using vpa() I should get rid of z, but I don't. Any idea?

Thanks in advance.

Walter Roberson
on 12 Apr 2019

1)

vpa(W_net2)

The Symbolic Toolbox is primarily for working with infinitely precise solutions, closed form when possible. By default it converts all floating point numbers (which are imprecise) to reasonably close algebraic numbers or multiples of Pi, and then works with those in order to try to find precise results.

It is possible to get the Symbolic Toolbox to work with software floating point numbers, but there are surprising limitations. Including that A + B - A might not exactly equal B which is a fundamental floating point limitation. (But the Symbolic Toolbox manipulates formulas under the assumption that it can work algebraically with association and commutation and distribution, so the answers it comes up with when you use software floating point might not be the same as what you might arrive at if you calculate by hand...)

2)

z can show up in three different contexts:

- it can show up in root(), in the form root(polynomial in z, z) or root(polynomial in z, z, number) . These stand in for the set of values, z, such that the polynomial evaluated at z becomes 0 -- the roots of the polynomial. z is a placeholder variable in this situation, that does not necessarily correspond to any of your variables, but one of your variables involves an expression that involves roots of a polynomial. root() was added in R2015b. vpa() of root() will resolve down to specific numeric values only in the case where all of the coefficients of the polynomial are numeric or standard functions applied to a numeric value (e.g., z^5 - cos(pi/7)*z + exp(2))
- it can show up in RootOf(), in the form RootOf(expression in z, z) or RootOf(expression in z, z, number). This is similar to root() but the expression does not have to be a polynomial. These stand in for the set of values, z, such that the expression evalulated at z becomes 0 -- the roots of the expression. z is a placeholder variable in this situation, that does not necessarily correspond to any of your variables, but one of your variables involves an expression that involves roots of an expression. RootOf are more likely to show up in older releases of MATLAB, but can still show up in cases where the expression is not a polynomial. vpa() of RootOf() is only certain to resolve down to specific numeric values only in cases similar to what root() can handle. Because RootOf() might involve more complicated functions such as erfc() or ilaplace or integrals, even when there are no other unbound symbolic variables, MATLAB might not be able to find a software floating point approximation, so vpa() of a RootOf() sometimes returns a RootOf() rather than a software floating point number.
- it can show up in the results of solve() outside of a root() or RootOf() . When this happens, you probably need to add the 'ReturnConditions', true option to solve() and assign the output of solve to a single variable, after which you should look at the Parameters field of the solution. If Parameters is non-empty, it will be a list of variables that the Symbolic Toolbox introduced to make the solution easier to write. For example if x and y both involve the result of calculating an expression, such as x = 3*expression^2 + 7, and y = expression/19, then instead of putting the full value of the expression into x and y, the Symbolic Toolbox might introduce a temporary variable, such as z, and then say x = 3*z^2 + 7, y = z/19 . You then need to look in the Conditions property of the solution to see how the temporary variable is defined or constrained. Typically the Symbolic Toolbox will only define a temporary variable if the variable can have multiple values, but it also sometimes defines a temporary variable for complicated conditions, such as "z has to be such that arccos(sqrt(z-2))/Pi is a positive integer and z = sin(a^3+log(b))" . Sometimes it is possible, with some work, to show that the conditions cannot be met. When a Parameter shows up in the result of solve(), then vpa() will never get rid of the parameter: you have to find solutions for the Conditions and subs() those into the solutions.

Susan
on 17 Apr 2019

Hey guys,

I have got some questions for you and really appreciate your help. I use the following code

n_L = 3; n_W = 2; W_net1 = 16; m_net1 = 6; m_net2 = 6; Beta_L = 0.25;

syms Y X

eqn = Y*((1-Y)^(nL - 1))*((1 - X)^nW) - Beta_L == 0;

solY = solve(eqn, Y, 'MaxDegree', 4); % Add 'MaxDegree', 4 to get rid of z

"solY" give me [3 * 1] which each row is a function of X and this is what I am looking for. Then I use,

funX=@(X, W, W_net1, m_net1) ( (2*(1 - 2*W)/((1 - 2*W)*(1 + W_net1) + W*W_net1*(1 - (2*W)^m_net1))) - X );

funY=@(Y, X) ( solY(1,1) - Y );

funW=@(W, X, Y, n_W, n_L) ( 1 - ((1 - X)^(n_W - 1))*((1 - Y)^n_L) - W );

fun1=@(X, Y, W, W_net1, n_L, m_net1, n_W) ([funX(X, W, W_net1, m_net1); funY(Y, X) ; funW(W, X, Y, n_W, n_L)]);

fun2=@(P) (P-fun1(P(1), P(2), P(3), W_net1, n_L, m_net1, n_W));

InitialGuess=[0;0;0];

fsolve(fun2,InitialGuess)

then I get the following error

Error using fsolve (line 269)

FSOLVE requires all values returned by functions to be of data type double.

Error in test (line 41)

x = fsolve(fun2,InitialGuess);

Anyone knows why? However, if I put the value of solY(1,1) on the funY function, I don't get any errors and the program runs perfectly.

Thanks in advance

Walter Roberson
on 17 Apr 2019

Any relationship between n_L and nL ? Any relationship between n_W and nW ?

Walter Roberson
on 17 Apr 2019

funY = @(Y, X) double( subs(solY(1,1), sym('X'), X) - Y);

Sign in to comment.

Susan
on 22 Apr 2019

Hey MATLAB experts,

Can anyone kindly help me to write down the functions in 'solve function' for below equations? Thanks in advance.

ivec = 1 : Nw;

jvec = 1 : Nl;

X = zeros(Nw, K);

Y = zeros(Nl, K);

W = zeros(Nw, K);

Z = zeros(Nl, K);

S = zeros(Nl, K);

for k = 1 : K

for i = ivec

X(i, k) = 2*(1 - 2*W(i,k))/((1 - 2*W(i,k))*(1 + W_net1(i,k)) + W(i,k)*W_net1(i,k)*(1 - (2*W(i,k))^m_net1(i,k)));

ii = setdiff(ivec, i);

tW1 = prod( 1 - X(ii, k) );

tW2 = prod( 1 - Y(jvec, k) );

W(i,k) = 1 - tW1 * tW2;

end

for j = jvec

Y(j, k) = 2*(1 - 2*Z(j,k))/((1 - 2*Z(j,k))*(1 + W_net2(j,k)) + Z(j,k)*W_net2(j,k)*(1 - (2*Z(j,k))^m_net2(j,k)));

i = ivec;

tZ1 = prod(1 - X(i, k));

jj = setdiff(jvec, j);

tZ2 = prod(1 - Y(jj, k));

tZ3 = tZ2 * tZ1;

Z(j,k) = 1 - tZ3;

S(j,k) = Y(j, k) * tZ3;

end

end

Walter Roberson
on 22 Apr 2019

You have different sizes of arrays, so I do not know what is intended to correspond to what.

Do not try to be fancy: just use nested loops to find each combination of scalar parameters and run those through your solve() code and accumulate the output in an array of appropriate size. Get it working first before you worry about vectorzing it.

Susan
on 22 Apr 2019

Thanks for your reply.

I wanted to use ndgrid() to construct the inputs and then use the arrayfun() as you suggested but unfortunately all my tries wasn't succesful.

Sure! I will use nested loop and try to get it working first. Thanks again for your reply.

Susan
on 8 May 2019

Hi Walter,

I have tried to implement this in Matlab, but still get some errors. Would you please be so kind as to take a look at my code and tell me what kind of modification I need to do?Thanks in advance!

%%

clc

clear all

K = 5;

Nw = 2;

Nl = 3;

W_net1 = 16*ones(Nw, K);

m_net1 = 6*ones(Nw, K);

W_net2 = 16*ones(Nl, K);

m_net2 = 6*ones(Nl, K);

ivec = 1 : Nw;

jvec = 1 : Nl;

X = zeros(Nw, K);

Y = zeros(Nl, K);

W = zeros(Nw, K);

Z = zeros(Nl, K);

S = zeros(Nl, K);

for k = 1 : K

for i = ivec

ii = setdiff(ivec, i);

tW1 = prod( 1 - X(ii, k) );

tW2 = prod( 1 - Y(jvec, k) );

W(i,k) = 1 - tW1 * tW2;

end

for j = jvec

i = ivec;

tZ1 = prod(1 - X(i, k));

jj = setdiff(jvec, j);

tZ2 = prod(1 - Y(jj, k));

tZ3 = tZ2 * tZ1;

Z(j,k) = 1 - tZ3;

S(j,k) = Y(j, k) * tZ3;

end

end

results = zeros(4, K);

for k = 1 : K

for i = ivec

for j = jvec

funX = @(X, W, W_net1, m_net1) (2*(1 - 2*W(i,k))/((1 - 2*W(i,k))*(1 + W_net1(i,k)) + W(i,k)*W_net1(i,k)*(1 - (2*W(i,k))^m_net1(i,k))) - X(i, k));

funY = @(Y, Z, W_net2 ,m_net2) (2*(1 - 2*Z(j,k))/((1 - 2*Z(j,k))*(1 + W_net2(j,k)) + Z(j,k)*W_net2(j,k)*(1 - (2*Z(j,k))^m_net2(j,k))) - Y(j, k));

funW = @(W, X, Y, Nw, Nl) (1 - prod( 1 - X(setdiff(1 : Nw, i), k) ) * prod( 1 - Y(1 : Nl, k) ) - W(i,k));

funZ = @(Z, X, Y, Nw, Nl) (1 - prod(1 - Y(setdiff(1 : Nl, j), k)) * prod(1 - X(1 : Nw, k)) - Z(j,k));

fun1=@(X, Y, W, Z, W_net1, m_net1, W_net2, m_net2, Nw, Nl) ([funX(X, W, W_net1, m_net1);...

funY(Y, Z, W_net2 ,m_net2) ; funW(W, X, Y, Nw, Nl); funZ(Z, X, Y, Nw, Nl)]);

fun2=@(P) (P-fun1(P(1), P(2), P(3), P(4), W_net1, m_net1, W_net2, m_net2, Nw, Nl));

InitialGuess=[0;0;0;0];

fsolve(fun2,InitialGuess)

results(:, k) = [P(1); P(2); P(3); P(4)];

end

end

end

Walter Roberson
on 8 May 2019

Walter Roberson
on 8 May 2019

Note that I am still lost as of https://www.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_694341 . I have never recovered from that.

If you want to extend some of the variables to be arrays where they used to be scalars, then take the previous code and put it into a function that accepts one scalar at a time for those values, and call that function in nested loops to generate all combinations of input values that are relevant.

However, if you are extending to arrays with values corresponding to your previous X and Y positions, then I am lost about what the new equations are.

Susan
on 9 May 2019

Thanks so much Walter for your comments.

I want to extend some of the variables to be arrays where they used to be scalars. As you suggested, took the previous code and put it into a function that accepts one scalar at a time for those values, i.e.,

function[P] = calculate_p(n_W,n_L,Wmin_LAA,m_LAA,Wmin_WiFi,m_WiFi)

funX=@(X, W, Wmin_WiFi, m_WiFi) ((2*(1 - 2*W)/((1 - 2*W)*(1 + Wmin_WiFi) + W*Wmin_WiFi*(1 - (2*W)^m_WiFi))) - X );

funY=@(Y, Z, Wmin_LAA ,m_LAA) ( (2*(1 - 2*Z)/((1 - 2*Z)*(1 + Wmin_LAA) + Z*Wmin_LAA*(1 - (2*Z)^m_LAA))) - Y );

funW=@(W, X, Y, n_W, n_L) ( 1 - ((1 - X)^(n_W - 1))*((1 - Y)^n_L) - W );

funZ=@(Z, X, Y, n_W, n_L) ( 1 - ((1 - X)^n_W)*((1 - Y)^(n_L - 1)) - Z );

fun1=@(X, Y, W, Z, Wmin_LAA, m_LAA, n_L, Wmin_WiFi, m_WiFi, n_W) ([funX(X, W, Wmin_WiFi, m_WiFi); funY(Y, Z, Wmin_LAA, m_LAA) ; funW(W, X, Y, n_W, n_L); funZ(Z, X, Y, n_W, n_L) ]);

fun2=@(P) (P-fun1(P(1), P(2), P(3), P(4), Wmin_LAA, m_LAA, n_L, Wmin_WiFi, m_WiFi, n_W));

InitialGuess=[0;0;0;0];

fsolve(fun2,InitialGuess)

end

Now, as you mentioned, I have to call that function in nested loops to generate all combinations of input values that are relevant. for all i, j, k.

I am strugling in this point. where should I call the above function in the following one? SHould I prelocate X,Y,W, Z with zeros or it's going to give me some wrong results?

ivec = 1 : Nw;

jvec = 1 : Nl;

%X = zeros(Nw, K);

%Y = zeros(Nl, K);

%W = zeros(Nw, K);

%Z = zeros(Nl, K);

%S = zeros(Nl, K);

for k = 1 : K

for i = ivec

ii = setdiff(ivec, i);

tW1 = prod( 1 - X(ii, k) );

tW2 = prod( 1 - Y(jvec, k) );

W(i,k) = 1 - tW1 * tW2;

end

for j = jvec

i = ivec;

tZ1 = prod(1 - X(i, k));

jj = setdiff(jvec, j);

tZ2 = prod(1 - Y(jj, k));

tZ3 = tZ2 * tZ1;

Z(j,k) = 1 - tZ3;

S(j,k) = Y(j, k) * tZ3;

end

end

What I want to do is for a given K, Nw, Nl, solve the following equations simualtinously and figure out what is the value of X(i, k), W(i,k), Y(j, k) , Z(j,k) for each i, j, k.

For example if K = 1, Nw = 2, Nl = 3, I need to know what are the values of X(1,1), X(2, 1), Y(1,1), Y(2,1), Y(3,1), W(1,1), W(2, 1), Z(1,1), Z(2,1), and Z(3,1).

Any help would be greatly appreciated. Thanks again

Walter Roberson
on 10 May 2019

Sorry, I do no know when I will be well enough to address this.

Sign in to comment.

Susan
on 26 Apr 2019

Hi Walter,

In one of the comments above you mentioned that "The tests I am doing effectively recover from NaN, so I know that is not the reason we cannot find a root." Did you do any specific things that the test recover from Nan?

The reason I am asking is that I am using fmincon() to solve two optimization problems (the objective function is the same but I optimize the objfun w.r.t. two different variables), and regardless of what I am selecting the initial values, I ended up with this error

"Error using sqpInterface

Objective function is undefined at initial point. Fmincon cannot continue."

Thanks in advance.

Walter Roberson
on 26 Apr 2019

I am not using fmincon(); I am using my own algorithm that includes a bunch of fminsearch() calls that I have wrapped around with some range restraints, convincing it to do things it was not designed to do. My wrappers start from an initial point and keep searching until the location is out of range or the function call returns non-finite, and when they find an undesired location, they back off to the last valid place they looked at and return that. (They do not try to "bounce off" of the invalid region though.)

When you use fmincon with sqp, your initial point will be manipulated if necessary so that it is at upper or lower bound instead of being outside of range. However, the initial point will not be manipulated to be within linear inequalities (not for sqp -- the situation is slightly different for trust-region.) It is best to start with a point that is within the bounds constraints and the linear inequalities, and preferably the linear equalities as well. It is common to not know an initial point that is within the nonlinear constraints.

When you get told the objective function is undefined at the initial point, it means you are getting nan or one of the infinities. Your objective function will be called with the input initial point (modified for upper/lower constriants if need be) so you can test that directly without worrying about the other constraints.

Susan
on 26 Apr 2019

Thanks for your reply. It totaly makes sense.

I start with a point that is within the bounds constraints and don't have any linear inequalities. However, I have a complex nonlinear constraint.

Do you know how I can replace this NaNs with a value in a cell array? I have the following and would like to replace the r with a value when it is Nan.

r = cell(sum(nL), numel(nL), numel(nW), max(nW(:)));

for k = 1 : numel(nW)

for m = 1 : nW(k)

for j = 1 : numel(nL)

for i = 1 : nL(j)

r{i + Nup*(j - 1), j, k, m} = .....

end

end

end

end

%%

r(cellfun(@(cell) any(isnan(cell(:))),r))={0}

(I have added a line after %%, is it a correct way to do so?)

Thank you so much in advance

Susan
on 26 Apr 2019

Walter Roberson
on 26 Apr 2019

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply TodayUnable to complete the action because of changes made to the page. Reload the page to see its updated state.

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web siteYou can also select a web site from the following list:

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

- América Latina (Español)
- Canada (English)
- United States (English)

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

## 27 Comments

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692447

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692447

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692452

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692452

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692481

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692481

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692545

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692545

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692807

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692807

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692826

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692826

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692830

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692830

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692835

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692835

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692853

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692853

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692862

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692862

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692867

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692867

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692870

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692870

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692891

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692891

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692898

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692898

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692906

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692906

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692907

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692907

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692917

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692917

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692921

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692921

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692958

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692958

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692964

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692964

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692977

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692977

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692981

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692981

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692988

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692988

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692989

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_692989

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_693017

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_693017

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_693344

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_693344

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_693429

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/455589-solving-system-of-n-equations#comment_693429

Sign in to comment.