2D Finite Difference Method

조회 수: 12 (최근 30일)
Dhruvilkumar Chavada
Dhruvilkumar Chavada 2022년 10월 18일
댓글: Dhruvilkumar Chavada 2022년 10월 21일
%% 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.

채택된 답변

Torsten
Torsten 2022년 10월 18일
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
Dhruvilkumar Chavada
Dhruvilkumar Chavada 2022년 10월 21일
Thanks a lot; now its showing same result as newtons method

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by