# Using Euler's Method [duplicate]

Relatively new with Mathematica and trying to finish this problem, this is the second problem, the previous was the same questions, but a different equation "x-y." I was able to complete that one with the same code I have provided in the image, but this new function is throwing me off. I have tried to run the code and as you can see it gave no output. Taking any advice and thank you for your time.

• Your table command can't just have n at the end. It needs something like {n,1,number}. if you google Euler method and mathematica, there are lots of examples even on this site. Jul 21, 2017 at 2:47
• @Nasser thanks I will look into it, this was the layout my teacher gave us.
– Mike
Jul 21, 2017 at 2:48
• May be teacher made typo. table command should have ,{n,starting number, ending number} at the end. You just have n there. What is n? You should tests your expressions on the side. Write each command on ONE cell and look at the output below it. Do not write more than command in same cell. One command per cell only. Makes debugging much easier. Jul 21, 2017 at 2:50
• @Nasser May I respond to this question with my code from the previous problem that worked, and maybe you can take a look at it?
– Mike
Jul 21, 2017 at 2:53
• @Nasser I think the Table command is fine, since n = 50 is initialized. It just accumulates the results of 50 Euler steps.-- Mike, for 2), look up VectorPlot and/or StreamPlot. I would bet your teacher mentioned one or the other at some point (or meant to). You could also search this site for direction field or slope field. Jul 21, 2017 at 3:03

I think the problem is that the notebook interface is stalled because your output grows to a huge expression. This is because you're using exact arithmetic in an iterative way. The simple solution would be to surround the first lines by N[...] or to put decimal points into all your starting values to make them machine-precision numbers.

So just modify your code to this:

xf = 4;
x0 = 1;
y0 = -2;
n = 50;

h = (xf - x0)/n;

f[x_, y_] := Log[x] + y + 1

Table[
(x1 = N[x0 + h]);
(y1 = N[y0 + h f[x0, y0]]);
(x0 = x1; y0 = y1), n]


{-2.06, -2.1201, -2.18051, -2.24141, -2.30299, -2.36543, -2.4289, \ -2.4936, -2.55969, -2.62736, -2.69681, -2.76821, -2.84176, -2.91767, \ -2.99614, -3.0774, -3.16167, -3.24918, -3.34019, -3.43495, -3.53374, \ -3.63684, -3.74456, -3.85721, -3.97512, -4.09865, -4.22817, -4.36407, \ -4.50676, -4.65669, -4.81432, -4.98013, -5.15464, -5.3384, -5.53199, \ -5.73603, -5.95116, -6.17806, -6.41748, -6.67017, -6.93695, -7.21869, \ -7.5163, -7.83076, -8.16309, -8.51437, -8.88577, -9.2785, -9.69386, \ -10.1332}

However, you should really not be using Table for this. Look up Do, or much better in this case: NestList.

It's not easy to figure out what's going on when Mathematica hangs without any feedback - the notebook interface does that much too often.

• I tried using both your method where I include the N on each equation and changing to Nestlist, neither of which worked.
– Mike
Jul 21, 2017 at 17:23
• It works fine, as you can see in my edit. Make sure to restart the calculation from the top. This is important because your variables aren't localized.
– Jens
Jul 21, 2017 at 18:27