70 likes | 219 Views
Youngnam Kim. Solving the nonlinear equation by using the Newton’s method. • Newton’s method for solving an equation of one variable. Initial value: x i. Using Taylor expansion. Repeat ,until a sufficiently accurate value is reached.
E N D
Youngnam Kim Solving the nonlinear equation by using the Newton’s method • Newton’s method for solving an equation of one variable. Initial value: xi Using Taylor expansion Repeat ,until a sufficiently accurate value is reached
• Newton’s method to solve equations of two variables. System of differential equations Initial values: xi, yi Using Taylor expansion Using Cramer’s rules Repeat ,until a sufficiently accurate values are reached
Algorithm • Newton’s method for solving an equation of one variable. Input: Initial value xi , Error bound ε ; Calculate: Approximate solution xn+1 ; Compute f’(xn) If f’(xn) = 0 then Output “Failure”, END ; Else compute DO{} while( ≥ ε) ; OUTPUT: xn+1 END • Newton’s method to solve equations of two variables. Input: Initial value xi, yi, Error bound ε ; Calculate: Approximate solution xn+1, yn+1 ; Compute If ≠ 0 then compute DO{ } while( ) ; Else Output “Failure”, END ; OUTPUT: xn+1, yn+1 ; END
Source Program • Newton’s method for solving an equation of one variable. /* Newton's method for solving an equation of one variable*/ /* Include header file */ #include <stdio.h> #include <math.h> /* Define global variable */ float xi,x,err; float f,df; char FOUTN[20]; FILE *FOUT; void Newton(); /* General routine */ void F(); void DF(); /* Function */ /* Main routine */ void main(){ printf("\nNewton's method for solving equation of one variable\n"); printf("\nEquation\nf(x)=x^3-x-1=0\ndf(x)=3x^2-1 \n Error Bound = 1.0e-14\n"); err = 1.0e-14; /*Input the name of the output file and open it */ printf("\nINPUT THE NAME OF THE OUTPUT FILE\n"); scanf("%s",FOUTN); if((FOUT=fopen(FOUTN,"w"))==NULL){ printf("FILE OPEN ERROR...\n"); getch(); exit(-1); } /* Input value of xi */ printf("\nINPUT VALUE OF xi\n xi = "); scanf("%f",&xi); x=xi; do{ Newton(); } while(fabs(f/df)>err); /* Print the root */ printf("Root = %10.3f",x); fprintf(FOUT,"Root = %10.3f",x); fclose(FOUT); getch(); } void Newton(){ F(); DF(); if(df==0){ printf("Failure!!"); getch(); exit(-1); } else{ x=x-(f/df); } } void F(){ f=x*x*x-x-1; } void DF(){ df=3*x*x-1; }
Source Program •Newton’s method to solve equations of two variables. /* Newton's method to solve equations of two variables */ /* Include header hile */ #include <stdio.h> #include <math.h> #include <conio.h> #include <stdlib.h> /* Global variable */ int xi,yi,fx,fy,gx,gy,det; float x,y; float f,g; char FOUTN[20]; FILE *FOUT; /* General routines */ void NEWTON(); /* Routines dependent on the system */ void F(); void FX(); void FY(); void G(); void GX(); void GY(); /* Main routine */ void main(){ printf("\n Newton's method to solve equatiosn of two variables\n"); printf("\n Equation\nF(x,y)=x+3y-3=0\nG(x,y)=2x+4y-3=0 \n Error bound = 1.0e-14\n"); printf("\nINPUT THE NAME OF THE OUTPUT FILE\n"); scanf("%s",FOUTN); if((FOUT=fopen(FOUTN,"w"))==NULL){ printf("FILE OPEN ERROR...\n"); getch(); exit(-1); } /* Input the Initial Value xi & yi */ printf("INPUT THE VALUE OF xi\n xi="); scanf("%f",&xi); printf("INPUT THE VALUE OF yi\n y=i"); scanf("%f",&yi); NEWTON();/* Call the general routine */ /* Print the root */ printf(“ Root: x= %f , y= %f",x,y); fprintf(FOUT,"Root: x= %f , y= %f",x,y); fclose(FOUT); getch(); } void NEWTON(){ float xnum,ynum,err; /* Equation: F(x,y)=x+3y=0 , G(x,y)=2x+4t=0 */ /* Define value of variable */ err=1.0e-14; x=xi; y=yi; do{ F();FX();FY(); G();GX();GY(); det=fx*gy-fy*gx; xnum=(f*gy)-(fy*g); ynum=(fx*g)-(f*gx); if(det!=0){ x=x-(xnum/det); y=y-(ynum/det); } else{ printf("Failure!!!"); getch(); exit(-1); } } while(fabs(xnum/det)>err || fabs(ynum/det)>err); } 5
Source Program •Newton’s method to solve equations of two variables. void F(){ f=x+3*y-3; } void FX(){ fx=1; } void FY(){ fy=3; } void G(){ g=2*x+4*y-3; } void GX(){ gx=2; } void GY(){ gy=4; }
Result • Newton’s method for solving an equation of one variable. • Newton’s method to solve equations of two variables.