# Solving 3 Equations in one program

• (2 Pages)
• 1
• 2

## 18 Replies - 3557 Views - Last Post: 16 April 2007 - 07:09 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=26457&amp;s=7e7bdf5eccbf959a3cfd768e5afa1126&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Cobra3232

Reputation: -1
• Posts: 12
• Joined: 10-April 07

# Solving 3 Equations in one program

Posted 12 April 2007 - 06:13 PM

I am having a lot of trouble figuring out the problem with my progam here. I am solving 3 equations and each equation has a different unknown. Once you find the unknown to the first equation, you must use that value for the first unknown as part of the second equation to solve for the second unknown. Heres my program. I successfully solved for the first variable, but once i tried to add in the second equation i got an error. The main thing to remember is i have to do this all in one program using the secant method. Heres my program. I have placed a happy face in front of the part the worked:

```
#include <stdio.h>
#include <math.h>

double f(double T2)	   /* Double T2 is used for improved precision of T2 */

{
double q = 750;	   /* Rate at which the blackened surface absorbs the solar energy */
double T3 = 303.15;	/* Temperature in Kelvin at which the outside surface of the glass loses energy to the environment */
double h = 20;		/* Unidentified constant given */
double E = 0.9;	   /* The epsilon constant given */
double S = 5.669E-8;  /* Sigma constant given */
double dX = .075;	 /* Delta X converted from centimeters to meters */
double k = 0.042;	 /* Effective air thermal conductivity */

return h*(T2-T3)+(E*S)*((T2*T2*T2*T2)-(T3*T3*T3*T3))-q;  /* Non-linear equation in which we are solving for T2 */
}

double SecantMethod(double T2n_1, double T2n, double e, int m)
{
int n;
double d;
for (n = 1; n <= m; n++)
{
d = (T2n - T2n_1) / (f(T2n) - f(T2n_1)) * f(T2n);
if (fabs(d) < e)
return T2n;
T2n_1 = T2n;
T2n = T2n - d;
}
return T2n;
}

int main(void)
{
printf("%0.10f\n", SecantMethod(0, 1, 5E-11, 1000));
return 0;
}
:D

double f(double J2O)	   /* Double J2O is used for improved precision of J2O */
{
double T2n;
double q = 750;	   /* Rate at which the blackened surface absorbs the solar energy */
double T3 = 303.15;	/* Temperature in Kelvin at which the outside surface of the glass loses energy to the environment */
double h = 20;		/* Unidentified constant given */
double E = 0.9;	   /* The epsilon constant given */
double S = 5.669E-8;  /* Sigma constant given */
double dX = .075;	 /* Delta X converted from centimeters to meters */
double k = 0.042;	 /* Effective air thermal conductivity */

return ((E/(1-E))*(S*(T2n*T2n*T2n*T2n)-J2O))-((E*S)*((T2n*T2n*T2n*T2n)-(T3*T3*T3*T3)));
}
double SecantMethod(double J2On_1, double J2On, double e, int m)
{
int n;
double d;
for (n = 1; n <= m; n++)
{
d = (J2On - J2On_1) / (f(J2On) - f(J2On_1)) * f(J2On);
if (fabs(d) < e)
return J2On;
J2On_1 = J2On;
J2On = J2On - d;
}
return J2On;
}
}

```

I had used the int main to solve for the frst function, but i know if i add more functions, the int main must be moved. I need to find J20 for this second equation which contains T2 which was found in the original equation. Can anybody help me?

Is This A Good Question/Topic? 0

## Replies To: Solving 3 Equations in one program

### #2 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: Solving 3 Equations in one program

Posted 13 April 2007 - 12:07 AM

I bet that you wish your problem could be done in Mathematica and not C. C does not do symbolic manipulation well, and even if it did, it can not directly evaluate a manipulated expression.

Well, that asside... using more discriptive variable names and function names would be nice.

Second of all lets look at the format for a C/C++ program

```//put your includes first...
#include <stdio.h>
#include <math.h>

//Here you would declare any special typedefs, or structs etc...

//Here you would declare any global/external data...

//Then you should Declare all of your funcitons
//Note that each function must have its own "profile"
//Profile: double SecantMethod(double, double, double, int);
// note that there are no vaiable names in the profile... so both

double SecantMethod1(double J2On_1, double J2On, double e, int m);
double SecantMethod2(double T2n_1, double T2n, double e, int m);
double f1(double T2);
double f2(double J2O);

//Next comes main...
int main(void)
{
printf("%0.10f\n", SecantMethod1(0, 1, 5E-11, 1000));
return 0;
}

double f1(double T2)	   // Double T2 is used for improved precision of T2

{
double q = 750;	   // Rate at which the blackened surface absorbs the solar energy
double T3 = 303.15;	// Temperature in Kelvin at which the outside surface of the glass loses energy to the environment
double h = 20;		// Unidentified constant given
double E = 0.9;	   // The epsilon constant given
double S = 5.669E-8;  // Sigma constant given
double dX = .075;	 // Delta X converted from centimeters to meters
double k = 0.042;	 // Effective air thermal conductivity

return h*(T2-T3)+(E*S)*((T2*T2*T2*T2)-(T3*T3*T3*T3))-q;  // Non-linear equation in which we are solving for T2
}

double SecantMethod1(double T2n_1, double T2n, double e, int m)
{
int n;
double d;
for (n = 1; n <= m; n++)
{
d = (T2n - T2n_1) / (f1(T2n) - f1(T2n_1)) * f1(T2n);
if (fabs(d) < e)
return T2n;
T2n_1 = T2n;
T2n = T2n - d;
}
return T2n;
}

double f2(double J2O)	   // Double J2O is used for improved precision of J2O
{
double T2n;  //Is not defined!!!
double q = 750;	   // Rate at which the blackened surface absorbs the solar energy
double T3 = 303.15;	// Temperature in Kelvin at which the outside surface of the glass loses energy to the environment
double h = 20;		// Unidentified constant given
double E = 0.9;	   // The epsilon constant given
double S = 5.669E-8;  // Sigma constant given
double dX = .075;	 // Delta X converted from centimeters to meters
double k = 0.042;	 // Effective air thermal conductivity
return ((E/(1-E))*(S*(T2n*T2n*T2n*T2n)-J2O))-((E*S)*((T2n*T2n*T2n*T2n)-(T3*T3*T3*T3)));
}

double SecantMethod2(double J2On_1, double J2On, double e, int m)
{
int n;
double d;
for (n = 1; n <= m; n++)
{
d = (J2On - J2On_1) / (f2(J2On) - f2(J2On_1)) * f2(J2On);
if (fabs(d) < e)
return J2On;
J2On_1 = J2On;
J2On = J2On - d;
}
return J2On;
}
```

The above code compiles, and the only real problem is in f2() becuase the variable T2n is not defined. I *think* that you will want to pass this as a parameter to f2(double J2O, double T2n) since it would be calculated by another function.

### #3 Cobra3232

Reputation: -1
• Posts: 12
• Joined: 10-April 07

## Re: Solving 3 Equations in one program

Posted 13 April 2007 - 10:08 AM

I tried tickering with it, but i still get one error that states:

fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?

Im not sure what that means, but i tried adding:

double f2(double J2O, double T2n) // Double J2O is used for improved precision of J2O

double f2(double J2O) // Double J2O is used for improved precision of J2O

but still no luck. What you did really helped me in terms of using multiple equations though so thanks a lot. I'd still like to know what is wrong or what the one error is. I tried adding that #include stdafx.h, but that didnt do anything but create another error.

If anybody has any ideas, please let me know. Im still not that good at this stuff.

NickDMax, on 13 Apr, 2007 - 12:07 AM, said:

I bet that you wish your problem could be done in Mathematica and not C. C does not do symbolic manipulation well, and even if it did, it can not directly evaluate a manipulated expression.

Well, that asside... using more discriptive variable names and function names would be nice.

Second of all lets look at the format for a C/C++ program

```//put your includes first...
#include <stdio.h>
#include <math.h>

//Here you would declare any special typedefs, or structs etc...

//Here you would declare any global/external data...

//Then you should Declare all of your funcitons
//Note that each function must have its own "profile"
//Profile: double SecantMethod(double, double, double, int);
// note that there are no vaiable names in the profile... so both

double SecantMethod1(double J2On_1, double J2On, double e, int m);
double SecantMethod2(double T2n_1, double T2n, double e, int m);
double f1(double T2);
double f2(double J2O);

//Next comes main...
int main(void)
{
printf("%0.10f\n", SecantMethod1(0, 1, 5E-11, 1000));
return 0;
}

double f1(double T2)	   // Double T2 is used for improved precision of T2

{
double q = 750;	   // Rate at which the blackened surface absorbs the solar energy
double T3 = 303.15;	// Temperature in Kelvin at which the outside surface of the glass loses energy to the environment
double h = 20;		// Unidentified constant given
double E = 0.9;	   // The epsilon constant given
double S = 5.669E-8;  // Sigma constant given
double dX = .075;	 // Delta X converted from centimeters to meters
double k = 0.042;	 // Effective air thermal conductivity

return h*(T2-T3)+(E*S)*((T2*T2*T2*T2)-(T3*T3*T3*T3))-q;  // Non-linear equation in which we are solving for T2
}

double SecantMethod1(double T2n_1, double T2n, double e, int m)
{
int n;
double d;
for (n = 1; n <= m; n++)
{
d = (T2n - T2n_1) / (f1(T2n) - f1(T2n_1)) * f1(T2n);
if (fabs(d) < e)
return T2n;
T2n_1 = T2n;
T2n = T2n - d;
}
return T2n;
}

double f2(double J2O)	   // Double J2O is used for improved precision of J2O
{
double T2n;  //Is not defined!!!
double q = 750;	   // Rate at which the blackened surface absorbs the solar energy
double T3 = 303.15;	// Temperature in Kelvin at which the outside surface of the glass loses energy to the environment
double h = 20;		// Unidentified constant given
double E = 0.9;	   // The epsilon constant given
double S = 5.669E-8;  // Sigma constant given
double dX = .075;	 // Delta X converted from centimeters to meters
double k = 0.042;	 // Effective air thermal conductivity
return ((E/(1-E))*(S*(T2n*T2n*T2n*T2n)-J2O))-((E*S)*((T2n*T2n*T2n*T2n)-(T3*T3*T3*T3)));
}

double SecantMethod2(double J2On_1, double J2On, double e, int m)
{
int n;
double d;
for (n = 1; n <= m; n++)
{
d = (J2On - J2On_1) / (f2(J2On) - f2(J2On_1)) * f2(J2On);
if (fabs(d) < e)
return J2On;
J2On_1 = J2On;
J2On = J2On - d;
}
return J2On;
}
```

The above code compiles, and the only real problem is in f2() becuase the variable T2n is not defined. I *think* that you will want to pass this as a parameter to f2(double J2O, double T2n) since it would be calculated by another function.

### #4 dizzywiggle

Reputation: 2
• Posts: 9
• Joined: 13-April 07

## Re: Solving 3 Equations in one program

Posted 13 April 2007 - 10:33 AM

Cobra3232,

Just turn off precompiled headers in VC++. Right click on the project, select Properties->C/C++->Precompiled Headers and set "Create/Use Precompiled Header" to "Not Using Precompiled Headers". That should fix your problem.

-Shane

### #5 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: Solving 3 Equations in one program

Posted 13 April 2007 - 10:45 AM

Quote

unexpected end of file..
this ussualy means that you opened a { , ", ', (, [ etc and forgot to close it... ussualy this is the {

This might not be the problem since it is talking about precompiled headers... don't know why it would... The code I gave should compile fine (it did on VC++6)

### #6 Cobra3232

Reputation: -1
• Posts: 12
• Joined: 10-April 07

## Re: Solving 3 Equations in one program

Posted 13 April 2007 - 11:47 AM

Im using microsoft visual C++ 2005 if that makes a difference but i dont think it would...

### #7 Cobra3232

Reputation: -1
• Posts: 12
• Joined: 10-April 07

## Re: Solving 3 Equations in one program

Posted 13 April 2007 - 12:00 PM

I did try to turn off those precompiled headersx and it ended up giving me errors for even more things. It said there were errors with f1, secantmethod1, f2, and secantmethod2. Im not sure what changing that did to the program.

### #8 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: Solving 3 Equations in one program

Posted 13 April 2007 - 01:40 PM

... I would try making a new project... I don't have 2005 so I can't look at it.

before you did that though:

The other thing you could try is adding #include "stdafx.h" to the begining of the project. Then right lick on "stdafx.h" and ask it to open that file and make sure there is nothing off in it (and that it exits).

What were the errors did it give you?

### #9 Cobra3232

Reputation: -1
• Posts: 12
• Joined: 10-April 07

## Re: Solving 3 Equations in one program

Posted 13 April 2007 - 04:43 PM

Ok...so before i did what you just proposed, i ended up changing some things to tweak it and try something new. I ended programming it successfully (it compiled), however, when i run it, the second value comes up as: -1.#IND0000000. I tried messing with that equation and it didnt do anything which tells me it isnt the equation. When i tried the stdafx.h thing you told me about, it said no such file existed. Im not sure what that means, but you did say to make sure it exists. Heres my new code....tell me if there is something wrong with it:
```#include <stdio.h>
#include <math.h>

double f1(double T2)	   /* Double T2 is used for improved precision of T2 */

{
double q = 750;	   /* Rate at which the blackened surface absorbs the solar energy */
double T3 = 303.15;	/* Temperature in Kelvin at which the outside surface of the glass loses energy to the environment */
double h = 20;		/* Unidentified constant given */
double E = 0.9;	   /* The epsilon constant given */
double S = 5.669E-8;  /* Sigma constant given */
double dX = .075;	 /* Delta X converted from centimeters to meters */
double k = 0.042;	 /* Effective air thermal conductivity */

return h*(T2-T3)+(E*S)*((T2*T2*T2*T2)-(T3*T3*T3*T3))-q;  /* Non-linear equation in which we are solving for T2 */
}

double SecantMethod1(double T2n_1, double T2n, double e, int m)
{
int n;
double d;
for (n = 1; n <= m; n++)
{
d = (T2n - T2n_1) / (f1(T2n) - f1(T2n_1)) * f1(T2n);
if (fabs(d) < e)
return T2n;
T2n_1 = T2n;
T2n = T2n - d;
}
return T2n;
}

double f2(double J2O, double T2n)
{
//Is not defined!!!
double q = 750;	   // Rate at which the blackened surface absorbs the solar energy
double T3 = 303.15;	// Temperature in Kelvin at which the outside surface of the glass loses energy to the environment
double h = 20;		// Unidentified constant given
double E = 0.9;	   // The epsilon constant given
double S = 5.669E-8;  // Sigma constant given
double dX = .075;	 // Delta X converted from centimeters to meters
double k = 0.042;	 // Effective air thermal conductivity

return (((E/(1-E))*((S*T2n*T2n*T2n*T2n)-J2O)))-(E*S*T2n*T2n*T2n*T2n)-(T3*T3*T3*T3);
}

double SecantMethod2(double J2On_1, double J2On, double e, int m)
{
int n;
double d;
for (n = 1; n <= m; n++)
{
d = (J2On - J2On_1) / ((J2On - J2On_1) * J2On);
if (fabs(d) < e)
return J2On;
J2On_1 = J2On;
J2On = J2On - d;
}
return J2On;
}

int main()
{
printf("%0.10f\n", SecantMethod1(0, 1, 5E-11, 1000));

printf("%0.15f\n", SecantMethod2(0, 1, 5E-11, 1000));

return 0;
}
```

### #10 Cobra3232

Reputation: -1
• Posts: 12
• Joined: 10-April 07

## Re: Solving 3 Equations in one program

Posted 14 April 2007 - 10:44 AM

I guess a better question might be

When an asnwer comes up as "-1.#IND0000000", what does that mean?

### #11 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: Solving 3 Equations in one program

Posted 14 April 2007 - 01:14 PM

well "-1.#IND0000000" is an indefinite. Ussualy this only comes up when using inverse trig functions. From what I can tell this comes out of a FPU exception:

Quote

The FPU produces indefinite values as responses to masked floating-point exceptions.
from here

Basicly I think this means that somewhere you did a division by zero, or you caused an overflow.

### #12 Cobra3232

Reputation: -1
• Posts: 12
• Joined: 10-April 07

## Re: Solving 3 Equations in one program

Posted 14 April 2007 - 02:35 PM

Well then do you see something that stands out in my program as an error cause i thought it looked good. It even compiled with no errors or warnings too. I just dont know what to do.

### #13 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: Solving 3 Equations in one program

Posted 14 April 2007 - 06:29 PM

the problem is that you do division by zero (my computer gives me -1#INF not and -1#IND). This is caused by two lines:

d = (J2On - J2On_1) / ((J2On - J2On_1) * J2On); Harmless by itself, but after the first iteration you have d=1, and you assign J2On = J2On - d; and 1-1=0 so the next calculation for d looks like
d=(0-1)/((0-1)*0); which is -1#INF (or undefined depending on your model).

My guess is that the function does not converge on the intervel (0, 1).

Now lets see. I sold back my good Numerical Analysis book, but in the one that I have the secant method is defined using the two-point interation method:

x(n+1) = x(n) - f(x(n))*(x(n)-x(n-1))/(f(x(n))-f(x(n-1))

So if it were me, I would use this formula directly:
```#include <stdio.h>
#include <math.h>

//This uses a pointer to a function so that you can pass an f(x) function to it.
//   namely the last parameter should be a pointer to a function
double Secant_Method(double x0, double x1, double err, double (*ptrFunc)(double));
double SomeFunction(double x);
double f1(double T2);
//we can not use this function as stated as it has two parameters
// I will assume that T2n is the point where f1(x) == 0
//double f2(double J2O, double T2n)
double f2(double J2O);

double T2n;

int main()
{
double value = Secant_Method(1, 2, 1E-10, SomeFunction);
printf("F(x)==0 at x~=%f\n",value);
printf("F(%f) ~= %f\n", value, SomeFunction(value));

T2n = Secant_Method(0, 1, 1E-10, f1);
printf("f1(x)==0 at x~=%f\n",T2n);
printf("f1(%f) ~= %f\n", T2n, f1(T2n));

//WOW these numbers are WAY out there
//  I would say that there is an error in formula #2
//  Mathematica solved it at T2n=331.41 x==5.338636567541994*10^9
//  if I had to GUESS, I would ask if you were using the correct units
value = Secant_Method(0, 1, 1E-10, f2);
printf("f2(x)==0 at x~=%f\n",value);
printf("f2(%f) ~= %f\n", value, f1(value));

return 0;
}

double Secant_Method(double x0, double x1, double err, double (*ptrFunc)(double))
{
// Formula x(n+1) = x(n) - f(x(n))*(x(n)-x(n-1))/(f(x(n))-f(x(n-1))

//x0 = x(n-1)
//x1 = x(n)
double x2; //x(n+1)
double fx0; //f(x(n-1))
double fx1; //f(x(n))
double dx; // x(n)-x(n-1)

fx0 = (*ptrFunc)(x0); //f(x(n-1))
fx1 = (*ptrFunc)(x1); //f(x(n))
do
{
dx=(x1 - x0);
x2 = x1 - fx1*(dx/(fx1-fx0));
x0 = x1;
x1 = x2;
fx0 = fx1; //no need to recalculate...
fx1 = (*ptrFunc)(x1); //f(x(n))
} while (fabs(fx1)>err && fabs(dx)>err);
return x1;
}

double SomeFunction(double x)
{
//Function:  f(x)=x^3-x^2-x-1
return x*x-x-1;
}

double f1(double T2)	   // Double T2 is used for improved precision of T2
{
double q = 750;	   // Rate at which the blackened surface absorbs the solar energy
double T3 = 303.15;	// Temperature in Kelvin at which the outside surface of the glass loses energy to the environment
double h = 20;		// Unidentified constant given
double E = 0.9;	   // The epsilon constant given
double S = 5.669E-8;  // Sigma constant given
double dX = .075;	 // Delta X converted from centimeters to meters
double k = 0.042;	 // Effective air thermal conductivity

return h*(T2-T3)+(E*S)*((T2*T2*T2*T2)-(T3*T3*T3*T3))-q;  // Non-linear equation in which we are solving for T2
}

double f2(double J2O)
{

double q = 750;	   // Rate at which the blackened surface absorbs the solar energy
double T3 = 303.15;	// Temperature in Kelvin at which the outside surface of the glass loses energy to the environment
double h = 20;		// Unidentified constant given
double E = 0.9;	   // The epsilon constant given
double S = 5.669E-8;  // Sigma constant given
double dX = .075;	 // Delta X converted from centimeters to meters
double k = 0.042;	 // Effective air thermal conductivity

return (((E/(1-E))*((S*T2n*T2n*T2n*T2n)-J2O)))-(E*S*T2n*T2n*T2n*T2n)-(T3*T3*T3*T3);
}
```

This uses a pointer to a function so that Secant_Method() can call the function. It makes the notation look a bit complicated but it simplifies the logic.

This method also had a problem with you finction, and Mathematica did as well. I would take a good look at f2()

### #14 Cobra3232

Reputation: -1
• Posts: 12
• Joined: 10-April 07

## Re: Solving 3 Equations in one program

Posted 15 April 2007 - 12:03 PM

I am kind of confused now what you just did. Everybody keeps sending me different example programs, and each one has something weird about it. I can calculate the T2 variabe, but when i go to calculate the J2O variable, im not sure what to do because i need to incorporate the value for T2 into that equation. How do i find T2, and then use that to find J2O in that same program? There has to be a way...

I know that T2 should come out to 331.4 and J2O should come out to like 662.2. The equation for J2O is linear so it should be quite simple. Im running out of time and any last opinions would help and very appreciated...thanks all for your help.

### #15 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: Solving 3 Equations in one program

Posted 15 April 2007 - 12:55 PM

The attached image is what I see in your code. If this is not the correct formula then what is? If it is the correct formula what makes you think that it will have a positive root?

My program just implemented a more strait forword approch of the secant method. It was also made more general than using two distinct secant method functions each customized to the calculation. It has two big differances from yours. #1 it does not have a set number of interations (your function did at most m interations), and #2 it will stop if the distance between x(n) and x(n+1) gets below the error value.

it is a little complicated because it uses a function pointer to pass the function that we will be finding the root of.