Karim
Karim

Reputation: 302

How to extract a matrix of symbolic functions in Matlab

syms c  A(t) v(t)
A(t) =
            0
 c*sin(tt(t))
 c*cos(tt(t))

How I can get X = A(2) = c*sin(tt(t)); (the function at second row)? If I type A(2), the result will be as below (it substitutes a constant for the function, which is not my desire):

>> A(2)
ans =
            0
 c*sin(tt(2))
 c*cos(tt(2))

Upvotes: 1

Views: 499

Answers (2)

horchler
horchler

Reputation: 18484

The problem is that you've defined A as a symbolic function (symfun), not as an array of symbolic expressions. Instead:

syms c A tt(t)
A = [0;
     c*sin(tt(t));
     c*sin(tt(t))];

Now A(2) will return c*sin(tt(t)).

Alternatively, if you can't change the definition of A(t), you'll need to assign it to an intermediate variable to convert it to an array of symbolic expressions:

syms c  A(t) tt(t)
A(t) = [0;
        c*sin(tt(t));
        c*cos(tt(t))];
B = A(t);

Then, B(2) will return c*sin(tt(t)). You can also use formula to extract the underlying expressions:

B = formula(A):

Upvotes: 1

Alexis Kowalczuk
Alexis Kowalczuk

Reputation: 46

In matlab you must to use "subs(f)" function to evaluate functions.

First create the function:

syms g(x)
g(x) = x^3;

After that asign the X value:

x=2;

then if you evaluate g using the subs function, the result is the expected value 8, but it is assigned to a symbolic function, gnew. This new symbolic function formally depends on the variable x.

gnew = subs(g)

The function call, g(x), returns the value of g for the current value of x. For example, if you assigned the value 2 to the variable x, then calling g(x) is equivalent to calling g(2)

g2 = g(x)

g2 =
4

g2 = g(2)

g2 =
4

Upvotes: 0

Related Questions