MATLAB Answers


Solving system of n equations

Asked by Susan
on 10 Apr 2019
Latest activity Commented on by Susan
on 10 May 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));
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


With this newer set of equations I am able to get down to a long expression in W_net2. The expression is rather long, but turns out to be closed form, all "algebraic" numbers (it involves a cubic and a quartic.) Of the 12 combinations of expressions, 10 of them turn out to be complex valued in the range W_net2 = 0 to 1024. Two of the combinations are real valued -- but only over 0 to slightly less than 4. One of those two has a singularity in that range and so ends up not crossing 0. The other is a bit oddly shaped but does not cross zero in the range. So... there does not appear to be any real-valued solutions.
on 12 Apr 2019
Thanks for the note.
Nice work! I appreciate your time. Just a quick question, is funS founction written correctly? shouldn't be "funS=@(X,Y,n_W,n_L,beta)(Y*((1 - Y)^(n_L - 1))*(1 - X)^(n_W) -beta);"? I mean beta is outside the () in the above equation.
Still I get p2>1 and W_net2<0 even with new equations.
on 12 Apr 2019
Walter, Could you please kindly take a look at my code and tell me why I am not getting the results that you get? Thank you so much in advance.
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, Z, W_net2 ,m_net2) ( (2*(1 - 2*Z)/((1 - 2*Z)*(1 + W_net2) + Z*W_net2*(1 - (2*Z)^m_net2))) - 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 );
funS=@(X, Y, n_W, n_L, Ps) ( Y*((1 - Y)^(n_L - 1))*((1 - X)^n_W) - Ps );
fun1=@(X, Y, W, Z, W_net2, m_net2, n_L, W_net1, m_net1, n_W, Ps) ([funX(X, W, W_net1, m_net1); funY(Y, Z, W_net2, m_net2) ; funW(W, X, Y, n_W, n_L); funZ(Z, X, Y, n_W, n_L) ; funS(X, Y, n_W, n_L, Ps)]);
n_W = 3;
n_L = 4;
Ps = 0.45;
m_net2 = 4;
W_net1 = 16;
m_net1 = 6;
fun2=@(P) (P-fun1(P(1), P(2), P(3), P(4), P(5), m_net2, n_L, W_net1, m_net1, n_W, Ps));
P = sym('p', [5 1]);

Sign in to comment.

4 Answers

Answer by John D'Errico
on 10 Apr 2019

syms a x b y
EQ = y == a*x + b;
ans =
x == -(b - y)/a


I will not get to this right away; I have something else I need to do tonight.
on 16 Apr 2019
I underestand. Whenever you get time works. I really appreciate your time and help. Thanks again for everything.
on 18 Apr 2019
Walter, could you please help me to implement your suggestion whenever you have time? Thanks in advance.

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.


Any relationship between n_L and nL ? Any relationship between n_W and nW ?
funY = @(Y, X) double( subs(solY(1,1), sym('X'), X) - Y);
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;
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;


on 10 May 2019
Could you please kindly help me to solve this issue?
Many thanks in advance
Sorry, I do no know when I will be well enough to address this.
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.


on 26 Apr 2019
It just came to my attention that I should use the tools explained on this page to make my question more readable. Just wanted to THANK YOU for being SO nice to edit all of my questions without even mentioning that! Thanks again Walter!
That would work, but I would recommend against using the variable named cell due to its use as the name of the constructor functions for cell arrays.
on 26 Apr 2019
Thank you! I will use another name for this variable then! Thanks again

Sign in to comment.