フィルターのクリア

2D Finite Difference Method

6 ビュー (過去 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 Center および File ExchangeMathematics and Optimization についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by