help needed to compute maclaurin series using c

Reply

Join Date: Aug 2007
Posts: 10
Reputation: sasikala123 is an unknown quantity at this point 
Solved Threads: 0
sasikala123 sasikala123 is offline Offline
Newbie Poster

help needed to compute maclaurin series using c

 
0
  #1
Jul 24th, 2008
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
  1. #include<stdio.h>
  2. #include<math.h>
  3. Printf("Enter the n value");
  4. scanf("%d", &n);
  5. main()
  6. {
  7. float x,i;
  8. float value,sinx;
  9. printf("Enter the x value");
  10. scanf("%d", &x);
  11. for(i=1;i<=n;i=i+2)
  12. {
  13. value=pow(x,i);
  14. sinx=x-value/factorial();
  15. }
  16. }
  17.  
  18. void factorial()
  19. {
  20. int j, fact=1;
  21. for(j=1;j<n;j++)
  22. {
  23. fact=fact*j;
  24. }
  25. }
Last edited by ~s.o.s~; Jul 24th, 2008 at 1:47 pm. Reason: Added code tags, learn to use them.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 241
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: help needed to compute maclaurin series using c

 
0
  #2
Jul 24th, 2008
  1. #include<stdio.h>
  2. #include<math.h>
  3.  
  4. int factorial( int );
  5.  
  6. int main()
  7. {
  8. int n;
  9. double x,i;
  10. double value, sinx;
  11.  
  12. value = sinx = 0.0;
  13.  
  14. printf("Enter the value for x and i");
  15. scanf("%lf %d", &x, &n);
  16.  
  17. for( i=1; i<=n; i=i+2)
  18. {
  19. value = pow(x,i);
  20. sinx += (x - value / factorial(i));
  21. }
  22.  
  23. printf("Result - %f", sinx );
  24.  
  25. getchar();
  26. return 0;
  27. }
  28.  
  29. int factorial( int n )
  30. {
  31. int j, fact=1;
  32.  
  33. for( j=n; j>0; j-- )
  34. fact = fact * j;
  35.  
  36. return fact;
  37. }

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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 79
Reputation: Adak is an unknown quantity at this point 
Solved Threads: 7
Adak Adak is offline Offline
Junior Poster in Training

Re: help needed to compute maclaurin series using c

 
0
  #3
Jul 24th, 2008
<< beat me to it >>
Last edited by Adak; Jul 24th, 2008 at 7:56 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 10
Reputation: sasikala123 is an unknown quantity at this point 
Solved Threads: 0
sasikala123 sasikala123 is offline Offline
Newbie Poster

Re: help needed to compute maclaurin series using c

 
0
  #4
Jul 24th, 2008
Thanks for the reply, but still i have doubt regd how it will calculate for alternative + and - sign in series. how it works????

sinx=x-x^3/3!+x^5/5!-x^7/7!.....
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 10
Reputation: sasikala123 is an unknown quantity at this point 
Solved Threads: 0
sasikala123 sasikala123 is offline Offline
Newbie Poster

Re: help needed to compute maclaurin series using c

 
0
  #5
Jul 24th, 2008
code is returning zero value. how to produce non zero value by dividing a small number by big number
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 241
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: help needed to compute maclaurin series using c

 
0
  #6
Jul 24th, 2008
Well, you need to do some error checking there. Thats not a complete code as I said before. You need to work upon it. You need to do some error checking there. Anything divided by 0 is 0. What are the input values did you give?

ssharish
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 10
Reputation: sasikala123 is an unknown quantity at this point 
Solved Threads: 0
sasikala123 sasikala123 is offline Offline
Newbie Poster

Re: help needed to compute maclaurin series using c

 
0
  #7
Jul 24th, 2008
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
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 241
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: help needed to compute maclaurin series using c

 
0
  #8
Jul 24th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: help needed to compute maclaurin series using c

 
0
  #9
Jul 25th, 2008
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. double sinRough(double x)
  2. {
  3. const double eps = 1e-6;
  4. double x2 = (x*x);
  5. double fact = 1.0 / 6.0;
  6. double sinx = x-x*x2*fact;
  7. double next = sinx;
  8. double step = 4.0;
  9.  
  10. const int maxi = 1000;
  11. int neg = 0;
  12.  
  13. for (int i = 0;fabs(next) > eps && i < maxi;step += 2.0,++i, neg ^= 1)
  14. {
  15. next *= x2;
  16. next /= step * (step + 1.0);
  17. sinx += (neg?-next:next);
  18. }
  19. return sinx;
  20. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: help needed to compute maclaurin series using c

 
0
  #10
Jul 25th, 2008
Oh, I'm sorry, advice #4:
4. Don't use expensive pow() function if you can do it differently.

Good luck!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC