Message Boards Message Boards

Error using Solve[ ] for an equation?

Posted 2 years ago

Could you please help me?

n = Input["n=?"];
h = 2/n;

Table[u[0, j] = 0, {j, 1, n}];
Table[u[n, j] = j*h*(2 - j*h), {j, 1, n}];
Table[u[i, 0] = 0, {i, 1, n}];
Table[u[i, n] = i h, {i, 0, Floor[n/2]}];
Table[u[i, n] = 2 - i h, {i, Floor[n/2 + 1], n}];

eq = Table[
  u[i + 1, j] + u[i - 1, j] + u[i, j + 1] + u[i, j - 1] - 4*u[i, j] ==
    0, {i, 1, n - 1}, {j, 1, n - 1}]
eq1 = Flatten[eq]
var = Table[u[i, j], {i, 1, n - 1}, {j, 1, n - 1}];
Solve[eq1, var]

Error : Solve::ivar: {u[1,1],u[1,2]} is not a valid variable.
POSTED BY: mahdi safarbali
5 Replies
Posted 2 years ago

var needs to be a vector.

Solve[eq1, Flatten@var]
(*
{{u[1, 1] -> 7/64, u[1, 2] -> 47/224, u[1, 3] -> 145/448, 
  u[2, 1] -> 51/224, u[2, 2] -> 13/32, u[2, 3] -> 131/224, 
  u[3, 1] -> 177/448, u[3, 2] -> 135/224, u[3, 3] -> 39/64}}
*)
POSTED BY: Rohit Namjoshi

Thank you. I changed my code as follow:

eq1 = Flatten[eq]
var = Table[u[i, j], {i, 1, n - 1}, {j, 1, n - 1}];
var1 = Flatten[var];
Solve[eq1, var1]

but I got these errors :

Flatten::normal: Nonatomic expression expected at position 1 in Flatten[eq].
[out]: Flatten[eq]
Table::iterb: Iterator {i,1,-1+n} does not have appropriate bounds.
Solve::naqs: Flatten[eq] is not a quantified system of equations and inequalities.
POSTED BY: mahdi safarbali
Posted 2 years ago

What is eq? I cannot reproduce the error

n = 4;
h = 2/n;

Table[u[0, j] = 0, {j, 1, n}];
Table[u[n, j] = j*h*(2 - j*h), {j, 1, n}];
Table[u[i, 0] = 0, {i, 1, n}];
Table[u[i, n] = i h, {i, 0, Floor[n/2]}];
Table[u[i, n] = 2 - i h, {i, Floor[n/2 + 1], n}];

eq = Table[
  u[i + 1, j] + u[i - 1, j] + u[i, j + 1] + u[i, j - 1] - 4*u[i, j] ==
    0, {i, 1, n - 1}, {j, 1, n - 1}]
eq1 = Flatten[eq]
var = Table[u[i, j], {i, 1, n - 1}, {j, 1, n - 1}];
Solve[eq1, Flatten@var]

eq1 = Flatten[eq]
var = Table[u[i, j], {i, 1, n - 1}, {j, 1, n - 1}];
var1 = Flatten[var];
Solve[eq1, var1]

Result is the same.

POSTED BY: Rohit Namjoshi

No I mean I changed just a part of my code. my entire code is : Clear[u]; c = 1; n = Input["n=?"]; h = 2/n;

Table[u[0, j] = 0, {j, 1, n}]; Table[u[n, j] = jh(2 - j*h), {j, 1, n}]; Table[u[i, 0] = 0, {i, 1, n}]; Table[u[i, n] = i h, {i, 0, Floor[n/2]}]; Table[u[i, n] = 2 - i h, {i, Floor[n/2 + 1], n}];

eq = Table[ u[i + 1, j] + u[i - 1, j] + u[i, j + 1] + u[i, j - 1] - 4*u[i, j] == 0, {i, 1, n - 1}, {j, 1, n - 1}] eq1 = Flatten[eq] var = Table[u[i, j], {i, 1, n - 1}, {j, 1, n - 1}]; var1 = Flatten[var]; Solve[eq1, var1] when I run this code, I got the error as I wrote in previous text.

POSTED BY: mahdi safarbali

It is solved. Thanks for your response.

POSTED BY: mahdi safarbali
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