How to substitute x1,...,xn at once?
4 views (last 30 days)
Show older comments
Hello
Consider the following code:
X=sym('x',[1,5]);
syms(X);
A=[cos(x1)+cos(x2) sin(x2)*sin(x5) x1^2*x3 x4+x5 x5+3];
How do you substitute A(1) to x1, A(2) to x2, ... ?
So next time you write x1, it displays cos(x1)+cos(x2).
4 Comments
John D'Errico
on 10 Aug 2019
Edited: John D'Errico
on 10 Aug 2019
HUH? You are asking
"So next time you write x1, it displays cos(x1)+cos(x2)."
You want x1 to be cos(x1) + cos(x2)? Do you recognize this is a self reference? An infinite, implicit loop?
Sorry. This makes no sense. Please look at what you wrote, at what you are asking to do, and consider if it makes any sense to you. If it till makes sense to you, then explain far more clearly what you are trying to do.
My guess is that what you want to do is to create a set of numbered variables, thus y1, y2, y3, y4, y5, such that y1 contains cos(x1) + cos(x2). And you already have A as a vector, such that A(1) contains exactly what you want. There is no need to write something like y1, when A(1) already exists and is easily accessed. Learn to use vectors and vector indexing. Your code will benefit greatly.
So it you want to create that set of variables y1,...,y5, that is just terribly poor programming style. You never want to create large sets of numbered variable names. It will lead to buggy code, terribly difficiult code to read and write.
madhan ravi
on 10 Aug 2019
Just realised , agree with John D'Errico
"How do you substitute A(1) to x1, A(2) to x2, ... ?
So next time you write x1, it displays cos(x1)+cos(x2)."
Both are different goals and is unclear atm.
Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!