1

I am trying to evaluate a MatlabFunction with a single vector, meaning that this vector would hold a value for each variable in that MatlabFunction.

To use a simple example, I can define a MatlabFunction as such :

syms x [3 1];

f1 = matlabFunction((x1 + x2)*x3, 'var', sym2cell(x));

This defines a symbolic variable x that holds three symbolic variables : x1 x2 x3 and a MatlabFunction equals to @(x1,x2,x3)x3.*(x1+x2)

I am able to calculate the gradiant of f1 on point a with :

a = [1; 0; 0];
gradf = gradient(mf, x);
gradiant_of_a = double(subs(graf, x, a));

What bothers me is that I cannot do the same for :

f1_of_a = double(subs(f1, x, a));

I've seen that subs won't work on function handle. But it does on symbolic objects, which gradf is for example.

Is there any solution to this problem ?

I found answers telling to go with f1_of_a = f1(1, 0, 0);, but this doesn't suits me since I am using MatlabFunctions with an unknown number of variable. (size of x and a could be (1, 3), or (1, 10) and should still work for example, and I won't be typing by hand the 10 arguments in the second case)

Thank you for any help about this !

MaxiBerdy
  • 11
  • 1
  • do you have to use symbolic variables? why not just calculate it numerically? You are already using the built-in gradient function anway – Thales Dec 18 '19 at 20:00
  • This one might also be relevant: https://stackoverflow.com/questions/26832620/passing-a-vector-to-a-matlabfunction-without-num2cell – Cris Luengo Dec 18 '19 at 20:08
  • I would prefer to, because I am making functions that take those Matlab function to some kinds of algorithms, and I find it very easy to define these kind of function, instead of making a particular and arbitrary choice of data representation.. But I'm not asking for relevance of my work, just about how to do it ! – MaxiBerdy Dec 18 '19 at 21:55

0 Answers0