| | |
help needed to compute maclaurin series using c
![]() |
•
•
Join Date: Aug 2007
Posts: 10
Reputation:
Solved Threads: 0
please tell me how to compute the maclaurin series sinx=x-x^3/3!+x^5/5!-x^7/7!.....
I written below coding but i know it will not give correct output. can anyone help me to compute the series
I written below coding but i know it will not give correct output. can anyone help me to compute the series
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<math.h> Printf("Enter the n value"); scanf("%d", &n); main() { float x,i; float value,sinx; printf("Enter the x value"); scanf("%d", &x); for(i=1;i<=n;i=i+2) { value=pow(x,i); sinx=x-value/factorial(); } } void factorial() { int j, fact=1; for(j=1;j<n;j++) { fact=fact*j; } }
Last edited by ~s.o.s~; Jul 24th, 2008 at 1:47 pm. Reason: Added code tags, learn to use them.
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<math.h> int factorial( int ); int main() { int n; double x,i; double value, sinx; value = sinx = 0.0; printf("Enter the value for x and i"); scanf("%lf %d", &x, &n); for( i=1; i<=n; i=i+2) { value = pow(x,i); sinx += (x - value / factorial(i)); } printf("Result - %f", sinx ); getchar(); return 0; } int factorial( int n ) { int j, fact=1; for( j=n; j>0; j-- ) fact = fact * j; return fact; }
Where is your code indendation and the code tags. There where quite a lot of mistakes which you have done in the code. Have a look at the above with some slite modification. If you don understand ask!
ssharish
•
•
Join Date: Aug 2007
Posts: 10
Reputation:
Solved Threads: 0
for( i=1; i<=n; i=i+2)
{
value = pow(x,i); --> this is computing power of x,i
sinx += (x - value / factorial(i)); --> here value is smaller than factorial(i) value, so it is producing zero value. Normally i use dot with that number to produce the correct result. for ex: 2/5 will be given as 2./5 then only it is producing correct result.
So in this case what i have to do produce correct result
}
{
value = pow(x,i); --> this is computing power of x,i
sinx += (x - value / factorial(i)); --> here value is smaller than factorial(i) value, so it is producing zero value. Normally i use dot with that number to produce the correct result. for ex: 2/5 will be given as 2./5 then only it is producing correct result.
So in this case what i have to do produce correct result
}
Well, you could actually return of type double from your factorial function. You could have the denominator float as well to produce the right result. So try with return double and some modification in the factorial function by changing the data type of variable "fact" and do double calculation there instead of int.
And what inputs are you giving for variables x and n?
ssharish
And what inputs are you giving for variables x and n?
ssharish
Some notes:
1. Never use float for math calculations (as in original post). Use double type only.
2. Try to avoid factorial function calculations (especially as integer). This function has too fast growth.
3. Try to use recurrent dependiences.
For example (it's not ideal solution, of course):
1. Never use float for math calculations (as in original post). Use double type only.
2. Try to avoid factorial function calculations (especially as integer). This function has too fast growth.
3. Try to use recurrent dependiences.
For example (it's not ideal solution, of course):
c Syntax (Toggle Plain Text)
double sinRough(double x) { const double eps = 1e-6; double x2 = (x*x); double fact = 1.0 / 6.0; double sinx = x-x*x2*fact; double next = sinx; double step = 4.0; const int maxi = 1000; int neg = 0; for (int i = 0;fabs(next) > eps && i < maxi;step += 2.0,++i, neg ^= 1) { next *= x2; next /= step * (step + 1.0); sinx += (neg?-next:next); } return sinx; }
![]() |
Other Threads in the C Forum
- Previous Thread: Do i need a better compiler?
- Next Thread: Quadratic Formula
| Thread Tools | Search this Thread |
#include * adobe ansi array arrays asterisks binarysearch centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram inches include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux locate looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation owf pattern pdf performance pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






