# Thread Subject: Solving three equations in three unknowns

 Subject: Solving three equations in three unknowns From: Bilal Date: 1 Aug, 2012 17:28:52 Message: 1 of 4 I have three equations with three unknowns, p, gamma1, gamma2. All three unknowns are probabilities each of them in interval [0 1]. Equations are Eq (1): p = p = 1-((1-gamma1)^(n_C-1))*(1-gamma2)^n_H; Eq (2): gamma1 = ((p^X - p^(m+1)))/ ((1-p))*((1-p^(m+1))/((1-p))); Eq (3): gamma2 = (1/2) * ((1 - p^X) / ((1 - p))); All other variables are known. I want to find out gamma1 and gamma2. I wrote code for this, but apart from being slow it doesn't give very accurate answer. I am giving below the code I wrote but I want to know how would it be possible to solve these equations using fsolve or any other built-in function in matlab. Thanks in advance. -- Bilal function [gamma1,gamma2] = FUNCgamma(n_C, n_H) precision = 1000; %the degree of analysis accuracy          for(i=1:precision)         gamma1 = i/precision;                  for(j=1:precision)             gamma2 = j/precision;                           p = 1-((1-gamma1)^(n_C-1))*(1-gamma2)^n_H;              mygamma1 = ((p^X - p^(m+1)))/ ((1-p))*((1-p^(m+1))/((1-p)));              mygamma2 = (1/2) * ((1 - p^X) / ((1 - p)));              if(abs(gamma2-mygamma2)<0.001)                  break;              end        end % end of loop j, gamma2 found                    if(abs(gamma2-myg2)<0.001)                   if(abs(gamma1-myg1)<0.001)                   break;                   end           end     end % end of loop j, both gamma2 and gamma1 found,
 Subject: Solving three equations in three unknowns From: Alan_Weiss Date: 1 Aug, 2012 18:09:19 Message: 2 of 4 On 8/1/2012 1:28 PM, Bilal wrote: > I have three equations with three unknowns, p, gamma1, gamma2. All three unknowns are probabilities each of them in interval [0 1]. Equations are > > Eq (1): p = p = 1-((1-gamma1)^(n_C-1))*(1-gamma2)^n_H; > Eq (2): gamma1 = ((p^X - p^(m+1)))/ ((1-p))*((1-p^(m+1))/((1-p))); > Eq (3): gamma2 = (1/2) * ((1 - p^X) / ((1 - p))); > > All other variables are known. I want to find out gamma1 and gamma2. I wrote code for this, but apart from being slow it doesn't give very accurate answer. I am giving below the code I wrote but I want to know how would it be possible to solve these equations using fsolve or any other built-in function in matlab. Thanks in advance. > -- > Bilal > *snip* You could write this as a problem in one variable with one unknown, p. Simply replace gamma1 and gamma2 in equation 1 with their definitions in equations 2 and 3. gamma1 = @(p)((p^X - p^(m+1)))/ ((1-p))*((1-p^(m+1))/((1-p))); gamma2 = @(p)(1/2) * ((1 - p^X) / ((1 - p))); fun = @(p) 1-((1-gamma1(p))^(n_C-1))*(1-gamma2(p))^n_H - p; psolution = fzero(fun,[0 1]) or, because there can be problems at the endpoints, psolution = fzero(fun,[0.01 .99]) Alan Weiss MATLAB mathematical toolbox documentation
 Subject: Solving three equations in three unknowns From: Bilal Date: 2 Aug, 2012 17:13:54 Message: 3 of 4 Thanks Alan, your solution is faster and better than the one I wrote. Basically I want to draw "n_C vs gamma" graph where n_C increases from 1 to 20 one by one. however the 'gamma' fluctuates in my solution where in the original solution, graph is (someone has already drawn these graphs) smooth. Do you have any idea what could be the reason? Also, I didn't mention the original equations for Eq(1) and Eq(2) above as they are a bit lengthy. Please let me know if there is any way these fluctuations could be eliminated. cheers -- Bilal On Thursday, August 2, 2012 3:09:19 AM UTC+9, Alan_Weiss wrote: > On 8/1/2012 1:28 PM, Bilal wrote: > > > I have three equations with three unknowns, p, gamma1, gamma2. All three unknowns are probabilities each of them in interval [0 1]. Equations are > > > > > > Eq (1): p = p = 1-((1-gamma1)^(n_C-1))*(1-gamma2)^n_H; > > > Eq (2): gamma1 = ((p^X - p^(m+1)))/ ((1-p))*((1-p^(m+1))/((1-p))); > > > Eq (3): gamma2 = (1/2) * ((1 - p^X) / ((1 - p))); > > > > > > All other variables are known. I want to find out gamma1 and gamma2. I wrote code for this, but apart from being slow it doesn't give very accurate answer. I am giving below the code I wrote but I want to know how would it be possible to solve these equations using fsolve or any other built-in function in matlab. Thanks in advance. > > > -- > > > Bilal > > > > > *snip* > > > > You could write this as a problem in one variable with one unknown, p. > > Simply replace gamma1 and gamma2 in equation 1 with their definitions in > > equations 2 and 3. > > > > gamma1 = @(p)((p^X - p^(m+1)))/ ((1-p))*((1-p^(m+1))/((1-p))); > > > > gamma2 = @(p)(1/2) * ((1 - p^X) / ((1 - p))); > > > > fun = @(p) 1-((1-gamma1(p))^(n_C-1))*(1-gamma2(p))^n_H - p; > > psolution = fzero(fun,[0 1]) or, because there can be problems at the > > endpoints, > > psolution = fzero(fun,[0.01 .99]) > > > > Alan Weiss > > MATLAB mathematical toolbox documentation
 Subject: Solving three equations in three unknowns From: Alan_Weiss Date: 2 Aug, 2012 18:04:08 Message: 4 of 4 On 8/2/2012 1:13 PM, Bilal wrote: > Thanks Alan, your solution is faster and better than the one I wrote. > Basically I want to draw "n_C vs gamma" graph where n_C increases from 1 to 20 one by one. however the 'gamma' fluctuates in my solution where in the original solution, graph is (someone has already drawn these graphs) smooth. Do you have any idea what could be the reason? > Also, I didn't mention the original equations for Eq(1) and Eq(2) above as they are a bit lengthy. > Please let me know if there is any way these fluctuations could be eliminated. > cheers > -- > Bilal > > On Thursday, August 2, 2012 3:09:19 AM UTC+9, Alan_Weiss wrote: >> On 8/1/2012 1:28 PM, Bilal wrote: >> >>> I have three equations with three unknowns, p, gamma1, gamma2. All three unknowns are probabilities each of them in interval [0 1]. Equations are >>> Eq (1): p = p = 1-((1-gamma1)^(n_C-1))*(1-gamma2)^n_H; >>> Eq (2): gamma1 = ((p^X - p^(m+1)))/ ((1-p))*((1-p^(m+1))/((1-p))); >>> Eq (3): gamma2 = (1/2) * ((1 - p^X) / ((1 - p))); >>> All other variables are known. I want to find out gamma1 and gamma2. I wrote code for this, but apart from being slow it doesn't give very accurate answer. I am giving below the code I wrote but I want to know how would it be possible to solve these equations using fsolve or any other built-in function in matlab. Thanks in advance. >>> -- >>> Bilal >> *snip* >> >> >> >> You could write this as a problem in one variable with one unknown, p. >> >> Simply replace gamma1 and gamma2 in equation 1 with their definitions in >> >> equations 2 and 3. >> >> >> >> gamma1 = @(p)((p^X - p^(m+1)))/ ((1-p))*((1-p^(m+1))/((1-p))); >> >> >> >> gamma2 = @(p)(1/2) * ((1 - p^X) / ((1 - p))); >> >> >> >> fun = @(p) 1-((1-gamma1(p))^(n_C-1))*(1-gamma2(p))^n_H - p; >> >> psolution = fzero(fun,[0 1]) or, because there can be problems at the >> >> endpoints, >> >> psolution = fzero(fun,[0.01 .99]) >> >> >> >> Alan Weiss >> >> MATLAB mathematical toolbox documentation I don't know what might be going on, unless your function has more than one root. In this case, fzero might choose the wrong root. One way to try to avoid these undesirable fluctuations could be to start the search for a new root right at the solution for the previous iteration. Good luck, Alan Weiss MATLAB mathematical toolbox documentation

Separated by commas
Ex.: root locus, bode

### What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.