Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Iteration in Mathematica

  1. May 12, 2012 #1
    What I want to do with Mathematica is this:

    1. Given are 4×1 matrices e and u, and 4×4 matrix V. Matrices e and V are constant.
    2. Solve the system u=V.f for f, where f is 4×1 matrix.
    3. Define 4×1 vector g such as g1=f1*Exp(e1*t), g2=f2*Exp(e2*t) etc, where g1 is the first component of g, f1 is the first component of f, e1 is the first component of e etc and t=0.1 or 1 or whatever small number.
    4. Compute p, where p=V.g
    5. Print p and t*(the number of current step in the iteration)
    6. Redefine u=p and do it all over again.

    Mathematica gives examples where only one expression is to be computed, while I want to compute more expressions in one step. I have no idea how to tell Mathematica to do this. Any idea will be appreciated. You can also give me (if you know one) a link or a title of a book where such kind of problems are explained.

    Please help :)
     
  2. jcsd
  3. May 12, 2012 #2
    Ok, I have found out how iterations could be made with function Do. But one problem I can't solve is this: how to tell Mathematica the new input to be equal to the last output? It seems that Mathematica doesn't change the input at all. Also, how to print the number of the step of the iteration?
     
  4. May 12, 2012 #3
    Here is my code. At the end I have written u:=p, to tell Mathematica to start the next step with last result. But it won't :(

    Do[u := ({
    {152100000},
    {0},
    {0},
    {29290}
    }); f = LinearSolve[V, u]; g := ({
    {Part[f, 1]*Exp[Part[e, 1]*10]},
    {Part[f, 2]*Exp[Part[e, 2]*10]},
    {Part[f, 3]*Exp[Part[e, 3]*10]},
    {Part[f, 4]*Exp[Part[e, 4]*10]}
    }); p := V.g; Print[p]; u := p, {10}]
     
  5. May 12, 2012 #4
    Fix a number of mistakes

    In[1]:= e={1.,2.,3.,4.};
    V={{1,2,6,3},{6,1,5,4},{3,5,2,7},{8,4,5,1}};
    u={152100000,0,0,29290};
    Do[
    f=LinearSolve[V,u];
    g={Part[f,1]*Exp[Part[e,1]*10], Part[f,2]*Exp[Part[e,2]*10], Part[f,3]*Exp[Part[e,3]*10], Part[f,4]*Exp[Part[e,4]*10]};
    p=V.g;
    Print[p];
    u=p,
    {10}
    ]

    From In[1]:= {-3.796667*^24, -5.063140*^24, -8.862560*^24, -1.264638*^24}
    From In[1]:= {-8.941114*^41, -1.192148*^42, -2.086260*^42, -2.980371*^41}
    From In[1]:= {-2.104606*^59, -2.806142*^59, -4.910749`*^59, -7.01535*^58}
    From In[1]:= {-4.953934*^76, -6.605245*^76, -1.155917*^77, -1.651311*^76}
    From In[1]:= {-1.166083*^94, -1.554777*^94, -2.720860*^94, -3.886943*^93}
    From In[1]:= {-2.744787*^111, -3.65971*^111, -6.40450*^111, -9.14929*^110}
    From In[1]:= {-6.460826*^128, -8.61443*^128, -1.50752*^129, -2.15360*^128}
    From In[1]:= {-1.520783*^146, -2.02771*^146, -3.54849*^146, -5.06927*^145}
    From In[1]:= {-3.579699*^163, -4.77293*^163, -8.35263*^163, -1.19323*^163}
    From In[1]:= {-8.426086*^180, -1.12347*^181, -1.96608*^181, -2.80869*^180}

    You can also simplify your code using some features of Mathematica using

    g=f Map[Exp[#*10]&,e];

    instead of

    g = {Part[f, 1]*Exp[Part[e, 1]*10], Part[f, 2]*Exp[Part[e, 2]*10], Part[f, 3]*Exp[Part[e, 3]*10], Part[f, 4]*Exp[Part[e, 4]*10]};

    I wonder why this

    f = u.Inverse[V];

    produces results that are not the same as this

    f = LinearSolve[V, u];
     
    Last edited: May 12, 2012
  6. May 13, 2012 #5
    Thank you very, very much.

    Although Mathematica has good help, it has no advice for any situation one may come in :(

    Thank you
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Iteration in Mathematica
  1. In mathematica (Replies: 4)

Loading...