2D Finite Difference Method

12 views (last 30 days)
%% Finite Difference Method
syms x1 x2
x1_0 = 1;
x2_0 = 1;
tol = [0.0001;0.0001];
h = 0.1;
a = zeros(2,10);
a(1,1) = x1_0;
a(2,1) = x2_0;
f1 = @(x1,x2)(x1^2 +x2 -5);
f2 = @(x1,x2)(x2^2 -x1);
tic
for i = 1:100
K_11 = (f1(a(1,i)+h,a(2,i))- f1(a(1,i)-h,a(2,i)))/(2*h);
K_22 = (f2(a(1,i)+h,a(2,i))- f2(a(1,i)-h,a(2,i)))/(2*h);
K_12 = (f1(a(1,i),a(2,i)+h)- f1(a(1,i),a(2,i)-h))/(2*h);
K_21 = (f2(a(1,i),a(2,i)+h)- f2(a(1,i),a(2,i)-h))/(2*h);
J = [K_11 K_12; K_21 K_22];
a(:,i+1) = a(:,i) - inv(J)*[f1(a(1,i),a(2,i));f2(a(1,i),a(2,i))]; %%error line
if abs([(f1(a(1,i+1),a(2,i+1))); f2(a(1,i+1),a(2,i+1))]) < tol
fprintf('x1=%.8f & x2 = %.8f',a(1,i+1),a(2,i+1))
fprintf('no of itterations %d',i)
break
end
end
t = toc
I am trying to find roots for f1 and f2 with finite difference method. But I am getting this erro "Warning: Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN. > In NR (line 47)" the line 47 i have mentioned as %%error line in code.
same question i have done with 2D newton method and within 5 itteration i got the answer. (x1 = 1.90278368 & x2 = 1.37941426).
please guide me through this.
Thanks in advance.

Accepted Answer

Torsten
Torsten on 18 Oct 2022
K_11 = (f1(a(1,i)+h,a(2,i))- f1(a(1,i)-h,a(2,i)))/(2*h);
K_22 = (f2(a(1,i),a(2,i)+h)- f2(a(1,i),a(2,i)-h))/(2*h);
K_12 = (f1(a(1,i),a(2,i)+h)- f1(a(1,i),a(2,i)-h))/(2*h);
K_21 = (f2(a(1,i)+h,a(2,i))- f2(a(1,i)-h,a(2,i)))/(2*h);
instead of
K_11 = (f1(a(1,i)+h,a(2,i))- f1(a(1,i)-h,a(2,i)))/(2*h);
K_22 = (f2(a(1,i)+h,a(2,i))- f2(a(1,i)-h,a(2,i)))/(2*h);
K_12 = (f1(a(1,i),a(2,i)+h)- f1(a(1,i),a(2,i)-h))/(2*h);
K_21 = (f2(a(1,i),a(2,i)+h)- f2(a(1,i),a(2,i)-h))/(2*h);
  1 Comment
Dhruvilkumar Chavada
Dhruvilkumar Chavada on 21 Oct 2022
Thanks a lot; now its showing same result as newtons method

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!