Path: news.mathworks.com!not-for-mail
From: "Steven_Lord" <slord@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Solving System of Nonlinear Equations
Date: Thu, 2 Dec 2010 10:00:36 -0500
Organization: MathWorks
Lines: 71
Message-ID: <id8cak$don$1@fred.mathworks.com>
References: <id6mpo$j4l$1@fred.mathworks.com> <id6o4v$fam$1@fred.mathworks.com> <id6u0t$m3$1@fred.mathworks.com> <id778k$lfl$1@fred.mathworks.com>
NNTP-Posting-Host: ah-slord.dhcp.mathworks.com
Mime-Version: 1.0
Content-Type: text/plain;
	format=flowed;
	charset="UTF-8";
	reply-type=response
Content-Transfer-Encoding: 7bit
X-Trace: fred.mathworks.com 1291302036 14103 172.31.44.225 (2 Dec 2010 15:00:36 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 2 Dec 2010 15:00:36 +0000 (UTC)
In-Reply-To: <id778k$lfl$1@fred.mathworks.com>
X-Priority: 3
X-MSMail-Priority: Normal
Importance: Normal
X-Newsreader: Microsoft Windows Live Mail 14.0.8089.726
X-MimeOLE: Produced By Microsoft MimeOLE V14.0.8089.726
Xref: news.mathworks.com comp.soft-sys.matlab:691888



"Jeremy " <guitarust@gmail.com> wrote in message 
news:id778k$lfl$1@fred.mathworks.com...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in 
> message <id6u0t$m3$1@fred.mathworks.com>...
>> "Jeremy " <guitarust@gmail.com> wrote in message 
>> <id6o4v$fam$1@fred.mathworks.com>...
>> > I've also tried to use the symbolic toolbox to solve it.
>> >
>> > a=0.1;
>> > n=4;
>> > F=[0, 1, 0, 0];
>> > J=[0, 0, 0, 0];
>> > H=[0, 0, 0, 1];
>> > L=[0, 0, 0, 0];
>> >
>> > syms a n F J H L
>> >
>> > [x(1),x(2),x(3),x(4),x(5)]=solve('(-x(1)/n)*J(4) + 
>> > (((n-3)*a)/(n*(n-1)*(n-2)))*F(2) - x(5)', ...
>> >     '(-x(1)/n)*F(4) + (((n-3)*a)/(n*(n-1)*(n-2)))*J(2) - x(2)', ...
>> >     '(-x(1)/n)*L(4) + (((n-3)*a)/(n*(n-1)*(n-2)))*H(2) - x(3)', ...
>> >     '(-x(1)/n)*H(4) + (((n-3)*a)/(n*(n-1)*(n-2)))*L(2) - x(4)', ...
>> >     '(F(1)+F(2)+F(3)+F(4)+x(5)) * ((1+2+3+4)*(n-1)*x(3) + 
>> > x(1)*(1+2+3+4)*x(4)) - (J(1)+J(2)+J(3)+J(4)+x(2)) * 
>> > ((1+2+3+4)*(n-1)*x(4) - x(1)*(1+2+3+4)*x(3)) - 
>> > (H(1)+H(2)+H(3)+H(4)+x(3)) * ((1+2+3+4)*(n-1)*x(5) + 
>> > x(1)*(1+2+3+4)*x(2)) + (L(1)+L(2)+L(3)+L(4)+x(4)) * 
>> > ((1+2+3+4)*(n-1)*x(2) - x(1)*(1+2+3+4)*x(5))');
>> >
>> > This has been running for about 20 minutes now so it's way too slow for 
>> > my purposes, if it ever even solves it.
>> - - - - - - - - I would suggest you make good use of the 'subs' function 
>> in the symbolic toolboxx.  Each of the first four equations can be used 
>> to substitute expressions in place of x(2), x(3), x(4), and x(5).  Then 
>> use 'subs' to substitute in the numerical values of your F, J, H, L, n, 
>> and a values.  Your 'solve' function is being suffocated with all the 
>> complexity of symbols present.  Finally do a simplify.  The result (if I 
>> read your equations correctly) should be a simple quadratic equation in 
>> the one unknown x(1) which is child's play to solve.  You don't need 
>> fsolve or even solve for this purpose.
>>
>> Roger Stafford
> ------------------------------------------------------------------------------------------------------------------
>
> That makes sense.  When I perform subs however, a and n are substituted 
> into the equation correctly, but anything indexed into an array (J(2), 
> F(4), etc. ) doesn't substitute.  Does subs only work with single index 
> variables?

You actually don't need SUBS here.  Just pass your equations into SOLVE not 
as strings but as the actual symbolic expressions.

x = sym(zeros(1, 5));
for k = 1:5
    x(k) = sym(sprintf('x%d', k));
end
myValues = solve((-x(1)/n)*J(4) + (((n-3)*a)/(n*(n-1)*(n-2)))*F(2) - x(5), 
...

Now myValues will be a struct array with five fields:  x1, x2, x3, x4, and 
x5 which should be the solutions to your system.

-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
To contact Technical Support use the Contact Us link on 
http://www.mathworks.com