0
$\begingroup$

How to make simple iteration in Mathematica for this three equations and save $q$ for each step. If we define $q'=dq/dx$, $q''=dq'/dx$ we have two equations

(1) $q''[j+1]=a_1(q[j+1]-q[j])-a_3q'[j]-a_5q''[j]$,

(2) $q'[j+1]=a_2(q[j+1]-q[j])+a_4q'[j]+a_6q''[j]$,

And we should substitute in third

(3) $q[j+1]=Q_2q[j]+Q_3q'[j]+Q_4q''[j]$

$a_i$ and $Q_k$ ($i=1,\ldots,6$; $k=1,2,3$) are functions of $\delta(x)$. Where $\delta(x)=0.01$ for example. For each $\delta(x)$ I need $q$.

Regards,

  • 0
    Should this really be here, and not a programming forum?2011-08-29
  • 0
    As it stands, it's unanswerable since you didn't say what the ai and Qk are. Are you trying to do Runge-Kutta by any chance?2011-08-30
  • 0
    Could you write the original differential equations?2011-08-30
  • 0
    GEdgar, yes it should be here, because it is a numerical method for solving differential equation2011-08-30
  • 0
    J.M. I wrote that ai and Qk are the function of the x I mean (delta x). This is not a Runge Kutta. habitmelon original differential equation is R1*q''(x)+R2*q'[x]+R3*q[x]=F(x), Ri contants, but I don`t want another method for solving. Just help about programing in mathematica for simple iteration which I wrote.2011-08-30

1 Answers 1

3

You can just use a For loop, following the manual. Equation 3 has no [j+1] on the right, so should be calculated first. Then equations 1 and 2 can be calculated as you already have q[j+1].

For[j=0,j
  • 1
    *Mathematica* also supports so-called "dynamic programming": e.g. `f[0] = 1; f[n] := f[n] = n f[n-1]` for the factorial, which I believe the OP wanted to do for his particular system. Still, the question is less than clear to me...2011-08-30
  • 0
    @J.M.: That should read `f[0] = 1; f[n_Integer?Positive] := f[n] = n f[n-1]`2011-08-30
  • 0
    @Simon: Yes, I forgot the `_` (and the head-checking for guaranteed correctness); thanks!2011-08-30
  • 0
    Dear Ross Millikan how can I set the step of delta{x}, because for example I have a2=1/delta{x}. I need from [0,1] for each delta{x} with step 0.01 value of q. Regards!2011-08-31
  • 0
    Usually your steps are in x, not q. You choose delta x, say as .01, or choose 1000 steps in [0,1] and find delta x is .001. Then the steps in q are calculated. You start with initial conditions q(0), q'(0). Usually your iteration starts with q'' as a function of q, q' and x. Then you step up to q'(delta x) and q(delta x). Then you use your equation to get q''(2 delta x), q'(2 delta x), q(2 delta x), etc.2011-08-31
  • 0
    Ok I determined from initial conditions first iteration, and I have q,q'q'' for x=0. Then how to write code in Mathematica step for x=0.01 for example and determine all values of q for each x? I must change something in your code above? Because I am not programmer. Thank you very much on suggestions.2011-08-31
  • 0
    The loop above is just for that. Index j is the steps and x=j delta x. So x=0.01 would be j=1, x=0.02 would be j=2, etc. The first line says do this with j=0, then increment j by 1 and repeat, until j hits end. In your case, if you want to stop at x=1, end would be 100 (=1/delta x)2011-08-31
  • 0
    Thank you very much Mr Millikan, I will try.2011-08-31
  • 0
    But if it is a1=5/delta{x} I must type a1=5/j? and also I must put initial value in form q[0]=0.5; for example?2011-08-31
  • 0
    Usually delta x is the step size and is fixed. Your parameters then often do not depend upon which step you are on. So does a1 change when x goes from 0 to 0.5? If not, it should not depend on j. You might look at a text on integrating differential equations. I like Numerical Recipes, available at nr.com (and obsolete versions are free), chapter 16.2011-08-31
  • 0
    Take a look here http://pastebin.com/raw.php?i=9RUZYPHp Copy paste from here in your Mathematica sheet. a1, a2 and a3 are changeable. So for each step I need also new a1, a2 and a3. I wasn't clear. My mistake!2011-08-31
  • 0
    Because Subscript[a, 1] = 1/(\[Beta]*x^2); Subscript[a, 2] = \[Gamma]/(\[Beta]*x); Subscript[a, 3] = 1/(\[Beta]*x); [Beta] and [Gamma] are constants, like a4, a5...2011-08-31
  • 0
    Then you recalculate a1, a2, and a3 each step using x=j delta x (or j+1/2 delta x if you want to center it on the step). It looks like you have a6 depending on j as well, so move that into the loop. The basic idea is you calculate things that don't change outside the loop, while things that do change with x inside the loop.2011-08-31
  • 0
    Yes, but how to define a1, like a1=1/(beta*x) or a1=1/(beta*j). It doesn't work for both. Please, check my .nb file what is wrong with writing?2011-09-01
  • 0
    What are the units of a1 and beta? j is unitless-a step number. x is a length.2011-09-01
  • 0
    Beta constant. To simplify the problem, I had three equations q''=..., q'=..., q=... And q is a function of x. (q'=dq/dx,q''=dq'/dx). I had for x=0 from initial conditions q[0]. Let for example q[0]=0.2. Now I need to determine q for each x. a1=1/(beta*x^2), a3... Original differential equations are here http://pastebin.com/raw.php?i=cchka9PV2011-09-01
  • 0
    If you can't get it to work, you could start very simple: try q'=a q, q(0)=1, which we know is solved by q=exp(x). Then try q'= 2x q so the coefficient depends on x,with q(0)=1 this is exp(x^2). Your syntax is different from what I am used to, as in Subscript, the [] around variables, etc2011-09-01
  • 0
    Ok, if we let the a1, a2, Q ... all constans even q. This code doesn't work again For[j=0,j2011-09-01
  • 0
    So did it work? You didn't need qpp as these are first order equations. The advantage is that we know the answer so can check.2011-09-01
  • 0
    No, nothing. Here is the original file http://www.sendspace.com/file/xha9z02011-09-01