Search code examples
functionmatlabsystemequation

Input vector to a symbolic system of functions


Suppose that I have written a function containing 3 separable functions(a system of equations). I want to calculate the value of this function for 3 different values of my variables but I do not want to use "subs" function. What I want to do is enter a vector containing the desired values of my variables and calculate the main function which is a vector. How could I do that?. Notice that I do not want to call the function by each variable. Here is my code:

syms x y z
f1 = symfun(x.^2+3.*x.*y,[x,y,z]);
f2 = symfun(z.^3+y-x.^3-12,[x,y,z]);
f3 = symfun(2*z+x.*y+z.*x+1,[x,y,z]);

f = [f1;f2;f3];

What I mean is to calculate the f function by for example: f([12 4 6]) not byf(12 4 5)


Solution

  • Not the most elegant, but the best I could think of at the moment is to put it in a function wrapper. This might be one way to pass inputs as an array. This will bridge the gap between indexing the array input to be passed into the symfcn (symbolic functions).

    f([12 4 6])
    
    
    function [Results] = f(Inputs)
    syms x y z
    f1(x,y,z) = x.^2+3.*x.*y;
    f2(x,y,z) = z.^3+y-x.^3-12;
    f3(x,y,z) = 2*z+x.*y+z.*x+1;
    
    Sym_Functions = [f1;f2;f3];
    Results = Sym_Functions(Inputs(1),Inputs(2),Inputs(3));
    end