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!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; }
Thanks.



LinkBack URL
About LinkBacks



