Asked by Susan
on 10 Apr 2019

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

Answer by 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

Walter Roberson
on 16 Apr 2019

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

Susan
on 16 Apr 2019

Susan
on 18 Apr 2019

Sign in to comment.

Answer by 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 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);

Susan
on 17 Apr 2019

Thank you so much Walter!

And sorry for the mess. Yes, n_L = nL and n_W = nW

Sign in to comment.

Answer by 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

Susan
on 10 May 2019

Walter,

Could you please kindly help me to solve this issue?

Many thanks in advance

Walter Roberson
on 10 May 2019

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

Susan
on 10 May 2019

No worries. Get well soon :)

Sign in to comment.

Answer by 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

Susan
on 26 Apr 2019

Thank you! I will use another name for this variable then! Thanks again

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 27 Comments

## Walter Roberson (view profile)

## Direct link to this comment

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

## Susan (view profile)

## Direct link to this comment

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

## Walter Roberson (view profile)

## Direct link to this comment

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

## Susan (view profile)

## Direct link to this comment

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

## Susan (view profile)

## Direct link to this comment

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

## Walter Roberson (view profile)

## Direct link to this comment

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

## Walter Roberson (view profile)

## Direct link to this comment

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

## Walter Roberson (view profile)

## Direct link to this comment

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

## Susan (view profile)

## Direct link to this comment

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

## Susan (view profile)

## Direct link to this comment

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

## Walter Roberson (view profile)

## Direct link to this comment

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

## Walter Roberson (view profile)

## Direct link to this comment

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

## Susan (view profile)

## Direct link to this comment

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

## Walter Roberson (view profile)

## Direct link to this comment

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

## Susan (view profile)

## Direct link to this comment

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

## Walter Roberson (view profile)

## Direct link to this comment

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

## Walter Roberson (view profile)

## Direct link to this comment

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

## Walter Roberson (view profile)

## Direct link to this comment

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

## Susan (view profile)

## Direct link to this comment

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

## Walter Roberson (view profile)

## Direct link to this comment

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

## Susan (view profile)

## Direct link to this comment

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

## Susan (view profile)

## Direct link to this comment

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

## Walter Roberson (view profile)

## Direct link to this comment

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

## Susan (view profile)

## Direct link to this comment

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

## Walter Roberson (view profile)

## Direct link to this comment

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

## Susan (view profile)

## Direct link to this comment

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

## Susan (view profile)

## Direct link to this comment

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

Sign in to comment.