Message Boards Message Boards

0
|
4163 Views
|
12 Replies
|
6 Total Likes
View groups...
Share
Share this post:

How can this Euler Method be implemented in Mathematica?

Posted 1 year ago

Hello,

Has anyone already implemented the Euler method in Mathematica and can he/she post the implementation?

enter image description here

enter image description here

enter image description here

https://math.libretexts.org/Courses/MonroeCommunityCollege/MTH225DifferentialEquations/3%3ANumericalMethods/3.1%3AEuler's_Method

I would like something like this, using a For loop (if possible, but can be as well even other types of implementation): https://community.wolfram.com/groups/-/m/t/2823888

Thank you.

POSTED BY: Cornel B.
12 Replies
Posted 1 year ago

Here is what ChatGPT gives for rewriting Hans Milton's answer in terms of a for loop.

Eu[func_, xinit_, yinit_, tstart_, tfinal_, h_] := Module[
  {x = xinit, y = yinit, outlist, t},
  outlist = {{tstart, x, y}};
  For[t = tstart + h, t <= tfinal, t += h,  x = x + h//N;  y = y + h*func[x, y];
    outlist = AppendTo[outlist, {t//N, x, y}];
  ];
  outlist
]

A = Eu[f, 1, 0, 0, 1, 10^(-3)];
Take[A, 12] // Column
POSTED BY: Asim Ansari
Posted 1 year ago

Gentlemen...It seems that I managed by myself to put together the code in Mathematica as it should look by comparison with the code in Mathcad, and this is with my little experience of using Wolfram Mathematica.

What is sad is that I thought that I would find more experienced people here in a community of people that use Wolfram Mathematica. I thought I would find the help I needed here in the Wolfram Mathematica community, due to the fact that I assumed I would meet more experienced people here...but it seems that I was wrong in assuming this.

It also seems that I can't really rely on the Wolfram Mathematica community, which in principle shouldn't have been the case (because that's why a community is created, for help). I don't even want to think what I would have done if I had to fight with a more complex code...

Sorry that I had to express these not very pleasant feelings, but...

Also, the code is implemented with for loop...

f[x_, y_] = 1 + y/x + (y/x)^2;
t[0] = 0.;
x[0] = 1.; 
y[0] = 0.;
Subscript[t, start] = t[0];
Subscript[t, final] = 1.;
h = 10^-3;
n = (Subscript[t, final] - Subscript[t, start])/h;
Y1 = {{t[0], x[0]}}; Y2 = {{t[0], y[0]}}; Y3 = {{x[0], y[0]}};

For[i = 0, i \[LessSlantEqual] n-1, i++,
 {
  t[i + 1] = t[i] + h,
  x[i + 1] = x[i] + h,
  y[i + 1] = y[i] + h*f[x[i], y[i]],

  AppendTo[Y1, {t[i + 1], x[i + 1]}],
  AppendTo[Y2, {t[i + 1], y[i + 1]}],
  AppendTo[Y3, {x[i + 1], y[i + 1]}]}]

Show[ListLinePlot[Y1, PlotStyle -> Blue, 
  PlotRange -> {{0, 2}, {0, 2}}],    
 ListLinePlot[Y2, PlotStyle -> Red, PlotRange -> {{0, 2}, {0, 2}}], 
 ListLinePlot[Y3, PlotStyle -> Green, PlotRange -> {{0, 2}, {0, 2}}]]

enter image description here

enter image description here

POSTED BY: Cornel B.
Posted 1 year ago

Cornel, you wrote:

It also seems that I can't really rely on the Wolfram Mathematica community

What is so wrong with my reply above?

POSTED BY: Hans Milton
Posted 1 year ago

Here is a way to replicate the Mathcad example, using a Do loop inside Module.

f[x_, y_] := 1 + y/x + (y/x)^2

Eu[func_,xinit_,yinit_,tstart_,tfinal_,h_]:=Module[
  {x=xinit,y=yinit,outlist},
  outlist={{tstart,x,y}};
  Do[
    x=x+h//N;
    y=y+h*func[x,y];
    outlist=AppendTo[outlist,{t//N,x,y}],
    {t,tstart+h,tfinal,h}
  ];
  outlist
]

A = Eu[f, 1, 0, 0, 1, 10^(-3)];
Take[A, 12] // Column
POSTED BY: Hans Milton

Cornel B.,

The thing is that I would like to learn more about Wolfram Mathematica ...

and I, for one, welcome, motivate and support anybody who is planning to do so! And yes, the code sample you provided seems simple and intuitive. I do not want to argue!

But my point is that WolframLanguage uses a different programming paradigm, the so called "Functional Programming". And in particular if you do not want just a single piece of code getting somehow transferred but doing exercises in order to learn, then you need to fully get into this concept (e.g. by avoiding those looping constucts). Learning WL can be tough and overwhelming, and you are ahead of a steep learning curve - but it will pay! You will be amazed about what is possible with just a few lines of code! And there are many excellent resources for a start, to name just a few:

Regards -- Henrik

POSTED BY: Henrik Schachner
Posted 1 year ago

and it would surprise me if with "Mathcad and other software" this would be different.

For example, in Mathcad it looks like this (simple, intuitive and you immediately understand what it's about):

enter image description here

enter image description here

enter image description here

With the for loop, the Euler method (but also other methods which I have at my hand) is implemented in Matlab as well.

and finally it is somewhat disconcerting to ask for substantial help - but under the condition that the answer should contain For loops and is easy to understand for just anyone.

This Euler program that I discussed above (meaning: how could such a simple program be implemented in Wolfram Mathematica) is only for the beginning, as training, because I have other more complex programs that only use for loops...that's why I still insisted on implementation with for loop.

To transfer the Euler program from Matlab to Mathcad was not difficult at all, and it did not give me such a headache as I encounter now when trying to implement this simple program in Wolfram Mathematica (as in Matlab also with for loop this Euler program is implemented). I didn't think it would be so painful when I had to implement it in Mathematica.

Then I honestly wonder what headaches I will have if I have to implement other more complex programs than in this case of this simple example (Euler Method) which causes such headaches and restrictions (for example, that it cannot be implemented in Mathematica with a for loop). Another sad thing is that we cannot find any helpful references which are supposed to be at our hand for such simple programs (what can I say then about more complicated programs...? you let yourself be beaten...I think..), concrete examples of how to do it, or at least a more bouncy community to jump to your help of what you want to achieve, your problem.

The thing is that I would like to learn more about Wolfram Mathematica (because between Maple and Mathematica, I don't know, but Wolfram Mathematica attracted me more, at least from the outside; and Matlab, and Mathcad each have their disadvantages, in addition to their advantages, and that's why I would like to learn another math program in addition), but when I see what a headache it is when you have to implement even something simple (not to mention something more complex), that it is not very intuitive how to implement a program in Mathematica, that you also won't find pretty useful resources and help for the problems you face.

POSTED BY: Cornel B.

Cornel B.

I disagree - this is unfair to Mathematica!

  • Mathematica is an incredible powerful tool - but as it is typical for those power tools one always needs a good deal of experience to fully use it.
  • my code above implementing the Euler method is as simple and obvious (and short!) as it can be! For doing this there is definitely no "very good experience" required. But yes - you need some basic knowledge and understanding. This is not surprising - and it would surprise me if with "Mathcad and other software" this would be different.
  • if I had to solve DEQs I simply would use highly proven functions like DSolve[] or NDSolve[] and not waste my time by implementing old algorithms! And even if (for some strange reason) it must be done with Euler one still can do it like NDSolve[ (* some DGL *) , Method -> {"FixedStep", Method -> "ExplicitEuler"}]
  • and finally it is somewhat disconcerting to ask for substantial help - but under the condition that the answer should contain For loops and is easy to understand for just anyone.
POSTED BY: Henrik Schachner
Posted 1 year ago

Mariusz Iwaniuk,
Henrik Schachner, Radiation Therapy Center, Weilheim, Germany

If even such a thing, as for example the implementation of the Euler method (which is a single equation)), is difficult to implement in Wolfram Mathematica (you don't even find clear examples) and gives you headaches with the implementation, and you must have a very good experience of using Wolfram Mathematica until you manage to implement an equation like the Euler method, then it becomes clear why people choose Mathcad and other software that is more intuitive, easy to use and understand, and which does not give you so much trouble...

POSTED BY: Cornel B.

Somehow this seems to be closely related to this recent discussion.

Why on earth you would want / insist in using a For[] loop in Mathematica?

POSTED BY: Henrik Schachner
Posted 1 year ago

Why on earth you would want / insist in using a For[] loop in Mathematica? - Henrik Schachner, Radiation Therapy Center, Weilheim, Germany

As I said in the post, I am also open to other types of implementations that do not necessarily have a For loop in the implementation, but why I keep talking about For is that I have already implemented and tested the respective implementations with For in Matlab (and also in Mathcad, but Matlab is more apropriate in the code to Mathematica than Mathcad - that's why I referred to Matlab), and so I know that this implementation with For loop it works, and on the other hand, having already implemented what I want to implement (for example, this Euler Method) with the For loop in Matlab, I have a reference to follow. But like I said, I'm open to other types of implementations as long as I can understand them.

POSTED BY: Cornel B.

Maybe this help see here.

POSTED BY: Mariusz Iwaniuk

OK, I would like (to try) to answer the question you asked in this post about a possible implementation of the Euler method in Mathematica. But I think to give an answer here is more appropriate.

To have an example at hand I use the famous Lorenz system. The core of the idea is to do the iteration with NestList[]: say, you want to iterate a function func three times, starting with x0, then you can do it like so:

NestList[func, x0, 3]
(*  Out:  {x0,func[x0],func[func[x0]],func[func[func[x0]]]}   *)

Now for the Euler method on the Lorenz system - here is my approach:

(* function describing v' at time t  -  here Lorenz system: *)
f[{t_, v : {x_, y_, z_}}] := {t, {-3 (x - y), -x z + 26.5 x - y, x y - z}}
(* step size - to be adjusted: *)
h = 0.02;   
(* single step of iteration - general formulation: *)
eulerStep[{t_, v_List}] := {t + h, v + h/2 (f[{t, v}][[2]] + f[{t + h, v + h f[{t, v}][[2]]}][[2]])}
(* initial conditions: *)
init = {0, {0, 1, 0}};
(* performing iteration (here 2000 steps): *)
data = NestList[eulerStep, init, 2000];
pts = Last /@ data;
Graphics3D[{Arrowheads[{.03, #} & /@ Range[0, 1, .05]], Arrow[pts]}, 
 Axes -> True, BoxRatios -> Automatic, AxesLabel -> {"X", "Y", "Z"}]

enter image description here

This reminds me of a washing machine, but in fact it is the well known Lorenz attactor.

Does that help? Regards -- Henrik

POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract