1
$\begingroup$
Clear["Global`*"]
f[x_] = -0.5*x*(x - 4);
h = 0.1;
Lina = {}
Dota[support_] = 
  {{1, 0, h}, {0, 1, f[support - 1  + h] - f[support - 1]}, {0, 0, 1}} . Warcraft;
Warcraft = {{0}, {f[0]}, {1}};
Dota[0] = {{0}, {0}, {f[0]}};
For[i = 1, i < 10, i++, 
  AppendTo[Lina, Part[Dota[1], 1 ;; 2]]; 
  Warcraft = Dota[1];]
Print [Lina]
Show[Plot[f[x], {x, 0, 4}], ListPlot[Lina]]

The above is my code. I want to plot points from the array Lina. I have them in this form (from my loop):

{{{0.1}, {0.195}}, {{0.2}, {0.39}}, {{0.3}, {0.585}}, {{0.4}, {0.78}}, {{0.5}, {0.975}}, {{0.6}, {1.17}}, {{0.7}, {1.365}}, {{0.8}, {1.56}}, {{0.9}, {1.755}}}

How can I do that?

$\endgroup$
5
  • 1
    $\begingroup$ You have to Append a row to Lina . Use Flatten like AppendTo[Lina, Flatten@Part[Dota[1], 1 ;; 2]] $\endgroup$
    – Sumit
    Jun 9 '16 at 19:19
  • $\begingroup$ Thanks a lot! It helps, how can i mark this answer as solution? (best answer?) $\endgroup$
    – Proximus
    Jun 9 '16 at 19:38
  • $\begingroup$ You cannot accept comments as answers, you can only vote them up. You can only accept answers submitted with the check mark to the left. $\endgroup$
    – jackskis
    Jun 9 '16 at 23:29
  • $\begingroup$ ListPlot[Catenate /@ Lina] maybe you want. $\endgroup$
    – yode
    Jun 10 '16 at 8:08
  • $\begingroup$ its a comment @Proximus. So you can't mark it as answer. I am glad that it helped :) $\endgroup$
    – Sumit
    Jun 10 '16 at 11:02
1
$\begingroup$

So, Lina is equal to the list,

{{{0.1}, {0.195}}, {{0.2}, {0.39}}, {{0.3}, {0.585}}, {{0.4}, {0.78}}, {{0.5}, {0.975}}, {{0.6}, {1.17}}, {{0.7}, {1.365}}, {{0.8}, {1.56}}, {{0.9}, {1.755}}}

Try this:

ListPlot[Map[Flatten[#, 1] &, Lina ]]

And it should generate this plot:

enter image description here

$\endgroup$
1
$\begingroup$

I post this to illustrate an alternative to For loop (one of the advantages of Mathematica). I may have misunderstood the aim, so apologies for any errors. The following aims to emulate the OP result.

I have changed variable names to lowercase. Dota is not required. The loop codes a recursion:

f[x_] = -0.5*x*(x - 4);
warcraft = {{0}, {f[0]}, {1}};
lina[support_, h_, n_] := 
 NestList[{{1, 0, h}, {0, 1, f[h + support - 1] - f[support - 1]}, {0,
       0, 1}}.# &, warcraft, n]
res = lina[1, 0.1, 9]
pl = Join @@@ res;
Plot[f[x], {x, 0, 4}, Epilog -> {Red, Line[pl[[All, {1, 2}]]]}, 
 PlotStyle -> {Blue}, 
 PlotLegends -> LineLegend[{Blue, Red}, {f[x], "Lina"}]]

So the result (res) is:

{{{0}, {0.}, {1}}, {{0.1}, {0.195}, {1.}}, {{0.2}, {0.39}, {1.}}, \ {{0.3}, {0.585}, {1.}}, {{0.4}, {0.78}, {1.}}, {{0.5}, {0.975}, \ {1.}}, {{0.6}, {1.17}, {1.}}, {{0.7}, {1.365}, {1.}}, {{0.8}, {1.56}, \ {1.}}, {{0.9}, {1.755}, {1.}}}

The indexing could be changed (here 0 to 9 rather than 1 to 10).

The plot:

enter image description here

$\endgroup$

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.