Non linear Newton Raphson Method for 3 variable simultaneous equations solved without 'syms'.
clear all;
clc;
f=@(x,y,z)((x^2)*y+y*x+x*z-9);
g=@(x,y,z)((x^2)*(y^2)-x*y*z+y*z-4);
k=@(x,y,z)((x^2)+(y^2)+(z^2)-30);
x0=input('Enter first approximation for X:');
y0=input('Enter first approximation for Y:');
z0=input('Enter first approximation for Z:');
x(1)=x0;
y(1)=y0;
z(1)=z0;
tol=0.000001;
N=1000000;
sqrteps = sqrt(eps);
h=sqrteps;
for n=1:N
f_x=((f(x(n)+h,y(n),z(n))-f(x(n)-h,y(n),z(n)))./(2*h));
f_y=((f(x(n),y(n)+h,z(n))-f(x(n),y(n)-h,z(n)))./(2*h));
f_z=((f(x(n),y(n),z(n)+h)-f(x(n),y(n),z(n)-h))./(2*h));
g_x=((g(x(n)+h,y(n),z(n))-g(x(n)-h,y(n),z(n)))./(2*h));
g_y=((g(x(n),y(n)+h,z(n))-g(x(n),y(n)-h,z(n)))./(2*h));
g_z=((g(x(n),y(n),z(n)+h)-g(x(n),y(n),z(n)-h))./(2*h));
k_x=((k(x(n)+h,y(n),z(n))-k(x(n)-h,y(n),z(n)))./(2*h));
k_y=((k(x(n),y(n)+h,z(n))-k(x(n),y(n)-h,z(n)))./(2*h));
k_z=((k(x(n),y(n),z(n)+h)-k(x(n),y(n),z(n)-h))./(2*h));
f0=f(x(n),y(n),z(n));
g0=g(x(n),y(n),z(n));
k0=k(x(n),y(n),z(n));
A=[f_x f_y f_z;g_x g_y g_z;k_x k_y k_z];
B=[f0;g0;k0];
if det(A)==0
break;
end
AB=[A,B];
AB=rref(AB);
h1=AB(1,4);
h2=AB(2,4);
h3=AB(3,4);
x(n+1)=x(n)-h1;
y(n+1)=y(n)-h2;
z(n+1)=z(n)-h3;
if (abs(x(n+1)-x(n))<=tol && abs(y(n+1)-y(n))<=tol && abs(z(n+1)-z(n))<=tol)
break;
end
end
fprintf('\nThe root of the equation is x=%0.5f, y=%0.5f, z=%0.5f \n No of Iteration=%d',x(n+1),y(n+1),z(n+1),n)
Comments
Post a Comment