Message Boards Message Boards

0
|
4303 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:
GROUPS:

how to use subscript here?

I have a numerical solution such as:

In: sol = NSolve[Eqn, Flatten[Array[Subscript[Fc, ##] &, {Ns - 1, Nx}]]]
Out: {{Subscript[Fc, 1, 1] -> 37559.9, Subscript[Fc, 1, 2] -> 18969.8, 
  Subscript[Fc, 1, 3] -> 11399.8, Subscript[Fc, 1, 4] -> 7452.95, 
  Subscript[Fc, 2, 1] -> 22983.4, Subscript[Fc, 2, 2] -> 11693.3, 
  Subscript[Fc, 2, 3] -> 7065.16, Subscript[Fc, 2, 4] -> 4639.3}}

I, then, want to place the values of the solution in another matrix:

In: Fcsol = (Array[Subscript[Fc, ##] &, {Ns - 1, Nx}] /. sol)[[1]]
Out: {{37559.9, 18969.8, 11399.8, 7452.95}, {22983.4, 11693.3, 7065.16, 
  4639.3}}

Finally, my post-solution function requires:

In: Subscript[Fcsol, 2, 1] 
Out: Subscript[{{37559.9, 18969.8, 11399.8, 7452.95}, {22983.4, 11693.3, 
  7065.16, 4639.3}}, 1, 1]

However, it should have resulted in

Out: 37559.9

Could you please help? Thanks.

4 Replies

To extract an entry from a matrix the standard way is this: Fcsol[[2,1]] or

Part[Fcsol,2,1]

Subscript is inert by itself. It is designed mainly for display, not for action.

POSTED BY: Gianluca Gorni

Thanks, Gianluca. Although, my problem is not resolved yet. I created a simple code that generates the same warning messages:

Eqn = {};
For[ns = 1, ns <= 2, ns++, 
  For[nx = 1, nx <= 2, nx++, 
   AppendTo[Eqn, 2*ns*Part[X, ns, nx] == nx^2]]];
Eqn
Flatten[Table[Part[X, i, j], {i, 1, 2}, {j, 1, 2}]]
NSolve[Eqn, Flatten[Table[Part[X, i, j], {i, 1, 2}, {j, 1, 2}]]]

Although I get the final solution, there are many warning messages which I want to get rid of:

Part::partd: Part specification X[[2,1]] is longer than depth of object. >>

{{X[[1, 1]] -> 0.5, X[[1, 2]] -> 2., X[[2, 1]] -> 0.25, 
  X[[2, 2]] -> 1.}}

Any idea to better code this problem?

You cannot use Part[X, i, j] as a variable in an equation, because it is a command to extract an element from the value of the symbol X, which has no value right now. As a good variable you can use for example X[i, j] (or, as you did before, Subscript[X,i,j], but that is more complicated to manage). You can then collect the solutions into a separate variable, from which you can extract parts:

Eqn = {};
For[ns = 1, ns <= 2, ns++, 
  For[nx = 1, nx <= 2, nx++, AppendTo[Eqn, 2*ns*X[ns, nx] == nx^2]]];
Eqn
MatrixForm[Table[X[i, j], {i, 1, 2}, {j, 1, 2}]]
First[NSolve[Eqn, Flatten[Table[X[i, j], {i, 1, 2}, {j, 1, 2}]]]]
MatrixForm[Xmatrix = Array[X, {2, 2}] /. %]
Part[Xmatrix, 1, 1]
POSTED BY: Gianluca Gorni

thanks.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract