-1
$\begingroup$

I used "Table" to calculate and plot the following complicated function. First, we choose an initial value for $G$ and other parameters, but at later times $G$ changes with function $I_a$.

Y = 1; r1 = .3; r2 = .3; G = 0.35;
points = Table[L = G - Y; w = Sqrt[-4 + G^2 + 2 G Y + Y^2];

Ia = 1/(
   8 w^2) (Cosh[2 L t] + Sinh[2 L t]) (8 + 2 G^2 - 2 w^2 + 
     4 G Y + 2 Y^2 - 2 (G^2 - w^2 + 2 G Y + Y^2) Cosh[2 r1] - 
     8 Cosh[2 r2] - 8 Cosh[2 t w] - 2 G^2 Cosh[2 t w] - 
     2 w^2 Cosh[2 t w] - 4 G Y Cosh[2 t w] - 2 Y^2 Cosh[2 t w] + 
     G^2 Cosh[2 (r1 - t w)] + w^2 Cosh[2 (r1 - t w)] + 
     2 G Y Cosh[2 (r1 - t w)] + Y^2 Cosh[2 (r1 - t w)] + 
     4 Cosh[2 (r2 - t w)] + G^2 Cosh[2 (r1 + t w)] + 
     w^2 Cosh[2 (r1 + t w)] + 2 G Y Cosh[2 (r1 + t w)] + 
     Y^2 Cosh[2 (r1 + t w)] + 4 Cosh[2 (r2 + t w)] - 
     4 G w Sinh[2 t w] - 4 w Y Sinh[2 t w] - 
     2 G w Sinh[2 (r1 - t w)] - 2 w Y Sinh[2 (r1 - t w)] + 
     2 G w Sinh[2 (r1 + t w)] + 2 w Y Sinh[2 (r1 + t w)]) + 
  Integrate[(
   E^(2 L (t - u) - 
     2 (t + u) w) (E^(2 u w) (-G + w - Y) + 
      E^(2 t w) (G + w + Y))^2)/(4 w^2), {u, 0, t}] // Chop;


 G = 1/(1 + Ia/.05);

 {{t, Ia}, {t, G}}, {t, 0, 10, .05}];
Iapoints = Map[First, points];
varpoints = Map[Last, points];


ListLinePlot [Iapoints,PlotRange -> All]

Iapoints

ListLinePlot [varpoints,PlotRange -> All]

varpoints

The output seems good to this point. But if I change the initial values as

Y = 2.2; r1 = .3; r2 = .3; G = 1.96;

and

G = 2/(1 + Ia/5)

then the output does not seem good. I couldn't upload the figure because I don't know how imgur works!

Anyway, I think the irregularities in the plots are due to error accumulation. I want to try a for loop that does the same job and see if the errors are still there or I can get a good plot for any parameters. But I don't know how to write a "for loop" for this problem.

$\endgroup$
9
  • 4
    $\begingroup$ People here hate "for loops" you know... $\endgroup$
    – ZaMoC
    Nov 26, 2018 at 22:40
  • $\begingroup$ @J42161217 Do you know a better alternative for "for loop"? I don't care what approach I use. I only want a good plot at the end. $\endgroup$
    – Saeid
    Nov 26, 2018 at 22:44
  • 1
    $\begingroup$ That was a joke... Also I think that everything works fine with Table. What results were you expecting? $\endgroup$
    – ZaMoC
    Nov 26, 2018 at 22:47
  • $\begingroup$ @J42161217 This is a code that someone wrote in this site! That is not mine. If you try to run it with those values I mentioned, you will see that the first values give a beautiful plot, but the latter values give an ill-conditioned plot. $\endgroup$
    – Saeid
    Nov 26, 2018 at 22:52
  • 3
    $\begingroup$ Actually, your integral can be done symbolically once before starting the loop. Then insert the numeric values into that result. This will be faster and more precise than doing the integral in every stop of your loop. Other than that, this looks like you are running in numeric instabilities which are a common phenomenon for algorithms like yours and most certainly have nothing to do with looping constructs. To help you understand what happens people on this site will need some more background about what it is that you are trying to solve, a link to the source of the code would be a start... $\endgroup$ Nov 27, 2018 at 6:39

1 Answer 1

0
$\begingroup$

here is your "for loop" with the same "ill-conditioned plot"

An explanation for the "strange" plot:
If you use ListPlot instead of ListLinePlot you will see that the points which formed a straight line in your first example, in the second, they are grouped in 2 groups half moving up and half moving down (alternating) as the denominator of Ia changes from 5 to .05

Y = 2.2; r1 = .3; r2 = .3; G = 1.96;
points = {};
For[t = 0, t <= 10, t = t + .05, L = G - Y; 
w = Sqrt[-4 + G^2 + 2 G Y + Y^2];
Ia = 1/(8 w^2) (Cosh[2 L t] + Sinh[2 L t]) (8 + 2 G^2 - 2 w^2 + 
    4 G Y + 2 Y^2 - 2 (G^2 - w^2 + 2 G Y + Y^2) Cosh[2 r1] - 
    8 Cosh[2 r2] - 8 Cosh[2 t w] - 2 G^2 Cosh[2 t w] - 
    2 w^2 Cosh[2 t w] - 4 G Y Cosh[2 t w] - 2 Y^2 Cosh[2 t w] + 
    G^2 Cosh[2 (r1 - t w)] + w^2 Cosh[2 (r1 - t w)] + 
    2 G Y Cosh[2 (r1 - t w)] + Y^2 Cosh[2 (r1 - t w)] + 
    4 Cosh[2 (r2 - t w)] + G^2 Cosh[2 (r1 + t w)] + 
    w^2 Cosh[2 (r1 + t w)] + 2 G Y Cosh[2 (r1 + t w)] + 
    Y^2 Cosh[2 (r1 + t w)] + 4 Cosh[2 (r2 + t w)] - 
    4 G w Sinh[2 t w] - 4 w Y Sinh[2 t w] - 
    2 G w Sinh[2 (r1 - t w)] - 2 w Y Sinh[2 (r1 - t w)] + 
    2 G w Sinh[2 (r1 + t w)] + 2 w Y Sinh[2 (r1 + t w)]) + 
 Integrate[(E^(2 L (t - u) - 
        2 (t + u) w) (E^(2 u w) (-G + w - Y) + 
        E^(2 t w) (G + w + Y))^2)/(4 w^2), {u, 0, t}] // Chop;
 G = 2/(1 + Ia/5);
AppendTo[points, {{t, Ia}, {t, G}}]];
Iapoints = Map[First, points];
varpoints = Map[Last, points];

ListLinePlot[Iapoints, PlotRange -> All]

ListLinePlot[varpoints, PlotRange -> All]
$\endgroup$
6
  • $\begingroup$ Do you know if it is due to imperfection of Mathematica, or there is a systematic way to avoid the ill-conditioned part? $\endgroup$
    – Saeid
    Nov 26, 2018 at 23:02
  • $\begingroup$ I think everything is correct. It is just a plot. $\endgroup$
    – ZaMoC
    Nov 26, 2018 at 23:07
  • $\begingroup$ @Saeid Try ListPlot instead of ListLinePlot at the end and tell me if you like the results. As you will see there are some points alternating up and down and that is why you get those lines. Everything is fine. $\endgroup$
    – ZaMoC
    Nov 26, 2018 at 23:11
  • $\begingroup$ I tried it, but the same issue is still there. For some reason that is not clear to me, the calculations are not valid for $t>2$ in this case. This behavior might seem OK to mathematicians but it has no physical meaning. That is why I thought my method is not good and asked a "for loop", but that did not work too! $\endgroup$
    – Saeid
    Nov 27, 2018 at 2:20
  • $\begingroup$ In G = 2/(1 + Ia/5) try to change 5 and give different values like 1 or 0.5 or 0.1 .This may produce better results $\endgroup$
    – ZaMoC
    Nov 27, 2018 at 2:29

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.