PS: Note that the symfun call above is identical to: clearĪnd this is how you would normally create a symbolic function. There is no way around this, except, as suggested in an answer to the post I linked at the top, to create an anonymous function that evaluates your symbolic expression: clear However, this creates a symbolic function where each of the elements of A is a separate input argument, which is not what you want. To create a symbolic function, you can do: clearį = symfun(A(1) + A(2)*t + A(3)*t^2, ) It computes the difference between subsequent array elements, t times. Here, t and A are just the input arguments, and can be filled in by anything.Ĭreates another function handle to an anonymous function, this time the function will evaluate x=F(t,A), then call diff(x,t), where diff is the normal function, not the one in the symbolic toolbox. It is completely unrelated to the symbolic variables t and A, and it is not a symbolic function. You are creating a function handle to an anonymous function.
Your code shows some misconceptions around symbolic functions and anonymous functions. The linked post goes in great detail explaining why and how the symbolic engine in MATLAB works. This all reflects a fundamental internal limitation in the symbolic engine: the very insides of the symbolic engine have no provision for describing or operating on an "array" whose contents will be filled in later.
You cannot create a symbolic function that uses an array as input: Your question is answered over at MATLAB Answers. Yet the form of inputs of symbolic function seems not so direct nor friendly. This is why I want to use symfun to define a function that has too many parameters. The matlabFunction runs much slower than symfun, and diff(.) can't admit numerical results. However, the form of inputs seems not so friendly. It is obvious that only dF2 is a symbolic function. Symbolic function expected 4 inputs and received 2. Now, type in the command window, > dF1(t,A) The testing codes are as follows: syms t A a0 a1 a2ĭF2(t,A) = diff( F(t,A), t ) % Same as using symfunĭF3 = matlabFunction( diff( F(t,A), t ), 'Vars', ) , A(100) ), suppose there are 100 parameters
To save the effort of typing, ony-by-one, A(.) in dF( 0, A(1), A(2). Is there a simpler way to make this direct evaluation work using the direct form dF(0,A) rather than dF(0, A(1).
However, it must be in the form of dF( 0, A(1), A(2), A(3) ) to evaluate Eq.3 rather than dF(0,A) This way, it allows direct evaluation at t=0 Then I tried to use the expression shown below: Eq.3 dF(t,A) = diff( F(t,A), t ) "The second argument must either be variables or a variable" The derivatives wrt theta1 all return 0, while the ones wrt theta2 return a value that looks correct.Suppose I have a simple function handle defined as Eq.1 F = A(1) + A(2)*t + A(3)*t^2 Īnd I'd like to define another function handle from the differentiation of F, such as Eq.2: dF = diff( F(t,A), t ) īut, it seems forbidden to evaluate dF at some specific t, such as dF(0,A), and an error occurs as
I2 = 1/12*eye(3) % Moment of Inertia link 2 I1 = 1/12*eye(3) % Moment of Inertia link 1 Am I applying something wrong? See code - % Set conditions given in problem statment For at least one derivative, I can confirm that its not generating the correct derivative, yet all other steps seem correct. I've been using the symbolic tool box to make sure I don't make a mistake coping long equations from hand. I'm working through a problem where I need to perform algebraic differentiations.