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].
Test your function by comparing your values with those
returned by the standard math library function exp()
for a range of parameter values.
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!
double expo(double x);
double y, myexp,ratio;
myexp = expo(y);
ratio = myexp/exp(y);
printf("%8.2f %20.10f %20.10f\n",y,myexp,ratio);
double expo(double x)
term =term * x/(n-1);
result=result + term;
return result; }