0
$\begingroup$

I want to use indexed given that s is an element [0,500] but I am unsure how to write that without getting a format error or a tensor error.

\[CapitalDelta]t = .0001;
t = .0833;
\[Sigma] = .2183;
\[CapitalDelta]s = 5;
s = [0, 500];
\[Mu] = ((\[Sigma]^2 Indexed[s, i]^2)/
    Indexed[\[CapitalDelta]s, i]^2*\[CapitalDelta]t);
\[Alpha] = (Indexed[s, i]/(
   2*Indexed[\[CapitalDelta]s, i]^2*\[CapitalDelta]t));
cn1[k2_, n_] = 
 SparseArray[{{m_, m_} -> 
    1/2 + 1/2*\[Mu] + 
     1/2*Indexed[rate, {k2, n}]*\[CapitalDelta]t, {m_, l_} /; 
     l - m == 1 -> -(1/4)*\[Mu] - 
     1/2*Indexed[rate, {k2, n}]*\[Alpha], {m_, l_} /; 
     m - l == 1 -> -(1/4)*\[Mu] + 
     1/2*Indexed[rate, {k2, n}]*\[Alpha]}, {101, 101}]
$\endgroup$
  • $\begingroup$ One of the many points is that you write Indexed[\[CapitalDelta]s, i] while \[CapitalDelta]s equals 5. That just does not make sense because you can index only into lists or arrays. Also s = [0, 500]; is meaningless in Mathematica. Maybe you mean s = {0, 500};. Also, I am quite sure that you should use cn1[k2_, n_] := ... (SetDelayed) instead of cn1[k2_, n_] = ... (Set). $\endgroup$ – Henrik Schumacher Dec 23 '19 at 8:33
  • $\begingroup$ I have corrected your points, thank you! My first question is, after making those changes and commenting out the Delta S, there is no out. Although, in terms of the \[CapitalDelta]=5, the reason I did this is because when I derived my equations, I found some s_i and some \[CapitalDelta]s and I assumed for every S I needed to do it in terms of time hence why I added the delta in the question. You are saying it does not make sense because it is only single value which defeats the purpose of indexed? @HenrikSchumacher $\endgroup$ – Cherry Dec 23 '19 at 8:41
  • $\begingroup$ "You are saying it does not make sense because it is only single value which defeats the purpose of indexed? " Yes. $\endgroup$ – Henrik Schumacher Dec 23 '19 at 8:51
0
$\begingroup$

I find the pattern-based way of using SparseArray not overly helpful in practice, but that's probably a matter of taste. However, you might find this way to construct the matrix easier. I am using just any constants for \[CapitalDelta]t, \[Mu], and \[Alpha].

This is what you run once:

\[CapitalDelta]t = .0001;
\[Mu] = 1.;
\[Alpha] = 1.;

A = Plus[
   DiagonalMatrix[SparseArray[ConstantArray[1/2 + \[Mu]/2, 101]]],
   DiagonalMatrix[SparseArray[ConstantArray[ -\[Mu]/4, 100]], 1],
   DiagonalMatrix[SparseArray[ConstantArray[ -\[Mu]/4, 100]], -1]
   ];

B = Plus[
   DiagonalMatrix[
    SparseArray[ConstantArray[ \[CapitalDelta]t/2, 101]]],
   DiagonalMatrix[SparseArray[ConstantArray[-\[Alpha]/2, 100]], 1],
   DiagonalMatrix[SparseArray[ConstantArray[ \[Alpha]/2, 100]], -1]
   ];

And in each time step of Crank-Nicolson (I guess you are about to implement Crank-Nicolson, right), you obtain the matrix you seek for with

L = A + Indexed[rate, {k2, n}] B
|improve this answer|||||
$\endgroup$
  • $\begingroup$ Ah yes I was having an issue combining my two sparse arrays! Thank you so much for this clarification! Although I did have one question, why is it that indexed stays in the sparse array you get as an output for L? I assumed that, yes, the output would be sectioned off into four parts: namely (1,2),(1,1),(2,1), and (2,3) but it does not combine lets say the 1/2+_ and rather just prints it out next to the indexed function @Henrik Schumacher $\endgroup$ – Cherry Dec 23 '19 at 9:06
  • $\begingroup$ Also, since the code has changed wouldn't the k2 change as well? since it is no longer defined @HenrikSchumacher $\endgroup$ – Cherry Dec 23 '19 at 9:13
  • $\begingroup$ Well, it is hard for me to help you here because I do not know why you introduced the parameters k2 and n in the first place. That's really something that you have to make your mind up about for yourself. $\endgroup$ – Henrik Schumacher Dec 23 '19 at 9:38
  • $\begingroup$ "I assumed that, yes, the output would be sectioned off into four parts: namely (1,2),(1,1),(2,1), and (2,3) [...]" Sorry, I absolutely do not understand what you mean. "[...] rather just prints it out next to the indexed function [...]" Same problem here. But maybe your issue is simply that rate is still undefined and not a matrix? $\endgroup$ – Henrik Schumacher Dec 23 '19 at 9:39
  • $\begingroup$ I introduced k2 and n because those were supposed to be independent variables that represent sequence number and time state, respectively. Also, I meant to say that for example one section of an output I got was (1,2)->-(1.19x10^-6Indexed[{0,5000},{i}]^2)/(Indexed[5,i]^2). Because the word indexed is printing out in my code, does that mean there is an issue with the iterative method of S_i . Also if I had questions on how to add my \[CapitalDelta]s should I make a new thread for that? @HenrikSchumacher $\endgroup$ – Cherry Dec 23 '19 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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