MATLAB: Feval() with fun as a vector

feval eval arrayfunMATLAB

Hi, I am looking how to use feval with the "fun" string input as a vecor
F=[f1,f2,...,fn]
This is in order tu compute the Jacobian
---------------------------------
function y = test(x)
y=[x(1)^3+x(2)^3 , x(1)];
---------------------------------
------------------------------------
function g = jacobian(fun, x0)
delta = 0.001;
for i = 1 : min(size(x0))
for j = 1 : max(size(x0))
x=x0;
x(j) = x0(j);
aux1 = arrayfun ( fun(i), x );
x(j) = x0(j) + delta;
aux2 = feval ( fun(i), x );
g(i,j)=(aux2-aux1)/(delta);
end
end
end
----------------------------------
And I call the function like this
jacobian('test',x)
with x=[1,1]
The problem is in fun(i), that gives a "t" from "test", I would like to take only the "i" element of the vector.
Do you have a clue?
arrayfun and eval do the same
Regards

Best Answer

Provided that I have understood your requirements...
fbase = str2fun(fun);
ith = @(v,i) v(i);
for i = 1 : min(size(x0))
fi = @(x) ith(fbase(x), i);
for j = 1 : max(size(x0))
[...]
aux1 = fi(x);
[...]
aux2 = fi(x);
[...]
end
end