Note that there are some explanatory texts on larger screens.

plurals
  1. POTaylor Series Calculating Program in C compiled with GCC
    text
    copied!<p>I have written a program in C that calculates the approximate value of the function exp(x) at various values of x, some positive and some negative. My questions are as follows:</p> <ol> <li>Even though I was able to include 151 terms in the sequence before, now the series blows up for exp(100) even at the 140th term. Afterwards I messed up more with the code and now the series blows up even at the 130 term. In addition to that I have used long double as the number format to allow large factorials to be calculated and displayed. How can one explain this phenomenon? Sometimes after successive execution of the code I get results approximating the real value of the function which I checked from Wolfram Alpha.</li> <li>I have heard that "for" loops proved to be inefficient; do you have alternatives for "for" loop in mind? I would be happy to try to implement them as much as I can.</li> <li>I want to display the output in scientific format but the format specifier (I am not sure about the term) did not allow me to do so. The specifier I used in printf statement was %g. How can I have the output displayed in scientific format?</li> <li>Based on the hardware and software I am using is this the maximum precision I can get?</li> <li>I also getting a Segmentation Fault (core dumped) error instead of the display for x = -100. What may be the reason behind this?</li> </ol> <p>Thanks for your contribution. I appreciate any help and recommendations. </p> <p>P.S: I compile using GCC on 64-bit hardware, but 32-bit Ubuntu 12.04 LTS (a.k.a Precise Pangolin).</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;math.h&gt; //We need to write a factorial function beforehand, since we //have factorial in the denominators. //Remembering that factorials are defined for integers; it is //possible to define factorials of non-integer numbers using //Gamma Function but we will omit that. //We first declare the factorial function as follows: long double factorial (double); //Long long integer format only allows numbers in the order of 10^18 so //we shall use the sign bit in order to increase our range. //Now we define it, long double factorial(double n) { //Here s is the free parameter which is increased by one in each step and //pro is the initial product and by setting pro to be 0 we also cover the //case of zero factorial. int s = 1; long double pro = 1; if (n &lt; 0) printf("Factorial is not defined for a negative number \n"); else { while (n &gt;= s) { pro *= s; s++; } return pro; } } int main () { long double x[13] = { 1, 5, 10, 15, 20, 50, 100, -1, -5, -10, -20, -50, -100}; //Here an array named "calc" is defined to store //the values of x. //The upper index controls the accuracy of the Taylor Series, so //it is suitable to make it an adjustable parameter. However, after //a certain value of p the series become infinitely large to be //represented by the computer; hence we terminate the series at //the 151th term. int p = 150; long double series[13][p]; int i, k; //We only define the Taylor series for positive values of the exponent, and //later we will use these values to calculate the reciprocals. This is done //in this manner to avoid the ambiguity introduced into the sum due to terms //alternating in sign. long double sum[6] = { 0 }; for (i = 0; i &lt;= 6;i++) { for (k = 0; k &lt;= p; k++){ series[i][k] = pow(x[i], k)/( factorial(k)); sum[i] += series[i][k]; } printf("Approximation for x = %Lf is %Lf \n", x[i], sum[i]); } //For negative numbers -taking into account first negative number is //in the 8th position in the x array and still denoting the approximation long double approx[5] = { 0 }; for (i = 7; i &lt;= 12;i++) { approx[i - 7] = 1 / sum[i - 7]; printf("Approximation for x = %Lf is %Lf \n", x[i], approx[i - 7]); } //printf("%Lf \n", factorial(3)); //The above line was introduced to test if the factorial function //was functioning properly. } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload