フィルターのクリア

Substitute vector of values for symbolic variable within a function?

7 ビュー (過去 30 日間)
Michael Ziedalski
Michael Ziedalski 2018 年 2 月 25 日
コメント済み: Walter Roberson 2018 年 6 月 12 日
So, using a really long Taylor expansion, I managed to generate a long function of 5 symbolic variables in Matlab, but now I want to replace one of those variables with a vector, so that the function outputs one value when evaluated on values of the other symbolic var.s and on any specific index of said vector.
For some context; normally, I can create a function of symbolic and vector values like this.
fun = @(x,data) 1/sqrt(2*pi) * exp(-(data(:,2)-(x(1)+x(2)*data(:,1)).^2)/2);
Where data happens to be a two-column vector.
For my current function to replace a var with a data vector, I tried doing
pX = subs(pX, "old var.", data(1:end))
but it just keeps running for forever, making me think something is wrong. How would I do this, guys? I would appreciate any help in this matter.

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 2 月 26 日
I advise against assigning a numeric value to a variable that has been used in a symbolic expression. The chances are too high that you will mix up whether the variable is symbolic or numeric.
People tend to expect that when they assign a numeric value to a variable that has been used in a symbolic expression, that suddenly the expression will start evaluating as if the symbol had been assigned the numeric value, but that is not how it works -- just like when you do
a = 1;
b = a + 1;
a = 2;
then b does not suddenly become 2+1: the value of a at the assignment is copied and used in the assignment. Just so,
syms a
b = a + 1;
a = 2;
the value of a at the time of the assignment is copied and used in b.
What I recommend is that you use a different variable name to hold the numeric value, and then subs() that in. So perhaps assign to Data instead of to data, and then
subs(Px, data, Data)
the (1:end) is redundant by the way.
It is possible for a subs() to take a long time to execute. The symbolic engine is not always able to get closed form solutions for an expression involving a symbolic variable, but when you substitute in a particular numeric value, then it might be able to create the closed form solution -- which could take a lot of time. For example, the exact value might provide enough information to perform a numeric integral, or might provide enough information to be able to find roots (which can be fairly expensive when a floating point exponent gets converted to a rational values and you have to suddenly find 2^53 complex roots of an expression...)
  10 件のコメント
Yu Tsai Chen
Yu Tsai Chen 2018 年 6 月 12 日
Sorry for an maybe unrelated question.
Is it possible to replace a symbolic function with a vector? For example, use syms theta(t) for representing a theta of time, then do the partial derivative and come out with an equation, now i need to solve the equation but there seems to be some difficulties.
The purpose is to solve the Lagrange equation, but there are six symbolic function in it. I cannot use "dsolve" for these six equation (with conditions but it says that the number of indeterminates can't match with the equations or it would come out with an empty answer) . I also tried assumption (assume some of the symbolic variables to be bound in a range, i.e. 1 <= theta(t) <= 5, and it says that it cannot assume symbolic function, only symbolic variables or expressions) and substitution (subs theta(t) with a vector composed of values, run the equation again and it didn't change). There are first-order and second-order differential of six symbolic function in those six equations.
I'm not sure which step i go wrong. Hope to get some positive response. Thanks!
Walter Roberson
Walter Roberson 2018 年 6 月 12 日
Yu Tsai Chen,
Please start a new Question for that, posting your equations and any relevant constraints.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by