Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Solving System of Nonlinear Equations

Subject: Solving System of Nonlinear Equations

From: Jeremy

Date: 1 Dec, 2010 23:47:04

Message: 1 of 5

I'm trying to solve a set of equations that has a nonlinear equation in it. Using fsolve however, the solution will only converge when the independent variable is set to 0. For any other value, the solution is trying to get to zero, but the change between iterations is exponentially getting smaller, so it will never reach it. If you don't know what I'm talking about, try running my code.

Note: a is the independent variable. Try changing it to anything besides 0.

Main Program:

[Q]clear all; close all; clc;


x0=[0.1, 0.1, 0.1, 0.1, 0.1];
options=optimset('Display','iter','MaxFunEvals',40000,'Maxiter',5000); % Option to display output
[x] = fsolve(@myfun,x0,options) % Call solver
% Note: x(1)=B, x(2)=J5, x(3)=H5, x(4)=L5, x(5)=F5[/Q]

Function File:

[Q]function SOE = myfun(x)
a=0;
n=4;
F=[0, 1, 0, 0];
J=[0, 0, 0, 0];
H=[0, 0, 0, 1];
L=[0, 0, 0, 0];


SOE = [(-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))];[/Q]


What i'm asking then is if there's another way i could go about obtaining the solution, preferrably quickly? Any ideas would be appreciated.

Subject: Solving System of Nonlinear Equations

From: Jeremy

Date: 2 Dec, 2010 00:10:08

Message: 2 of 5

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.

Subject: Solving System of Nonlinear Equations

From: Roger Stafford

Date: 2 Dec, 2010 01:50:21

Message: 3 of 5

"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

Subject: Solving System of Nonlinear Equations

From: Jeremy

Date: 2 Dec, 2010 04:28:04

Message: 4 of 5

"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?

Subject: Solving System of Nonlinear Equations

From: Steven_Lord

Date: 2 Dec, 2010 15:00:36

Message: 5 of 5



"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

Tags for this Thread

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.

Contact us