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!
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:
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; }
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?
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.
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.
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.
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)
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.
... 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).
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
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.
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.