473,324 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Regarding Taylors Sinx Series...

As I posted earlier,I ve been practicing C for my upcoming Graduate Studies Venture.Just reading online books and practicing some basic codes.I happen to play with taylors Sinx Series on Netbeans IDE and when I run the program with values of 'n' and angle in radians,the compiler is stuck.The only thing I can see is that my processor usage is full and I cant do anything else.

Here is the code that I made using Sinx formula from calculus book.



Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. int main()
  4. {
  5.     int n,m;
  6.     double sesum;
  7.     double sinx(double,double);
  8.     printf("Enter the limit of series greater than 0\n");
  9.     scanf("%f",&n);
  10.     printf("Enter the angle in radians\n");
  11.     scanf("%f",&m);
  12.     sesum=sinx(n,m);
  13.     printf("The value of series is %f",sesum);
  14.     return 0;
  15.  
  16.  }
  17. double sinx(double p,double q)
  18. {
  19.     int i;
  20.     double fact(double);
  21.     double sum=0;
  22.     for(i=0;i<=p;i++)
  23.             {
  24.                 sum=sum+(pow(q,2*i+1)/fact(2*i+1))*(pow(-1,i));
  25.             }
  26.  
  27.     return sum;
  28. }
  29. double fact(double a)
  30. {
  31.     double fact=1;
  32.     int j;
  33.     for(j=a;j>=1;j--)
  34.     {
  35.         fact=a * (a-1);
  36.     }
  37.     return fact;
  38. }

I just need some hints or implication as to where I can improve so that code will work without any problems with IDE.Further any suggestions for a good book of C and C++.Preferably American author.
May 14 '10 #1
9 2405
donbock
2,426 Expert 2GB
Please post the mathematical formulas you are trying to implement.

Something seems wrong. For example, I assume that fact is supposed to compute the factorial of the input; but the input is floating-point. Factorial only makes sense for integral arguments.
May 14 '10 #2
Yeah...even I tried to insert int data type first.But still NetBeans was acting weird thats why I used float data type.
I am inserting an image of that formula which I have here with me.
May 15 '10 #3
weaknessforcats
9,208 Expert Mod 8TB
Your scanf()s are not correct.

You say %f to put data into m and n which are ints. The result is a grebage value.

On a test run I made the garbage value is so large the machine appears to hang.

Use %d with ints.
May 15 '10 #4
@weaknessforcats

Did that.....And then I tried again...Now when I enter the values I get following result...

The value of series is 1.#INF00


I guess I will have to choose data types that are more suitable for these operations,I mean something float.Using double is also not harmful...Is it?
May 16 '10 #5
weaknessforcats
9,208 Expert Mod 8TB
Your fact() function does not work.

I suggest you use an int argument and an int result since factorial applies only to integers.

The factorial of 4 is 24 but your function got 49 for an answer.
May 16 '10 #6
whodgson
542 512MB
Your variable m is of type int as mentioned above so it only takes whole radians.
There are only Pi/4 radians in 90°!
May 17 '10 #7
jkmyoung
2,057 Expert 2GB
For optimization, I would not use fact(), or pow()

Store variables to hold the current factorial value and the current power value. You only need 2 easy multiplications to get to the next factorial value, and another multiplication to get the current power value (can be done in the for loop).
For added efficiency, you can simply store the term value as opposed to the factorial and power values.

Calculate -x^2 beforehand.
eg, for each iteration i, you multiply by -x^2/(2i)/(2i+1)
May 17 '10 #8
@jkmyoung

Sorry but I didnt understand what you said in your reply.....do you think I should calculate powers and factorials in other functions and store these values in some variables to use them in sinx function?
May 18 '10 #9
donbock
2,426 Expert 2GB
Look at the mathematical formula you provided in reply #3. jkmyoug pointed out that you can compute term i by multiplying term i-1 by -(x^2)/(2i)/(2i+1). You can speed up each iteration a little more if you precompute x^2 in a variable and use that for computing term i.
May 18 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Sriram Krishnan [at] gmx [dot] net | last post by:
Given an arbitrary number series, how do I identify the series and find the Nth number... for e.g 1,3,5,7 is an AP and the next number will be 9 1,8,27,64 is the series of cubes and the next will...
4
by: aW | last post by:
I have an interesting dilemma. I have a table with the following records: =================================================== Box | Series Start | Series End...
1
by: Sriram Krishnan [at] gmx [dot] net | last post by:
Given an arbitrary number series, how do I identify the series and find the Nth number... for e.g 1,3,5,7 is an AP and the next number will be 9 1,8,27,64 is the series of cubes and the next will...
3
by: Simon | last post by:
Hi everyone, I have a small problem regarding a wizard that I'm making on my website. The wizard is obviously a series of pages that take values from the user. My question is: - Should I...
0
by: Bhaskar | last post by:
Hi all, Problem: OS : Win XP Platform: VB.NET(Web Application) My application enumerates all the printers in the network. Currently there are only 2 network printers here. 1.EPSON TM...
5
by: sam_cit | last post by:
Hi Everyone, I read somewhere that there are some compile time operations behind switch-case, which is why it can work for cases which evaluates to an integer or character and not strings and...
1
by: rohiniswaminathan | last post by:
i want to write a program to calculate sum of the trignometric series for n terms sinx=x-x^3/3 +x^5/5+....x^n/n in c language
3
mia023
by: mia023 | last post by:
i was wondering if i could compute sin x using Taylor series my code was as follows i guess it is not correct. Tell me please what is wrong and what i should do. I guess there are alot of errors in...
4
by: keirnus | last post by:
Hello, I've been coding in Access VBA to create an Excel Graph and it was good. Until I got this error: Please check the code below: Private Sub TestGraph3()
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.