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[0] = lAB*cos(phi1)+lBC*cos(x[0])-xD-lDC*cos(x[1]);
f[1] = lAB*sin(phi1)+lBC*sin(x[0])-yD-lDC*sin(x[1]);
f[2] = xD+lDE*cos(x[1])-xF-lEF*cos(x[2]);
f[3] = yD+lDE*sin(x[1])-x[3]-lEF*sin(x[2]);
}
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[0]); /* df1/dx1 */
dfdx[1*n+0] = lBC*cos(x[0]); ; /* df2/dx1 */
dfdx[2*n+0] = 0; /* df3/dx1 */
dfdx[3*n+0] = 0; /* df4/dx1 */
dfdx[0*n+1] = lDC*sin(x[1]); /* df1/dx2 */
dfdx[1*n+1] = -lDC*cos(x[1]); /* df2/dx2 */
dfdx[2*n+1] = -lDE*sin(x[1]); /* df3/dx2 */
dfdx[3*n+1] = lDE*cos(x[1]); /* df4/dx2 */
dfdx[0*n+2] = 0; /* df1/dx3 */
dfdx[1*n+2] = 0; /* df2/dx3 */
dfdx[2*n+2] = lEF*sin(x[2]); /* df3/dx3 */
dfdx[3*n+2] = -lEF*cos(x[2]); /* 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[0] = lAB*cos(phi1)+lBC*cos(x[0])-xD-lDC*cos(x[1]);
f[1] = lAB*sin(phi1)+lBC*sin(x[0])-yD-lDC*sin(x[1]);
f[2] = xD+lDE*cos(x[1])-xF-lEF*cos(x[2]);
f[3] = yD+lDE*sin(x[1])-x[3]-lEF*sin(x[2]);
}
I use MS Visual Studio 2010 and IMSL version 8.
Thank you.