2D Finite Difference Method

6 次查看(过去 30 天)
%% 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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics and Optimization 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by