MATLAB: Why is A undefined? How to fix it

errorundefined

Root(x1) Root (x2) Error
--------------------------------------------------------------------------------
0.0000000000000000 0.0000000000000000 7.6393202250e-01
Undefined operator '/' for input arguments of type 'function_handle'.
Error in gauss_newton1>@(x,y)[((x-x1)/(S1)),((y-y1)/(S1));((x-x2)/(S2)),((y-y2)/(S2));((x-x3)/(S3)),((y-y3)/(S3))]
Error in gauss_newton1 (line 41)
A = dfxy(x1_old,x2_old) ;
close all
clear
clc
format long e
h = 10^(-14);
B = -1 ;
C = -0.5 ;
D = - 0.5 ;
x1 = -1;
x2 = 1;
x3 = 1;
y1 = 0;
y2 = 0.5;
y3 = -0.5;
fx1 = @(x,y) sqrt((x-x1)^2 + (y-y1)^2) + B;
fx2 = @(x,y) sqrt((x-x2)^2 + (y-y2)^2) + C ;
fx3 = @(x,y) sqrt((x-x3)^2 + (y-y3)^2) + D ;
S1 = @(x,y) sqrt ((x-x1)^2 + (y-y1)^2) ;
S2 = @ (x,y) sqrt ((x-x2)^2 + (y-y2)^2) ;
S3 = @(x,y) sqrt ((x-x3)^2 + (y-y3)^2) ;
dfxy = @(x,y) [((x-x1) / (S1)) , ((y-y1) / (S1)) ; ((x-x2) / (S2)) , ((y-y2) / (S2)) ; ((x-x3) / (S3)) , ((y-y3) / (S3))] ;
x0 = 0 ; y0 = 0 ;
K = 10^5;
tol = 10^(-14);
e = zeros(1,K);
e(1) = ((fx1(x0, y0))^2 + (fx2(x0, y0))^2 + (fx3(x0, y0))^2);
x1_old = x0;
x2_old = y0;
fprintf('\t\t\t\t Root(x1) \t\t\t\t\t\t Root (x2) \t\t\t\t\t Error \t\t\t \n')
fprintf('\t\t\t --------------------------------------------------------------------------------\n')
fprintf('\t\t\t %0.16f \t\t\t %0.16f \t\t\t %2.10e \t\t\t \n', x1_old, x2_old, e(1))
for j = 2:K
A = dfxy(x1_old,x2_old) ;
%v = -((A').*A)\(A').*e(j);
%e(j) = abs(fx1(A(1,1), A(2,1)) - fx2(A(1,1), A(2,1)));
x = x + V ;
r = [fx1(x1_old,x2_old) , fx2(x1_old,x2_old), fx3(x1_old,x2_old)];
e(j) = r'*(A);
if e(j) < tol
x1_old = A(1,1);
x2_old = A(2,1);
fprintf('\t\t\t %0.16f \t\t\t %0.16f \t\t\t %2.10e \t\t\t \n', x1_old, x2_old, e(j))
else
fprintf('\t\t\t %0.16f \t\t\t %0.16f \t\t\t %2.10e \t\t\t \n', A(1,1), A(2,1), e(j))
fprintf('\t\t\t Total number of Iterations %4d \n',j-1)
break
end
end
semilogy(1:j, e(1:j), '-ro')
xlabel('Iterations')
ylabel('log_{10}(Error)')

Best Answer

It is necessary to evaluate your functions in order to operate on the results they return. Call them as functions, not function handles. It is also necessary to vectorize (use element-wise operations) on multiplication, division, and exponentiation here.
Try this:
dfxy = @(x,y) [((x-x1) ./ (S1(x,y))) , ((y-y1) ./ (S1(x,y))) ; ((x-x2) ./ (S2(x,y))) , ((y-y2) ./ (S2(x,y))) ; ((x-x3) ./ (S3(x,y))) , ((y-y3) ./ (S3(x,y)))] ;
That should at least eliminate the error you reported.