29 views (last 30 days)

Show older comments

Hi All,

Please help to solve the following equations by the Newton–Raphson method:

fx1 = (2*x1^2) + (x2^2) -10;

fx2 = (x1^2) - (x2^2) + (x1*x2) - 4;

Start with an initial guess of x1 = 1 and x2 = 1., y =0

My code as below:

clc;clear;close all;

syms x1 x2 fx1 fx2 y1 y2;

fx1 = (2*x1^2) + (x2^2) -10;

fx2 = (x1^2) - (x2^2) + (x1*x2) - 4;

Fx = [fx1;fx2]

J11 = diff(fx1,x1);

J12 = diff(fx1,x2);

J21 = diff(fx2,x1);

J22 = diff(fx2,x2);

JJ = [J11 J12 ; J21 J22]

JJ1=[J11; J12]

JJ2=[J21 ; J22]

y1 = 0;

y2 = 0;

YY=[y1;y2]

x1(1) = 1;

x2(1) = 1;

X = [1;1];

epsilon = 0.001;

n = 10;

func1 = inline(Fx(1))

func2 = inline(Fx(2))

M = diff(sym(Fx(1)))

N = diff(sym(Fx(2)))

DF1 = inline(M)

DF2 = inline(N)

for i=1:4

X(i+1)=X(i)+(YY-func1(X(i)))

end

Thanks in advance

Walter Roberson
on 11 Jan 2021

Do not use inline(). Use matlabFunction() to convert symbolic expressions into anonymous functions.

Divija Aleti
on 25 Jan 2021

Hi,

Have a look at the following code which solves the above mentioned equations by Newton-Raphson Method :

clc;clear;close all;

syms fx1(x1,x2) fx2(x1,x2)

fx1(x1,x2) = (2*x1^2) + (x2^2) -10;

fx2(x1,x2) = (x1^2) - (x2^2) + (x1*x2) - 4;

Fx = [fx1;fx2]

J11 = diff(fx1,x1);

J12 = diff(fx1,x2);

J21 = diff(fx2,x1);

J22 = diff(fx2,x2);

JJ = [J11 J12 ; J21 J22];

epsilon = 0.001;

x1_o = 1;

x2_o = 1;

x = [x1_o];

y = [x2_o];

for i=1:10

h = det([-fx1(x(i),y(i)) J12(x(i),y(i)); -fx2(x(i),y(i)) J22(x(i),y(i))])/det(JJ(x(i),y(i)));

k = det([J11(x(i),y(i)) -fx1(x(i),y(i)); J21(x(i),y(i)) -fx2(x(i),y(i))])/det(JJ(x(i),y(i)));

x(i+1) = x(i) + h;

y(i+1) = y(i) + k;

if abs(x(i+1)-x(i)) <= epsilon && abs(y(i+1)-y(i)) <= epsilon

break

end

end

% The two roots are :

x_1 = x(i+1)

x_2 = y(i+1)

For additional information on 'syms', refer to the following link:

Regards,

Divija

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!