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
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
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 Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!