I was trying to create a array of symfun
, so that I may later access those functions and perform the diff
operation w.r.t to the specific variables, I have searched and found the code as:
syms x
f = symfun([x1^2+x2-x3; x2+x3^2; x1*x2], x);
But is not the thing that I am looking for, this snippet is creating a symfun
out of an array, but I need to create an array of symfun
. So that if I have n
symfun
stored in an array and also have n
variables stored in an array then want to create a matrix with the following rule:
[[diff(func_1, x1) diff(func_1, x2) ...... diff(func_1, xn)]
[diff(func_2, x1) diff(func_2, x2) ...... diff(func_2, xn)]
.
.
.
.
[diff(func_n, x1) .......................... diff(func_n, xn)]]
And here is my code:
function[K] = bigPopaPump()
x1 = sym('x1')
x2 = sym('x2')
f1 = symfun(3*x1+2, x1)
f2 = symfun(8*x2+5, x2)
funcs = [f1, f2]
xess = [x1, x2]
dummy_array = zeros(2, 2)
for i = 1:size(funcs)
for j = 1:size(funcs)
dummy_array(i, j) = diff(funcs(i), xess(j));
end
end
display dummy_array
end
I assume you mean
syms x1 x2 x3
f = symfun([x1^2+x2-x3; x2+x3^2; x1*x2], [x1 x2 x3])
which returns
f(x1, x2, x3) =
x1^2 + x2 - x3
x3^2 + x2
x1*x2
Similarly, this returns identical output:
syms x1 x2 x3
f = [symfun(x1^2+x2-x3, [x1 x2 x3]);
symfun(x2+x3^2, [x1 x2 x3]);
symfun(x1*x2, [x1 x2 x3])]
If you want an array of symfun
's then you'll need to use cell array. The reason for this is that a symfun
is effectively a function handle. One must use cell arrays rather than arrays to group function handles too.
For your example:
syms x1 x2 x3
f = {symfun(x1^2+x2-x3, [x1 x2 x3]);
symfun(x2+x3^2, [x1 x2 x3]);
symfun(x1*x2, [x1 x2 x3])}
or
syms x1 x2 x3
f = arrayfun(@(fx)symfun(fx,[x1 x2 x3]),[x1^2+x2-x3; x2+x3^2; x1*x2],'UniformOutput',false)
returns
f =
[1x1 symfun]
[1x1 symfun]
[1x1 symfun]
You can then evaluate the first function, for example, via f{1}(2,3,4)
.
See also this related question.