Diana
Diana

Reputation: 363

Undefined function or variable 'x'

I am trying to evaluate two matrixes which I defined outside of the function MetNewtonSist using subs and I get the error Undefined function or variable 'x' whenever I try to run the code.

[edit] I added the code for the GaussPivTot function which determines the solution of a liniear system.

syms x y
f1 = x^2 + y^2 -4;
f2 = (x^2)/8 - y;
J = jacobian( [ f1, f2 ], [x, y]);
F = [f1; f2];
subs(J, {x,y}, {1, 1})
eps = 10^(-6);
[ x_aprox,y_aprox, N ] = MetNewtonSist( F, J, 1, 1, eps )

function [x_aprox, y_aprox, N] = MetNewtonSist(F, J, x0, y0, eps)

    k = 1;
    x_v(1) = x0;
    y_v(1) = y0;

    while true

        k = k + 1;

        z = GaussPivTot(subs(J, {x, y}, {x_v(k-1), y_v(k-1)}),-subs(F,{x, y}, {x_v(k-1), y_v(k-1)}));
        x_v(k) = z(1) + x_v(k-1);
        y_v(k) = z(1) + y_v(k-1);

        if norm(z)/norm([x_v(k-1), y_v(k-1)]) < eps
            return 
        end
    end

    N = k;
    x_aprox = x_v(k);
    y_aprox = y_v(k); 

end

function [x] = GaussPivTot(A,b)

n = length(b);
A = [A,b];
index = 1:n;

for k = 1:n-1

    max = 0;

    for i = k:n
        for j  = k:n
            if A(i,j) > max
                max = A(i,j);
                p = i;
                m = j;
            end
        end
    end

    if A(p,m) == 0

        disp('Sist. incomp. sau comp. nedet.')
        return;

    end

    if p ~= k

        aux_line = A(p,:);
        A(p,:) = A(k, :);
        A(k,:) = aux_line;

    end

    if m ~= k

        aux_col = A(:,m);
        A(:,m) = A(:,k);
        A(:,k) = aux_col;
        aux_index = index(m);
        index(m) = index(k);
        index(k) = aux_index;

    end

    for l = k+1:n

            M(l,k) = A(l,k)/A(k,k);
            aux_line = A(l,:);
            A(l,:) = aux_line - M(l,k)*A(k,:);

    end

end

if A(n,n) == 0

    disp('Sist. incomp. sau comp. nedet.')
    return;

end

y = SubsDesc(A, A(:,n+1));

for i = 1:n

    x(index(i)) = y(i);

end

end


Upvotes: 0

Views: 801

Answers (1)

Sardar Usama
Sardar Usama

Reputation: 19689

By default, eps is defined as 2.2204e-16 in MATLAB. So do not overwrite it with your variable and name it any word else.

epsilon = 1e-6;

Coming to your actual issue, pass x and y as input arguments to the MetNewtonSist function. i.e. define MetNewtonSist as:

function [x_aprox, y_aprox, N] = MetNewtonSist(F, J, x0, y0, epsilon, x, y)
%added x and y and renamed eps to epsilon

and then call it with:

[x_aprox, y_aprox, N] = MetNewtonSist(F, J, 1, 1, epsilon, x, y);

Upvotes: 1

Related Questions