In: Computer Science
Modify/write your Newton-Raphson program in c language for single nonlinear equation to solve the following nonlinear system
f1(x,y)=x3+y−1 = 0
f2(x,y)=y3−x+1 = 0
using the Newton-Raphson method with initial solutions x(0) = 0.5 and y(0) = 0.5 and the convergence criterion max(|f1(x, y)|, |f2(x, y)|) < ε = 10−6.
//Program for Newton-Raphson method on Non-linear
equations
#include<stdio.h>
#include<math.h>
#define ESP pow(10,-6) //Here the Epsilon value define that is
10^-6
#define X(x,y) pow(x,3) + (y) - 1 //Define the first equation
f1(x,y)=x^3+y-1
#define Y(x,y) pow(y,3) - (x) + 1 //Define the second equation
f2(x,y)=y^3-(x)+1
#define F1(x,y) 3*(x)*(x) //Here calculate the derivative of first
equation with respect to x
#define G1(x,y) -1 //Here calculate the derivative of second
equation with respect to x
#define F2(x,y) 1 //Here calculate the derivative of first equation
with respect to y
#define G2(x,y) 3*(y)*(y) //Here calculate the derivative of second
equation with respect to y
void main()
{
double x1,y1,y2,x2,x,y,f,g,f1,f2,g1,g2;
int i=1;
printf("\nEnter the initial value of x: ");
scanf("%lf",&x);
printf("\nEnter the initial value of y: ");
scanf("%lf",&y);
f=X(x,y);
g=Y(x,y);
printf("\n\nf = %lf",f);
printf("\n\ng = %lf",g);
f1=F1(x,y);
f2=F2(x,y);
g1=G1(x,y);
g2=G2(x,y);
printf("\n\nf1 = %lf",f1);
printf("\n\ng1 = %lf",g1);
printf("\n\nf2 = %lf",f2);
printf("\n\ng2 = %lf",g2);
x1= x-((f*g2-g*f2)/(f1*g2-f2*g1));
y1= y-((g*f1-f*g1)/(f1*g2-f2*g1));
printf("\n\nx = %lf",x1);
printf("\n\ny = %lf",y1);
printf("\n\n");
do
{
f=X(x1,y1);
g=Y(x1,y1);
printf("\n\nf = %lf",f);
printf("\n\ng = %lf",g);
f1=F1(x1,y1);
f2=F2(x1,y1);
g1=G1(x1,y1);
g2=G2(x1,y1);
printf("\n\nf1 = %lf",f1);
printf("\n\ng1 = %lf",g1);
printf("\n\nf2 = %lf",f2);
printf("\n\ng2 = %lf",g2);
x2= x1-((f*g2-g*f2)/(f1*g2-f2*g1));
y2= y1-((g*f1-f*g1)/(f1*g2-f2*g1));
printf("\n\n x = %lf ",x2);
printf("\n\n y = %lf ",y2);
printf("\n\n");
if(fabs(x1-x2)<ESP && fabs(y1-y2)<ESP)
{
printf("\n\nREAL ROOT1 for x value = %.3lf",x1);
printf("\n\nREAL ROOT2 for y value = %.3lf",y1);
i=0;
}
else
{
x1=x2;
y1=y2;
}
}while(i!=0);
}
OUTPUT:-