Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Solving non-linear simultaneous equations

Status
Not open for further replies.

duwei

Electrical
Joined
May 8, 2003
Messages
1
Location
AU
How do you solve simultaneous non-linear equations in Matlab? I figured out I need to use fsolve or lsqnonlin, but I couldn't get the right answer using fsolve.

The equations that I need to solve are in this form:
y1=1-2cos(x1)+2cos(x2)-2cos(x3)=0.5
y2=-2cos(3*x1)+2cos(3*x2)-2cos(3*x3)=0
y3=-2cos(5*x1)+2cos(5*x2)-2cos(5*x3)=0

I wrote the m-file like this

function z = abc(x)
z(1)=0.5-2*cos(x(1))+2*cos(x(2))-2*cos(x(3))
z(2)=-2*cos(3*x(1))+2*cos(3*x(2))-2*cos(3*x(3))
z(3)=-2*cos(5*x(1))+2*cos(5*x(2))-2*cos(5*x(3))

and solve it using something like this (might be a bit wronf since I don't have the code here with me)
x0=[10;20;30;40]
options=optimset('tolfun',1.0e-008);
[x,fval]=fsolve[@zbc,x0,options]

I found that by using different initai guess x0 I'll get different answer, and the solutions never converge and Matlab keeps asking me to try different x0. I have no idea what to choose, and the solutions I get never satisfy the given equations..

Can anyone help?
 
duwei

I tried your problem and didn't have any trouble. Here's my answer and code. Possibly the problem was in your call to fsolve.

x =

10.2951
21.2964
29.9719
0.0000


fval =

1.0e-012 *

-0.1032 0.2182 0.0437

function test

x0=[10;20;30;40]
options=optimset('tolfun',1.0e-008,'Display','iter');
[x,fval]=fsolve(@abc,x0,options)

function z = abc(x)
z(1)=0.5-2*cos(x(1))+2*cos(x(2))-2*cos(x(3));
z(2)=-2*cos(3*x(1))+2*cos(3*x(2))-2*cos(3*x(3));
z(3)=-2*cos(5*x(1))+2*cos(5*x(2))-2*cos(5*x(3));

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top