How to substitute x1,...,xn at once?

4 次查看(过去 30 天)
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 个评论
John D'Errico
John D'Errico 2019-8-10
编辑:John D'Errico 2019-8-10
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
madhan ravi 2019-8-10
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by