# Thread: Problem with msl_f_zeros_sys_eqn

1. ## Problem with msl_f_zeros_sys_eqn

Hello. My task is to solve the system of nonlinear equations:
1.JPG

In this system I need to find roots phi2, phi3, phi4 and yF;
The value of angle phi1 is known and changes from 0 to 360 degrees by step 1.

I know all the values of lAB, LBC,.....lEF, xD, yD, and also an initial guess:
2.JPG

Here is my code:

Code:
```#include "stdafx.h"
#include <imsl.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
#define N  4
#define Pi  3.141592654

float phi1 =0.0;
float lAB = 0.05;
float lBC = 0.32;
float lDC = 0.3;
float lCE = 0.12;
float lDE = lDC+lCE;
float lEF = 0.11;
float xF = -0.25;
float xD = 0.16;
float yD = 0.29;

void            fcn(int, float[], float[]);

int main()
{   int         maxitn = 100;
float       *x, err_rel = 0.0001, fnorm;
float       xguess[N] = {0.2163449647, 2.311943298, 1.884823320, 0.9802707203};

for(int i=1;i<=360;i++){

phi1 = (i*Pi)/180.0;

x = imsl_f_zeros_sys_eqn(fcn, N,
IMSL_ERR_REL, err_rel,
IMSL_MAX_ITN, maxitn,
IMSL_XGUESS, xguess,
IMSL_FNORM, &fnorm,
0);
imsl_f_write_matrix("The solution to the system is", 1, N, x, 0);

}
system("pause");
}

void fcn(int n, float f[], float x[])
{
f = lAB*cos(phi1)+lBC*cos(x)-xD-lDC*cos(x);
f = lAB*sin(phi1)+lBC*sin(x)-yD-lDC*sin(x);
f = xD+lDE*cos(x)-xF-lEF*cos(x);
f = yD+lDE*sin(x)-x-lEF*sin(x);
}```
But iteration process gives wrong values of roots.
3.jpg
I've solved this problem in Maple and got first 10 root:
4.JPG

Why IMSL function does not work properly, am I doing something wrong?

I also tried to supply Jacobian, but still doesn't work.
Code:
```#include "stdafx.h"
#include <imsl.h>
#include <stdio.h>
#include <math.h>
#include <iostream>

#define N       4
float phi1 =0.0;
float lAB = 0.05;
float lBC = 0.32;
float lDC = 0.3;
float lCE = 0.12;
float lDE = lDC+lCE;
float lEF = 0.11;
float xF = -0.25;
float xD = 0.16;
float yD = 0.29;

void jacob( int n, float t, float x[], float p, float dfdx[] )
{
dfdx[0*n+0] = -lBC*sin(x);   /* df1/dx1 */

dfdx[1*n+0] =  lBC*cos(x); ;   /* df2/dx1 */

dfdx[2*n+0] = 0;   /* df3/dx1 */

dfdx[3*n+0] = 0;   /* df4/dx1 */

dfdx[0*n+1] = lDC*sin(x);   /* df1/dx2 */

dfdx[1*n+1] = -lDC*cos(x);   /* df2/dx2 */

dfdx[2*n+1] = -lDE*sin(x);  /* df3/dx2 */

dfdx[3*n+1] = lDE*cos(x);  /* df4/dx2 */

dfdx[0*n+2] = 0;   /* df1/dx3 */

dfdx[1*n+2] = 0;   /* df2/dx3 */

dfdx[2*n+2] = lEF*sin(x);   /* df3/dx3 */

dfdx[3*n+2] = -lEF*cos(x);   /* df4/dx3 */

dfdx[0*n+3] = 0;   /* df1/dYF */

dfdx[1*n+3] = 0;   /* df2/dyF */

dfdx[2*n+3] = 0;   /* df3/dyF */

dfdx[3*n+3] = -1;   /* df4/dyF */

}

void            fcn(int, float[], float[]);

int main()
{   int         maxitn = 100;
float       *x, err_rel = 0.0000001, fnorm;
float       xguess[N] = {0.2163449647, 2.311943298, 1.884823320, 0.9802707203};

for(int i=0;i<=10;i++){

phi1 = (i*3.141592654)/180.0;

x = imsl_f_zeros_sys_eqn(fcn, N,
IMSL_ERR_REL, err_rel,
IMSL_MAX_ITN, maxitn,
IMSL_XGUESS, xguess,
IMSL_JACOBIAN, jacob,
IMSL_FNORM, &fnorm,
0);
imsl_f_write_matrix("The solution to the system is", 1, N, x, 0);

}

system("pause");
}

void fcn(int n, float f[], float x[])
{
f = lAB*cos(phi1)+lBC*cos(x)-xD-lDC*cos(x);
f = lAB*sin(phi1)+lBC*sin(x)-yD-lDC*sin(x);
f = xD+lDE*cos(x)-xF-lEF*cos(x);
f = yD+lDE*sin(x)-x-lEF*sin(x);
}```
I use MS Visual Studio 2010 and IMSL version 8.
Thank you.  Reply With Quote

2. Hello. Thank you for providing a detailed description of the problem.
The issue is a coding mistake in your program.

Your user-supplied function has the last two parameters reversed. That is, changing

void fcn(int n, float f[], float x[])

to

void fcn(int n, float x[], float f[])

should fix things. I have done some simple tests to confirm this but you should verify the fix yourself as well.

hth
-w  Reply With Quote

3. ##  Originally Posted by omega Hello. Thank you for providing a detailed description of the problem.
The issue is a coding mistake in your program.

Your user-supplied function has the last two parameters reversed. That is, changing

void fcn(int n, float f[], float x[])

to

void fcn(int n, float x[], float f[])

should fix things. I have done some simple tests to confirm this but you should verify the fix yourself as well.

hth
-w
Oh, Omega, thank you. You helped me a lot!  Reply With Quote

4. Hello everyone, I'v got another issue.
Is it possible to use the IMSL library's functions in Visual Studio's Windows Forms Applications?
It would be helpful, if anybody show me an example WFA project, which is implement calculations using IMSL. The example of the function zeros_sys_eqn is more appreciated.
Thank you.  Reply With Quote

5. Although not directly applicable, the following paper might give you some ideas

Scripting Environments and the IMSL® Libraries,

http://www.roguewave.com/DesktopModu...Id=0&TabId=607  Reply With Quote

#### Tags for this Thread

equation, imsl, imsl_f_zeros_sys_eqn, nonlinear, system #### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•