How to use a returning vector function inside another returning vector function ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to use a returning vector function inside another returning vector function ?

Adhanedhel
I have the following toy-code

%%%%%%%%%%%%%%%%
function test

        for i=1:2

                x(i,1)=rand();
                dx(i,1)=rand();

        end


        for epsi=10.^[-1:-1:-5]
               
                epsi
                Norm = norm((G(x+epsi*dx,z)-G(x-epsi*dx,z))/(2*epsi) - dG(x,z)*dx)
                z
        end

end



%---------------------------------------------
%---------------------------------------------
%---------------------------------------------


function Res = G(x,z)

for i=1:2
        Res(i,1) = x(i,1)^2+z(i,1);
end
       
end

function Res=dG(x)

for i=1:2
        Res(i,i) = 2*x(i,1);
end
       
end



function Res = z

        for i=1:2
                Res(i,1) = 10*i*y(i,1);
        end
end

function Res = y

        for i=1:2
                Res(i,1) = i;
        end
end

%%%%%%%%%%%%%%%%%%%%

The errors are :

error: z: =: nonconformant arguments (op1 is 1x1, op2 is 2x1)
error: called from
    testy>z at line 48 column 12
    testy at line 14 column 6

My goal here is to use the "vector-function" y inside the function z which is itself used in G, to check that dG is the jacobian matrix of G (of course here is a simple example where it is useless to check,  which is not the case of my real functions).

Here the variable for my jacobian is only the vector x, and y and z are side functions.

Can you help me, and explain me how to write properly what i want to do ?

Adhanedhel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to use a returning vector function inside another returning vector function ?

Colin Macdonald-2
On 2017-07-31 07:54 AM, Adhanedhel wrote:
> Norm = norm((G(x+epsi*dx,z)-G(x-epsi*dx,z))/(2*epsi) - dG(x,z)*dx)

At the end of this line, dG and dx are both vectors so "*" gives an error.

Perhaps you mean "norm(dG(...))*norm(dx)"?   Or an inner product like
"(dG')*dx", or "dot(dG, dx)", or ... etc

To me, it looks like you are trying to compare the finite difference to
the directional derivative.   One approach:

   dirderiv = ... % exercise for reader
   finitediff = G(x+epsi*dx,z)-G(x-epsi*dx,z))/(2*epsi);
   Norm = norm(finitediff) - norm(dirderiv)

best,
Colin

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Loading...