Formulas

I need help in writing code in C++ using the formula Tx-T9/T1-T9 = cosh [ m(L-x) ] / cosh(mL). Below is the data that I have collected and will be using. Note: The value for m can be found by iteration using a suggested starting value of 7.4. Thank you.
Variable

Value
Value
Value
Value

Voltage (V)

8
8
14
14

Current (Amps)

0.31
0.31
0.54
0.54

External Fan Setting (Off, Med, High

Off
Med
Off
High
Calculated m
T1 (°C) @ x = 0.00 m

46.3
36
76.6
55.8
N/A
T2 (°C) @ x = 0.05 m

38.9
28.7
58.7
37.5

T3 (°C) @ x = 0.10 m

33.2
24.9
46.3
28.6

T4 (°C) @ x = 0.15 m

29.5
23.4
38.4
24.9

T5 (°C) @ x = 0.20 m

26.7
22.6
33.2
23.1

T6 (°C) @ x = 0.25 m

24.7
22
29.8
22

T7 (°C) @ x = 0.30 m

23.6
21.7
27.9
21.6

T8 (°C) @ x = 0.35 m (tip)

23.4
21.8
27.4
21.6

T9 (°C) ambient air

20.7
20.7
20.6
20.8
N/A





Avg. m
1
2

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;


// Global variables (hmmm) - all needed by f and fprime
double L;                              // length of domain in m
double x;                              // position
double Tfactor;                        // temperature ratio


struct dataset
{
   double TL;                          // temperature at left-hand end
   double TR;                          // temperature at right-hand end (ambient)
   double T;                           // intermediate temperature
   double x;                           // position where that temperature is measured
};

//======================================================================

double f( double m )                   // function
{
   return cosh( m * ( L - x ) ) / cosh( m * L ) - Tfactor;
}

//======================================================================

double fprime( double m )              // derivative of f
{
   double arg  = m * L;
   double argx = m * ( L - x );
   return ( L - x ) * sinh( argx ) / cosh( arg ) - L * tanh( arg ) * ( f( m ) + Tfactor );
}

//======================================================================

double NewtonRaphson( double m0 )
{
   const int MAXITER = 100;            // maximum number of iterations
   const double TOLERANCE = 1.0e-10;   // tolerance for successive m values

   int iter = 0;                       // number of iterations
   double change = 1.0;                // anything large
   double m = m0;                      // eventual solution
   while ( iter < MAXITER && change > TOLERANCE )
   {
      iter++;
      double mprev = m;
      m = m - f( m ) / fprime( m );    // Newton-Raphson iteration
      change = abs( m - mprev );
   }

   if ( change > TOLERANCE && iter >= MAXITER ) cout << "Not converged.\n";

   return m;
}

//======================================================================

int main()
{
   const double m0 = 7.4;              // starting guess
   L = 0.4;                            // length of domain

   vector<dataset> tests = { { 46.3, 20.7, 38.9, 0.05 },        // Sets of values of T1, T9, T, x
                             { 46.3, 20.7, 26.7, 0.20 },
                             { 46.3, 20.7, 23.4, 0.35 },
                                                        };

   for ( auto e : tests )
   {
      Tfactor = ( e.T - e.TR ) / ( e.TL - e.TR );
      x = e.x;
      double m = NewtonRaphson( m0 );

      cout << "Case is:\n"
           << "TL = " << e.TL << '\n'
           << "TR = " << e.TR << '\n'
           << "T  = " << e.T  << '\n'
           << "x  = " << e.x  << '\n'
           << "Value of m = " << m << "\n\n";
   }
}

Case is:
TL = 46.3
TR = 20.7
T  = 38.9
x  = 0.05
Value of m = 6.90244

Case is:
TL = 46.3
TR = 20.7
T  = 26.7
x  = 0.2
Value of m = 7.48592

Case is:
TL = 46.3
TR = 20.7
T  = 23.4
x  = 0.35
Value of m = 7.52298
Last edited on
Thank you so much. I am getting an error when I try to compile the code on line 73 stating that 'tests' must be initialized by constructor, not by '{...}'. What does this mean?
It means that it's time you upgraded your compiler to at least C++11.

It's just possible that you can set compiler options like -std=c++11, but that's up to you.

You can run it in c++ shell (the little gear-wheel icon at the top right of the code). Alternatively you will have to create your vector of tests the old-fashioned way.

I have been able to compile the program. Thank you. One more question... If my initial guess is to be m0 = 7.4, How would I alter the code to allow for finding m2 = .85714, m3 = .71429, m4 = .57143, m5 = .42857, m6 = .28571, m7 = .14286, and m8 = 0. Would I have to create a loop?
Hello, @smitty007,
I'm not sure what m values you are referring to. The three tests that I set up are just three (T,x) pairs from the first row of your table. You can type the other 25 datasets in if you want to. The code loops through these on lines 72-84. It solves for m, given the values of x and the relevant temperatures.

I think you need to explain a bit more clearly what you want.
I will type the other 25 data sets into the code. Thank you so much for your help.
Topic archived. No new replies allowed.