You are on page 1of 8

\\1.Finding root #include<stdio.h> #include<conio.h> #include<math.

h> void main() { float x1,x2,xn,fx1,fx2,fxn,dx2,dxn,con=-99; int i=0; clrscr(); printf("enter the value of x1:"); scanf("%f",&x1); printf("enter the value of x2:"); scanf("%f",&x2); fx1=pow(x1,3)+3*x1*x1-3; fx2=pow(x2,3)+3*x2*x2-3; printf("fx1 : %.4f\nfx2 : %.4f\n",fx1,fx2); if(x1>x2) dx2=3*x2*x2+6*x2; else { dx2=3*x1*x1+6*x1; x2=x1; fx2=fx1; } while(fabs(con-x2)>.0001) { i=i+1; if(i>15) goto last; con=x2; xn=x2-(fx2/dx2); fxn=pow(xn,3)+3*xn*xn-3; dxn=3*xn*xn+6*xn; printf("x%d : %f\tfx%d : %f\n",i+1,xn,i+1,fxn); x2=xn; fx2=fxn; dx2=dxn; } last: printf("Root of equation : %f",xn); getch(); }

2. I'm a numerical methods and modeling engineer so I should understand the NewtonRaphson Method to be: j( i+1 ) = j( i ) - [ j( i )^2 - 2 ] / [ 2*j( i ) ] for the function f( j ) = (j *j - 2).

So, you should step i forward until f( j ) is within your tolerance of 0. Then y our answer should be your last j. Here's some pseudocode: i = 0 while( absoluteValue( f( j ) ) > toleranceValue) { j = j - [ f( j ) ] / [ df( j ) ]; i++; } printOut("Root of Function is at %g After %d Iterations" j, i );

3. #include <iostream> 02 #include <cmath> 03 using namespace std; 04 05 double find_root(double num, int root) 06 { 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 } } for (i = 0; i <= num; i++) { val = i*i-num; if (val > 0.0) { xn = (i+(i-1))/2.0; break; double x = 0.0; double xn = 0.0; double val; int repeat = 0; int i;

22 23 24 25 26 )); 27 28 29 30 } 31 32 int main() 33 { 34 35 36 37 38 39 40 ; 41 42 43 44 45 46 47 48 49 50 } if (root == 2) root_name = "nd"; if (root == 3) root_name = "rd"; if (root <= 1) { cout << "The root must be greater than 1.\n"; system ("pause"); return 0; cout << "Enter a number: "; cin >> num; cout << "Enter the number of the root you want to find for this number: " cin >> root; double num; int root; string root_name; return xn; } while (!(repeat++ >= 100 { x = xn; xn = x - (pow(x, (double)root) - num) / (root * pow(x, (double)root-1 x == xn))

51 52

if (root > 3) root_name = "th";

53 cout << "The " << root << root_name << " root of " << num << " is " << fi nd_root(num, root) << ".\n"; 54 55 56 57 } system ("pause"); return 0;

You are to write a program to solve a cubic equation ax3+bx2+cx+d using the New ton-Raphson method. Your program should ask the user to enter coefficients: a, b , c and d. Check that a is not equal to zero. the program should ask the user to enter the initial guess, xo and convergence indicator, . 02 03 b. Apply the Newton-Raphson iteration and determine the solution if any. Appr opriate messages need to be printed. 04 05 -. Ask the user whether he/she wants to solve another cubic equation, in whic h case repeat steps (a) - (b) above. 06 07 -. If the user quits the program, please print the followings: 08 Number of cubic equations solved: 09 The average value of coefficients a entered: 10 The largest value of coefficients b entered: 11 The number of cubic equations with negative d values solved:You may use the f ollowing algorithm for Newton-Raphson method: 12 Initialize: xo, E, N 13 fo = f(xo) 14 i = 0 15 while [ i < N and fo > E] 16 i = i + 1 17 xo = xo - fo/ f '(xo) 18 fo = f(xo) \ E = convergence indicator, N = iteration limit

19 end while 20 if [ fo > E ] then 21 22 else 23 print: Estimated solution xo and check value of fo print: "Non-convergence to required limit" message

24 end if*/#include<stdio.h> 25 #include<math.h> 26 int main() 27 { 28 char command; 29 double E; 30 int i=0,s=1,r=1; 31 double N = 12; 32 double a,b,c,d; 33 double fx,fpx,X0,x; 34 printf("Enter the value of a, b, c and d: "); 35 scanf("%lf %lf %lf %lf",&a,&b,&c,&d); 36 if(a>0) 37 { 38 printf("Enter the value of X0 (initial guess): "); 39 scanf("%lf",&X0); 40 printf("Enter the value of convergence limit: "); 41 scanf("%lf",&E); 42 43 while(command!='q') 44 { 45 46 x= X0; 47 fx = a*x*x*x +b*x*x +c*x +d; 48 i=0;

49 while(i<N) 50 { 51 52 53 54 55 56 57 58 } 59 60 61 printf("Press 'c' to continue or 'q' to quit: "); 62 scanf("%c",&command); if(command=='c') 63 printf("Enter the value of a, b, c and d (separated by space): "); 64 scanf("%lf %lf %lf %lf",&a,&b,&c,&d); 65 } 66 } 67 printf("After iteration %d, root = %f\n",r++,x); fx = a*x*x*x +b*x*x +c*x +d; fpx = 3*a*x*x +2*b*x +c; x= x - (fx/fpx); i=i+1;

int main() 02 { 03 04 05 06 char command = 'c'; // assume we will loop :) double E; int i=0,s=1,r=1; double N = 12;

07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

double a,b,c,d; double fx,fpx,X0,x;

while(command == 'c') { printf("Enter the values of a, b, c and d (separated by space): "); scanf("%lf %lf %lf %lf",&a,&b,&c,&d);

if(a == 0) break; // exit

printf("Enter the value of X0 (initial guess): "); scanf("%lf",&X0);

printf("Enter the value of convergence limit: "); scanf("%lf",&E);

x= X0; fx = a*x*x*x +b*x*x +c*x +d; i=0;

while(i<N) { i=i+1;

fx = a*x*x*x +b*x*x +c*x +d; fpx = 3*a*x*x +2*b*x +c; x= x - (fx/fpx);

printf("After iteration %d, root = %f\n",r++,x);

37 38 39 40 41 42 43 44 45 46 47 48 } }

scanf("%c",&command); // eat extraneous newlines

printf("Press 'c' to continue or 'q' to quit: ");

scanf("%c",&command);

system("pause"); return 0;

You might also like