What is the logic behind independent variables in symbolic functions

3 次查看(过去 30 天)
Can anybody explain me how matlab symbolic functions uses the indipendend variables?
In some more complicated problems i get serious trouble because sometimes I have to use the function(t) to not get errors and some other times i don't have to use '(t)", and i cant see the pattern behind it. It becomes very annoying with the use of vector functions where i have to access a certain element.
An example to show what i mean:
syms a(t) b(t) c(t)
u(t)=a(t)*b(t)
v =b(t)*c(t)
The result is
u(t) =
a(t)*b(t)
v =
b(t)*c(t)
like it would expect it. But look at the following
X=u*v
Y=u(t)*v
result_without_t= X*Y
result_with_t= X(t)*Y
that becomes now strange to me:
X(t) =
a(t)*b(t)^2*c(t)
Y =
a(t)*b(t)^2*c(t)
without_t(t) =
a(t)^2*b(t)^4*c(t)^2
with_t =
a(t)^2*b(t)^4*c(t)^2
Even though i didnt use the '(t)' in my expression, matlab creates it (see X), and on the other hand when i type it (see Y) it doesnt appear. Now look at the next lines of code: In without_t i dont use 't' even if X has one according to Matlab - so its similar to the X calculation - and then there matlab creats the t.
Maybe my description is confusing, just look at the result by yourself.
I really dont see the logic behind it, especially for nested commands and crossproducts it becomes unpredicable. It makes a big difference for functions vectors! Accessing a element in a vector is impossible if there is the (t) dependency. Then i have to reassign the variable to a new variable one without t, change the value of 1 element and turn in back for the calculations with t dependency and hope that everything is like it was before - if matlab is missing (t) he gives following error:
Error using symfun/subsref Symbolic function indexing evaluates the function at the inputs. To perform colon indexing call FORMULA before indexing.
- What is the logic of the use of (t)?
- How do i change a single element in a symbolic vector function keeping this restrictions in mind?

回答(2 个)

Walter Roberson
Walter Roberson 2012-11-19
I have not used the MuPAD Symbolic Toolbox, and have not seen much written about using syms to define a function, so I am going to have to give an interpretation here.
It looks to me that when you use syms to define a function, then when you reference the function name without indicating any parameters, that the parameter names you gave in the syms command are always added on automatically (except perhaps on the left side of an assignment, and I would never expect them to be added inside a string.)
When you reference the function name and give something in () afterwards, then you are doing parameter substitution, so u(t) is the function "u" with the actual argument "t" substituted for its first parameter (which happens to be "t"). For example,
syms p; u(p) + u
I suspect would be would be a(p) * b(p) + u(t)
Now, add to this that
v = b(t) * c(t)
is defining the symbol v to be b(t) * c(t), with the fixed parameter "t", rather than defining a function. v(r) would then be (b(t) * c(t))(r)
and everything you show in your test follows.
The error message you are getting is saying that if you want u(t) to mean indexing, then you need to use formula(u)(t) where "formula" is a fairly new MuPAD function. It might be, though, that you have to assign the result of formula(u) to a variable and index that, as I do not know if MATLAB has special-cased formula() to allow indexing immediately afterwards.

Stefan
Stefan 2012-11-19
Thank you so far!
Now while working I found pretty much the perfect example
syms a(t) b(t) c d e f
cross ([a(t) b(t) c], [d e f])
e=a*b
cross ([a(t) b(t) c], [d e(t) f])
It works only exactly this way, as soon as I change (t) at ANY place it crashes with the same error. Unluckily formula is pretty useless, just returns the function
formula(e(t))
ans =
a(t)*b(t)
Seriously, how can somebody work with this toolkit, i really hope there is a hidden logic behind it... I can not adjust the code, just depending on what multiplications and operations were made in the past! Any help is welcome, right now i'm working on trial-and-error...

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by