Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,124 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,784 people online right now. Registration is fast and FREE... Join Now!




Solving 3 Equations in one program

2 Pages V  1 2 >  
Reply to this topicStart new topic

Solving 3 Equations in one program

Cobra3232
12 Apr, 2007 - 05:13 PM
Post #1

New D.I.C Head
*

Joined: 10 Apr, 2007
Posts: 12


My Contributions
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:

biggrin.gif
CODE


#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?
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Solving 3 Equations In One Program
12 Apr, 2007 - 11:07 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
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

CODE

//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"
//  your functions had the same profile
//Profile: double SecantMethod(double, double, double, int);
// note that there are no vaiable names in the profile... so both
//of your functions had the same profile... so I renamed them.

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.
User is offlineProfile CardPM
+Quote Post

Cobra3232
RE: Solving 3 Equations In One Program
13 Apr, 2007 - 09:08 AM
Post #3

New D.I.C Head
*

Joined: 10 Apr, 2007
Posts: 12


My Contributions


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

instead of:

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.






QUOTE(NickDMax @ 13 Apr, 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

CODE

//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"
//  your functions had the same profile
//Profile: double SecantMethod(double, double, double, int);
// note that there are no vaiable names in the profile... so both
//of your functions had the same profile... so I renamed them.

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.


User is offlineProfile CardPM
+Quote Post

dizzywiggle
RE: Solving 3 Equations In One Program
13 Apr, 2007 - 09:33 AM
Post #4

New D.I.C Head
*

Joined: 13 Apr, 2007
Posts: 9


My Contributions
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

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Solving 3 Equations In One Program
13 Apr, 2007 - 09:45 AM
Post #5

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
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)
User is offlineProfile CardPM
+Quote Post

Cobra3232
RE: Solving 3 Equations In One Program
13 Apr, 2007 - 10:47 AM
Post #6

New D.I.C Head
*

Joined: 10 Apr, 2007
Posts: 12


My Contributions
Im using microsoft visual C++ 2005 if that makes a difference but i dont think it would...
User is offlineProfile CardPM
+Quote Post

Cobra3232
RE: Solving 3 Equations In One Program
13 Apr, 2007 - 11:00 AM
Post #7

New D.I.C Head
*

Joined: 10 Apr, 2007
Posts: 12


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Solving 3 Equations In One Program
13 Apr, 2007 - 12:40 PM
Post #8

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
... 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?
User is offlineProfile CardPM
+Quote Post

Cobra3232
RE: Solving 3 Equations In One Program
13 Apr, 2007 - 03:43 PM
Post #9

New D.I.C Head
*

Joined: 10 Apr, 2007
Posts: 12


My Contributions
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:
CODE

#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;
}

User is offlineProfile CardPM
+Quote Post

Cobra3232
RE: Solving 3 Equations In One Program
14 Apr, 2007 - 09:44 AM
Post #10

New D.I.C Head
*

Joined: 10 Apr, 2007
Posts: 12


My Contributions
I guess a better question might be

When an asnwer comes up as "-1.#IND0000000", what does that mean?
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Solving 3 Equations In One Program
14 Apr, 2007 - 12:14 PM
Post #11

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Cobra3232
RE: Solving 3 Equations In One Program
14 Apr, 2007 - 01:35 PM
Post #12

New D.I.C Head
*

Joined: 10 Apr, 2007
Posts: 12


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/1/08 10:12PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month