![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 4
| I am taking a required c programming class at school, and I feel like the projects are way over my head. This is only the second project, and I am unsure of how to start it. I'm not asking for anyone to write the code for me. I just need some help starting the project. The objective is to write a program based on the Taylor series of expansion. The first part of the project is: Develop a function in C that calculates the error between an exponential function evaluated at x and the Taylor Series approximation using only the first two terms, i.e. 1+x. It should take in the value of x and return the value of the error. Any help would be greatly appreciated!!! |
| MG2610 is offline | |
| | #2 |
| Robot Join Date: Mar 2009
Posts: 258
| Not accounting for precision loss, it sounds like you just need to return the absolute error, i.e. Code: double error = abs(1+x - exp(x)) |
| Memloop is offline | |
| | #3 |
| Registered User Join Date: Oct 2009
Posts: 4
| This is what I have so far, but I'm not sure where to go from here: Code: #include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
int x;
double error = abs(1+x - exp(x));
printf("Enter a value for x \n");
scanf("%d", &x);
getchar();
return 0;
}
|
| MG2610 is offline | |
| | #4 |
| Registered User Join Date: Sep 2006
Posts: 3,133
| You need to calculate the error, *after* you have been given the value for x by the user's input. Code: #include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
int x;
double error;
printf("\nEnter a value for x: ");
scanf("%d", &x);
error = abs(1+x - exp(x));
printf(" x is: %lf\n\n\n\t\t\t press enter when ready", x);
getchar();
return 0;
}
Last edited by Adak; 10-31-2009 at 04:02 PM. |
| Adak is offline | |
| | #5 |
| Registered User Join Date: Oct 2009
Posts: 4
| Okay I changed it around a little bit... Code: #include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
int x;
char input1 = 'E';
double error;
printf("\n\nEnter E to calculate error\n");
printf("Enter Q to quit program\n");
scanf("%c", &input1);
while( input1 != 'Q' )
{
if ( input1 == 'E' )
{
printf("\nEnter a value for x: ");
scanf("%d", &x);
error = abs(1+x - exp(x));
printf("error is: %lf\n\n", error);
}
else
{
printf( "\nERROR! Please try again!\n\n" );
}
}
return 0;
}
Thanks for the help!! |
| MG2610 is offline | |
![]() |
| Tags |
| help new to c programming |
| Thread Tools | |
| Display Modes | |
|