samedi 20 décembre 2014

C Program to calculate the value of a function at a particular value of x using a McLaurin series expansion


I have a college assignment due soon for which I need to write a C program to calculate the value of a function at a particular value of x using a McLaurin series expansion to evaluate the function. I'm quite new to C programming and to the McLaurin series and could really use some help to implement this program.


The program should take in a a value for x, a real variable given to the appropriate number of decimal places. The function will return the value f(x), also a real number, to the same accuracy.


The function to be implemented is the solution to the following differential equation:


\frac{d^2f}{dx^2} + 5\frac{df}{dx} + 4f = 0,\tag{displayed} with \frac{df}{dx}(0) = 0 and f(0) = 2\tag{displayed}


This is the solution I've come up with:


f(x) = f(0) + \frac{f′(0)}{1!}x + \frac{f′′(0)}{2!}x^{2} + \frac{f′′′(0)}{3!}x^{3} +...\tag{displayed}


f(0) = 2 f′(0) = 0


Putting these values into the differential equation:


f′′(0) + 5f′(0) + 4f(0) = 0 f′′(0) + 5.0 + 4.2 = 0 f′′(0) = -8


Taking the derivative of the differential equation:


\frac{d^3f}{dx^3} + 5\frac{df^2}{dx^2} + 4\frac{df}{dx} = 0,\tag{displayed}


Then evaluating in 0:


\frac{d^3f}{dx^3} + 5f′′(0) + 4f′(0) = 0\tag{displayed} \frac{d^3f}{dx^3}(0) = 40


The next derivative of the differential giving:


f′′′′(0) + 5f′′′(0) + 4f′′(0) = 0 f′′′′(0) + 5.40 + 4.-8 = 0 f′′′′(0) = -168


And so on..


The solution for the recurrence relation:


f^{(n)}(0) = -\fract 23(-1)^{n}(4^{n}-4)\tag{displayed}


The program should calculate the value of f(x) based purely on the McLaurin series without using any of the functions available in the C library, only basic arithmetic operators +,-,*,/ can be used. Loops, logical statements and function calls can be used as seen fit.


What I had in mind for the program was to use printf to ask the user for a real number value for x. While it's not a real number to keep asking the user to enter real number. When they have done that, scanf the value for use in the program calculations.


As regards calculating the function I'm not sure exactly how many derivations would be appropriate. I'd like the program output the value of f(x) and the recurrence relation. Some comments on the code would be great if possible to help understand what's going on. Help appreciated, thank you.





Aucun commentaire:

Enregistrer un commentaire