MATLAB: How to write a single line code for ‘n’ number of functions.

MATLAB: How to write a single line code for ‘n’ number of functions.

looping in funtions

I would like to write a single line code for ‘n’ number of functions.
For example (here ‘v’ and ‘theta’ are variables)
f(1)= v(1)cos(theta(1));
f(2)= v(2)cos(theta(2));
f(3)= v(3)cos(theta(3));
.
.
f(n)= v(n)cos(theta(n));
I wrote my code as
n=10;
syms v theta
for i=1:n
f(i)= v(i)*cos(theta(i));
end
it gave the following errors
Index exceeds matrix dimensions.
Error in sym/subsref (line 814)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in Untitled2 (line 6)
f(i)= v(i)*cos(theta(i));
Would appreciate your kind help.
Regards, Mustafa

Best Answer

  • For at least the last 15 years, the only time that you can have an array of functions indexed with () is if the functions are symbolic. However if you do create an array of symbolic functions indexed by () then it is difficult to extract the individual functions without accidentally triggering executing all of the functions with the index as the parameter.
    So… if you need an array of functions, you should use a cell array of function handles.
    Next, you are missing some information on how symbolic variables work. You have
    n=10;
    syms v theta
    for i=1:n
    f(i)= v(i)*cos(theta(i));
    end
    The syms you give there is the same in part as
    v = sym('v')
    Notice this assigns the scalar value sym(‘v’) to v. With it being a scalar, you can reference v(1) and pull back that sym(‘v’) scalar. But then when you try v(2) then it would attempt to index the scalar sym(‘v’) at offset 2, and that would fail because it is only length 1.
    Any time you have a scalar symbol name in MATLAB, like v after the
    syms v
    then MATLAB will treat it as a scalar. There is no way in MATLAB to declare that “This symbol will refer to a vector of length n, and I just want you to use the symbol and array indexing notation in the formulae.” There is also no way to use a symbol to index something. Instead, each time you have a symbol indexed, the array has to exist at that location and the value stored there will be brought into the equation.
    What you can do is:
    n=10;
    v = sym('v', [1 n]);
    theta = sym('theta', [1 n]);
    for i=1:n
    f{i} = v(i)*cos(theta(i));
    end
    and the result of that would be a cell array that contains symbolic expressions, with the first expression containing v1*cos(theta1) and the second expression containing v2*cos(theta2) and so on to v10*cos(theta10) . Notice that these are no longer indexed references: the value stored at the index was looked up and used in the expression.
    This is not really so bad, because you can later do things like
    Pi = sym('pi');
    v_vals = rand() * 2 * Pi;
    subs(f{8}, v, v_vals)
    Here, because v is a vector of symbol names, that would expand to
    subs(f{8}, [v1, v2, v3, v4, v5, v6, v7, v8, v9, v10], v_vals)
    which would instruct it to replace v1 with v_vals(1), v2 with v_vals(2) and so on. It is an arrangement that is mostly usable — but it does make it difficult to program symbolic functions that are intended to accept arrays of different sizes.
  • Add a Comment

    Your email address will not be published. Required fields are marked *