November 14th, 2004, 08:40 PM

Prog, for computing maclaurin series, need help...
hi there, i made this program to calculate the estimated value using the maclaurin series. can someone please tell me where im wrong, i know im wrong so, and i typed it up knowing so hoping to find a direction, anyways thanks in advance
it must print to a table like so
x = vvvv
Number of terms Estimated value exp( x) Difference
1 xxxx xxxx xxxx
2 xxxx xxxx xxxx
3 xxxx xxxx xxxx
...
numTerms xxxx xxxx xxxx
PHP Code:
#include <stdio.h>
#include <math.h>
#define OUTFILENAME "exponentialFn.txt"
int main(void)
{
/* Variables */
int numTerms, n, k, term;
float est, eX, X, diff;
long factorial(int k);
FILE *outfile;
outfile = fopen(OUTFILENAME, "w");
/* Input */
printf("Enter the number of terms:");
scanf("%d", &numTerms);
printf("Enter the value of X:");
scanf("%f", &X);
/* Term */
for(k = 0; k < numTerms; k++)
{
term = k;
scanf("%d", &term);
}
/* Compute e^x */
for(k = 0; k < numTerms; k++)
{
eX = expf(X);
scanf("%f", &eX);
}
/* Compute Estimate */
for(k = 0; k < numTerms; k++)
{
est = pow(X,(k))/factorial(k) + pow(X,(k1))/factorial(k1);
scanf("%f", &est);
}
/* Compute the diffance */
for(k = 0; k < numTerms; k++)
{
diff =(pow(X,(k))/factorial(k) + pow(X,(k1))/factorial(k1))  expf(X);
if(diff > 0)
{
diff = diff*(1.0);
}
scanf("%f", &diff);
}
/* Print computations to a .txt table */
fprintf(outfile,"%f\n", X);
fprintf(outfile,"Number of terms  Estimated value  exp( x)  Difference\n");
fprintf(outfile,"%d  %f  %f  %f\n", term, est, eX, diff);
system("PAUSE");
return 0;
}
/**/
/*This function computes the factorial value*/
long factorial(int n)
{
if (n == 0)
return 1;
else
return n * factorial(n  1);
}
/**/
November 14th, 2004, 08:45 PM

It helps a lot to tell your potential respondents what errors are occurring or how the code isn't performing as expected. Very few of us here are psychic; I am, but my crystal is in the shop for 'ball' joints, so I'd just have to wing it, examining your code and trying to read your mind. I will confidently predict that I can sit down at the keyboard and break your program, though.
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
November 14th, 2004, 08:55 PM

Originally Posted by DaWei_M
It helps a lot to tell your potential respondents what errors are occurring or how the code isn't performing as expected. Very few of us here are psychic; I am, but my crystal is in the shop for 'ball' joints, so I'd just have to wing it, examining your code and trying to read your mind. I will confidently predict that I can sit down at the keyboard and break your program, though.
haha, ya okay, i changed the setup a bit. the problem is, it doesnt run... no okay, when i run it, it runs the first input statements then it just closes. it prints out nothing to the text file. the maclaurin series is ex = 1 + x1 / 1! + x2 / 2! + x3 / 3!... so i think i got that covered in the "/* Compute Estimate */" statement. im not to sure why its not working...
PHP Code:
#include <stdio.h>
#include <math.h>
#define OUTFILENAME "exponentialFn.txt"
int main(void)
{
/* Variables */
int numTerms, n, k, term;
float est, eX, X, diff;
long factorial(int k);
FILE *outfile;
outfile = fopen(OUTFILENAME, "w");
/* Input */
printf("Enter the number of terms:");
scanf("%d", &numTerms);
printf("Enter the value of X:");
scanf("%f", &X);
for(k = 0; k < numTerms; k++)
{
/* Term */
term = k = 1;
/* Compute e^x */
eX = expf(X);
/* Compute Estimate */
est = pow(X,(k))/factorial(k) + pow(X,(k1))/factorial(k1);
/* Compute the diffance */
diff =(pow(X,(k))/factorial(k) + pow(X,(k1))/factorial(k1))  expf(X);
if(diff > 0)
{
diff = diff*(1.0);
}
/* Print computations to a .txt table */
fprintf(outfile,"%f\n", X);
fprintf(outfile,"Number of terms  Estimated value  exp(x)  Difference\n");
fprintf(outfile,"%d  %f  %f  %f\n", term, est, eX, diff);
}
system("PAUSE");
return 0;
}
/**/
/*This function computes the factorial value*/
long factorial(int n)
{
if (n == 0)
return 1;
else
return n * factorial(n  1);
}
/**/
November 14th, 2004, 09:02 PM

Code:
for(k = 0; k < numTerms; k++)
{
/* Term */
term = k = 1; **** YOU JUST BLEW YOUR 'K' CONTROL RIGHT HERE *****
If you want to know what I meant by breaking your program, just enter the letter, 'A', at one of your first couple of prompts. Then read about scanf (or see the post, COMMONLY ASKED QUESTIONS) to see how to deal with human users that make typos, or with malicious users that snicker if they can make it go south while you're being evaluated for a grade or a raise.
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
November 14th, 2004, 09:04 PM

I'm not sure what all those scanf's are for  scanf is for user input, normally, but you seem to be overwriting the contents of your calculations with it.
I think you just want one loop, for each term. Then you can output the values as you go. You probably don't need to calculate expf in each iteration either, as it won't change.
In pseudo code
Code:
Get user input
write the output header
calculate eX
for (term = 0 term < numTerms; term++)
{
calculate estX using term
diff = abs(extX  eX)
fprintf the output to the file
}
close the output file
Edit: Bah, thats what I get for not refreshing before I post. Move along, nothing to see here. As you were.
Last edited by The Dark; November 14th, 2004 at 09:07 PM.
November 14th, 2004, 09:27 PM

DaWei_MI actually spotted that but after I posted. Thanks though
So with what i have the terms and eX collum returns a value if i take out the "/* Compute Estimate */" and "/* Compute the difference */" statements. if i put either in the program does nothing. i think ive narrowed it down, so what could be wrong with those two statements and how they affect the code?
November 14th, 2004, 10:02 PM

By "does nothing" do you mean it gives no sign of output or error message or dialog of any kind, just goes away? Until you do what? CtrlC? Reboot? Kick the tar out of the tower? We still can't see your code, you know; you've changed it in at least one way. Is that all? You're being hell on wheels to help.
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
November 14th, 2004, 10:09 PM

Your factorial function does not handle negative numbers as its input, yet you are passing k1 to it when k is zero. This will cause it to loop approximately 4 billion times (on a 32 bit machine).
November 14th, 2004, 10:29 PM

I envy you your spotting ability, Dark, but do you think that'd slow it down? :D
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
November 14th, 2004, 11:20 PM

Originally Posted by DaWei_M
By "does nothing" do you mean it gives no sign of output or error message or dialog of any kind, just goes away? Until you do what? CtrlC? Reboot? Kick the tar out of the tower? We still can't see your code, you know; you've changed it in at least one way. Is that all? You're being hell on wheels to help.
haha, ya just goes away then i kick the tar out of the tower! okay, it prompts me for the first two printfs, then it just closes and nothing is printed to the file. if i get rid of two of the statements mentioned, i get the "press any key to continue" thing whatever it is, and it prints out the terms collum and the eX value to my text file. i dont know what to say, does nothing obvious stand out as being wrong? i cant imagine thats the case. well im troubled by this, im sure its easy, i dont know what to do though... thanks
November 14th, 2004, 11:30 PM

Read The Dark's post about the looping....
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
March 19th, 2013, 09:53 PM

Help......
Please can someone help me with this C Programming question...
Write a C function mysin (double x, int n) which calculates the Maclaurin series for
sin(x) up to power 2n + 1, i.e.
x  x^3/3! + x^5/5! ... + ... + (1)^n *[x^(2n+1)]/(2n + 1)!
Suppose we have x = 1:0. Use the mysin function in a main program to produce a table
similar to the following (15:10 format for all doubles):
n approx exact approxexact
0 1.0000000000 0.8414709848 0.1585290152
1 0.8333333333 0.8414709848 0.0081376515
2 0.8416666667 0.8414709848 0.0001956819
3 0.8414682540 0.8414709848 0.0000027308
4 0.8414710097 0.8414709848 0.0000000249
5 0.8414709846 0.8414709848 0.0000000002
6 0.8414709848 0.8414709848 0.0000000000
7 0.8414709848 0.8414709848 0.0000000000
8 0.8414709848 0.8414709848 0.0000000000
9 0.8414709848 0.8414709848 0.0000000000
10 0.8414709848 0.8414709848 0.0000000000
Thank You
March 20th, 2013, 12:18 PM

First, I think you should limit the range of x.
Use mod(x,TAU) where TAU=2 pi to find the quadrant.
Cut the range further using
mod(x,TAU/8)
Now compute the Taylor series on that value,
use trigonometric identities (double angle formulas and the sign from quadrant) to figure out the answer.
In computing the Taylor series you should combine the small magnitude terms first, which you'll be able to predict given that you cut the domain to [0,TAU/8)
Subtraction is a real problem because it throws away the significant information.
Has anyone got thoughts about
A = sum(positive terms sorted low to hi)
B = sum(abs(negative terms sorted low to hi))
A  B
or is there a better way to compute the series?
[code]
Code tags[/code] are essential for python code and Makefiles!