License Public Domain
Lines 53
Keywords
finding zeros (2) numerical analysis (5)
Permissions
Viewable by Everyone
Editable by All Siafoo Users
Hide
Siafoo – the intersection of pastebin, help desk, version control, and social networking Join Siafoo Now or Learn More

The Newton-Raphson Method Atom Feed 0

In Brief Uses the Newton-Raphson Method to find zeros of a function.
# 's
 1/*
2Programmed By: David Isaacson, 2003
3Released into the Public Domain
4
5newton-raphson2.c
6
7The puropse of this program is to find the lowest zero of
8f(x)=4d*x^3-3x+1 where x=r/h and d=0.7
9
10this will be needed: f'(x)=12d*x^2-3
11*/
12
13#include <stdio.h>
14#include <math.h>
15
16int main()
17{
18 const float d=0.7; //defining d
19
20 double fofx; //f evaluated at x
21 double fprimeofx; //f' evaluated at x
22
23 double x; //the guess variable
24 double xnew; //the "next" guess variable
25
26 printf("Hello\n");
27
28 printf("\nWhat value shall we start at?");
29 scanf("%lf",&xnew);
30
31 do
32 {
33 x=xnew;
34 fofx=4*d*x*x*x-3*x+1; //eval f at x
35 fprimeofx=12*d*x*x-3; //eval f' at x
36 xnew=x-fofx/fprimeofx; //the n-r formula
37 printf("%.10f\n",xnew);
38
39 }while(xnew!=x);
40
41 printf("The graphs intersect at %.6f \n",xnew);
42 //this prints xnew to a precision of 6 decimal places
43
44 return 0;
45}
46
47/*
48 When I ran this on my computer, I recieved the result:
49
50 What value shall we start at?0
51 0
52 0.3333333333
53 0.3835125429
54 0.3877086296
55 0.3877413974
56 0.3877413994
57 0.3877413994
58 The graphs intersect at 0.387741
59 */
60
61
62
63
64
65

Uses the Newton-Raphson Method to find zeros of a function.