1. ## Sine series summation

Okay so, Im not going to ask that you give me specific code or anything, Im just looking for a little guidance. Im new to C and frankly im pretty lost.

I have to do the following quesion:

Write your own function, double sine1(double x); which returns the value of the sine of x by evaluating the series expansion sin(x)= x- x^3/3! + x^5/5! - X^7/7"!.... etc. where x is in radians. You can calculate each new term from the previous term.[Hint: the termx^n in is –x^2/n(n-1) times the term in x^n-2]. You will have to stop the loop either after a fixed number of terms, or by breaking out when the current term becomes sufficiently small. Write a program to test your function by comparing your values with those returned by the standard math library function sin(). Your program should print out a comparison table listing x, sine1(x), sin(x) and the difference sine1(x) – sin(x) for a range of angles. Record a copy of your sample output.

My lecturer no doubt is looking something similar to how the following question was done:

Write your own function, double expo(double x);
which returns the value of the exponential function e^x
based on the series expansion e^x=1+x+x^2/2!+x^3/3!+ ... etc.
You can calculate each new term from the previous term
[hint: the nth term is x/(n-1) times the (n-1)th term].
returned by the standard math library function exp()
for a range of parameter values.
*/

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double expo(double x);

int main()
{
double y, myexp,ratio;

for (y=0.0;y<2.0;y=y+0.1)
{
myexp = expo(y);
ratio = myexp/exp(y);
printf("%8.2f  %20.10f  %20.10f\n",y,myexp,ratio);
}
return 0;
}
double expo(double x)
{
double term,result;
int n;
term=1.0;
result=1.0;
for (n=2;n<20;n=n+1)
{
term =term * x/(n-1);
if(term/result<1e-1)break;
result=result + term;
}
printf("%4d",n);
return result; }```
Ive been playing around for a while and im just getting nowhere. My knowledge is C is obviously very shakey, but I really need help! Would anyone even perhaps explain (in bullet point form, something brief) the exponential code was constructed? Particularly that within bold. Hopefully from that I can tackle my own question!

Thanks.

2. You are basically be asked to find the value of.

x- x^3/3! + x^5/5! - X^7/7

So....

I would do something like this, it may have some problems but it might get you on the
right lines?

Code:
```
for( n=3; n<somebigvalueforn; n=n+2){//you are stepping through the terms here
z=n;//z is the 'significant number in the term, you need a 'copy' of it.
term=x; //x is the angle

while(z>1){ you are calculating them here.
term=x*term/z;
z--;
}

running_total+=term

}

result=running_total+x;```
I think basically you needed another loop?
Might be a prob with division returning a double?
Try it.

You could start from 1 not 3 I guess, looks nicer perhaps.

3. If you use the Maclaurin series for sine, you could modify my cosine function that you could probably find if you searched. Here is what I discovered to make it easy.

You actually dont need to calculate factorials. Look at this expression
Code:
`x^3/3!`
Is that not the same as
Code:
`x/3 * x/2 * x/1`
<bighint>There you go</bighint>

4. Originally Posted by carrotcake1029
If you use the Maclaurin series for sine, you could modify my cosine function that you could probably find if you searched. Here is what I discovered to make it easy.

You actually dont need to calculate factorials. Look at this expression
Code:
`x^3/3!`
Is that not the same as
Code:
`x/3 * x/2 * x/1`
<bighint>There you go</bighint>
Yes it is, I was not sure if you could use ! as I rarely need to use it.
Same goes for ^
Makes it lot easier I guess