2
$\begingroup$

I have these set of equations. The number of equations depends on $n,$ which I want to keep control of. The code that I have generates it using Table and equations generated by this does not take an input. I want to generate same equations as functions.

n = 5; 
h = 1/n;
x[0] = 0;
x[n] = 1;
xs = Table[x[k], {k, 1, n - 1}];
eqFs = Table[x[k] - 1/(h^2 + 2)*(x[k - 1] + x[k + 1] - h^2*Cos[((x[k + 1] - x[k - 1])/(2*h))^2]), {k, 1, n - 1}]; 

The command eqFs print the equations. But I want something like

xs = Array[x,5]
eqFs[xs]

should results same equations.

$\endgroup$

3 Answers 3

3
$\begingroup$
$Version

(* "13.3.1 for Mac OS X ARM (64-bit) (July 24, 2023)" *)

Clear["Global`*"]

The pure function is

eqFs = (#[[2]] - 
       1/(h^2 + 2)*(#[[1]] + #[[3]] - 
          h^2*Cos[((#[[3]] - #[[1]])/(2*h))^2])) & /@ Partition[#, 3, 1] &;

n = 5;
h = 1/n;

Define xs to include all values of x

x[0] = 0;
x[n] = 1;
xs = Array[x, n + 1, 0];

Then,

eqFs[xs]

(* {x[1] - 25/51 (-(1/25) Cos[(25 x[2]^2)/4] + x[2]), 
 x[2] - 25/51 (-(1/25) Cos[25/4 (-x[1] + x[3])^2] + x[1] + x[3]), 
 x[3] - 25/
   51 (-(1/25) Cos[25/4 (-x[2] + x[4])^2] + x[2] + x[4]), -(25/
    51) (1 - 1/25 Cos[25/4 (1 - x[3])^2] + x[3]) + x[4]} *)
$\endgroup$
3
  • $\begingroup$ See here for pure functions. $\endgroup$
    – user64494
    Commented Oct 17, 2023 at 19:20
  • $\begingroup$ @user64494 - What is your point? As shown in the link which you provided, body& is a pure function. Alternatively, just look at the FullForm of the definition above for eqFs and you will see that it is Function[ ... ] $\endgroup$
    – Bob Hanlon
    Commented Oct 17, 2023 at 19:41
  • $\begingroup$ Yes, and I voted up your answer. I find it useful to give a reference, $\endgroup$
    – user64494
    Commented Oct 19, 2023 at 5:03
2
$\begingroup$

I would start by decomposing your problem into manageable pieces. The basic structure of the computation you want given the signature you specified would look something like this:

eqFsNew[{xs__}] := BlockMap[TBD, {0, xs, 1}, 3, 1]

Let's try it on your example to see what basic structure results:

eqFsNew[Array[x, 4]] (* Since your semantics would set x[5] to 1, we don't need to include that. *)
(* {TBD[{0, x[1], x[2]}], TBD[{x[1], x[2], x[3]}], TBD[{x[2], x[3], x[4]}], TBD[{x[3], x[4], 1}]} *)

So, TBD needs to be a function that takes a list of three values and produces the expression you've used in your eqFs. There's a slight hitch, though, because TBD needs to know h, or equivalently n since h just derives from n. But n can be derived from the length of the argument to eqFsNew. So, let's parameterize TBD:

eqFsNew[{xs__}] := BlockMap[TBD[1 + Length[{xs}]], {0, xs, 1}, 3, 1]
eqFsNew[Array[x, 4]]
(* {TBD[5][{0, x[1], x[2]}], TBD[5][{x[1], x[2], x[3]}], TBD[5][{x[2], x[3], x[4]}], TBD[5][{x[3], x[4], 1}]} *)

Okay, now we just need to define TBD and make sure it follows the structure we've come up with. I'll leave it up to you what TBD should actually be named.

TBD[n_][{x1_, x2_, x3_}] :=
  With[
    {h = 1/n},
    x2 - 1/(h^2 + 2)*(x1 + x3 - h^2*Cos[((x3 - x1)/(2*h))^2])]

And now,

eqFsNew[Array[x, 4]]
(* {x[1] - (25*(-1/25*Cos[(25*x[2]^2)/4] + x[2]))/51, 
    x[2] - (25*(-1/25*Cos[(25*(-x[1] + x[3])^2)/4] + x[1] + x[3]))/51, 
    x[3] - (25*(-1/25*Cos[(25*(-x[2] + x[4])^2)/4] + x[2] + x[4]))/51, 
    (-25*(1 - Cos[(25*(1 - x[3])^2)/4]/25 + x[3]))/51 + x[4]} *)
$\endgroup$
2
  • $\begingroup$ See here and here for pure functions. $\endgroup$
    – user64494
    Commented Oct 17, 2023 at 19:14
  • $\begingroup$ @user64494 You can stop pestering everyone with these links to pure functions. We all know about "pure functions" as Wolfram defines the term. We also know that Wolfram's use of the term is not standard in the domains of math or software. So, just because the OP uses the term "pure function", it cannot therefore be concluded that the OP only wants anonymous functions. The OP may want something like a mathematical function (in math we don't need the "pure" modifier) or a software pure function (a function that depends only on its arguments and has no side-effects). $\endgroup$
    – lericr
    Commented Oct 17, 2023 at 19:28
0
$\begingroup$
Clear[eqFs, x, n, h]

eqFs[x_] := 
 Table[x[[k]] - 
   1/(h^2 + 2)*(x[[k - 1]] + x[[k + 1]] - 
      h^2*Cos[((x[[k + 1]] - x[[k - 1]])/(2*h))^2]), {k, 2, Length[x] - 1}]

xs = Array[x, 6, 0]
eqFs[xs]
$\endgroup$
3
  • $\begingroup$ See here for pure functions. $\endgroup$
    – user64494
    Commented Oct 17, 2023 at 15:07
  • 4
    $\begingroup$ @user64494 Wolfram uses "pure function" in a non-standard way in that documentation. I don't think we can always assume that a poster means what Wolfram means by "pure function". $\endgroup$
    – lericr
    Commented Oct 17, 2023 at 15:57
  • $\begingroup$ @Domen Thank you $\endgroup$
    – Learner
    Commented Oct 17, 2023 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.