function [node, elem] = nodes_check(node,elem, max_length, min_length)

n_node=size(node,1);
n_elem=size(elem,1);
[temp, temp2]=size(node);
DOF=temp2-1;
n_eqn=DOF*n_node;

% backup
node_temp=node;
elem_temp=elem;

for i=1:n_elem;
    node1=elem(i,2);
    node2=elem(i,3);

    %Positions of the ends of the element
    x1=node(node1,2);
    y1=node(node1,3);
    x2=node(node2,2);
    y2=node(node2,3);

    %The length of the element, sin, cos
    l = lenangle(x1,y1,x2,y2);
    elem_temp(i,4)=l;
end


% remove node related to min element length
[min_v, row]=min(elem_temp(:,4));
if min_v < min_length; % merge too small element with its neighbor element
    [min_v, row]=min(elem_temp(:,4));
    size_elem_temp=size(elem_temp,1);

    if row==1;
        node_temp( elem_temp(row,2):size_elem_temp-1, 2:3)...
            =node_temp( elem_temp(row,2)+1:size_elem_temp, 2:3);
        a=elem_temp(row+1:size_elem_temp,4);
        elem_temp(row+1:size_elem_temp,4)=zeros;
        elem_temp(row:size_elem_temp-1,4)=elem_temp(row:size_elem_temp-1,4)+a;

    elseif row==length(elem_temp(:,1));
        node_temp( elem_temp(row,3):size_elem_temp-1, 2:3)...
            =node_temp( elem_temp(row,3)+1:size_elem_temp, 2:3);
        a=elem_temp(row+1:size_elem_temp,4);
        elem_temp(row+1:size_elem_temp,4)=zeros;
        elem_temp(row:size_elem_temp-1,4)=elem_temp(row:size_elem_temp-1,4)+a;

    else
        if elem_temp(row-1,4) < elem_temp(row+1,4);  % compare the lengths of 2 neighbor elements
            %select 1st node of the element with minimun length
            node_temp( elem_temp(row,2):size_elem_temp-1, 2:3)...
                =node_temp( elem_temp(row,2)+1:size_elem_temp, 2:3);
            a=elem_temp(row+1:size_elem_temp,4);
            elem_temp(row:size_elem_temp,4)=zeros;
            elem_temp(row:size_elem_temp-1,4)=elem_temp(row:size_elem_temp-1,4)+a;

        else
            node_temp( elem_temp(row,3):size_elem_temp-1, 2:3)...
                =node_temp( elem_temp(row,3)+1:size_elem_temp, 2:3);
            a=elem_temp(row+1:size_elem_temp,4);
            elem_temp(row+1:size_elem_temp,4)=zeros;
            elem_temp(row:size_elem_temp-1,4)=elem_temp(row:size_elem_temp-1,4)+a;
        end
    end
    node_temp2=node_temp(1:size_elem_temp-1,:);
    clear node_temp;
    node_temp=node_temp2;
    elem_temp2=elem_temp(1:size_elem_temp-1, :);
    elem_temp2(size_elem_temp-1, 3)=1;
    clear elem_temp;
    elem_temp=elem_temp2;
end


% add node related to maximum length
[max_v, row]=max(elem_temp(:,4));
if max_v > max_length; % merge too small element with its neighbor element

    [max_v, row]=max(elem_temp(:,4));
    size_elem_temp=size(elem_temp,1);
    clear a;
    % insert node
    node_temp2=zeros(size_elem_temp+1,3); % since node size = size_elem_temp
    node_temp2(1:row,1:3)=node_temp(1:row,1:3);
    node_temp2((row+2):size_elem_temp+1,1:3)=node_temp((row+1):size_elem_temp,1:3);
    node_temp2((row+1),2)=0.5*(node_temp2(row,2)+(node_temp2((row+1),2))); % x
    node_temp2((row+1),3)=0.5*(node_temp2(row,3)+(node_temp2((row+1),3))); % y
    for i=1:size_elem_temp+1
        node_temp2(i,1)=i;
    end
    % update element
    elem_temp2=zeros(size_elem_temp+1,4);
    elem_temp2(1:size_elem_temp,1:3)=elem_temp(:,1:3);
    for i=1:row-1
        elem_temp2(i,4)=elem_temp(i,4);
    end
    for i=row+2:size_elem_temp+1
        elem_temp2(i,4)=elem_temp(i-1,4);
    end
    elem_temp2(row,4)=(1/2)*elem_temp(row,4);
    elem_temp2(row+1,4)=(1/2)*elem_temp(row,4);

end

% update
clear node; % remove old value
clear elem; % remove old value

elem=elem_temp(:,1:3);
node=node_temp;