I programmed a Euler function but misread the instructions, so now I have to make a new one, but I can't figure it out.

I have made the following automatic Euler function.

```
f[x_, y_] := -x y^2;
x0 = 0;
y0 = 2;
xend = 2;
steps = 20;
h = (xend - x0)/steps // N;
x = x0;
y = y0;
eulerlist = {{x, y}};
For[i = 1, i <= steps, y = f[x, y]*h + y;
x = x + h;
eulerlist = Append[eulerlist, {x, y}];
i++
]
Print[eulerlist]
```

But it just generates the list I have specified.

I would like to have a Euler function which is able to generate this form:

```
Euler[y, 2, -x y^2, {x, 0, 2}, 20]
```

I don't seem to get any further.

It is not clear what you are asking, but if what you want is to be able to input

```
Euler[y, 2, -x y^2, {x, 0, 2}, 20]
```

and get

```
{{0,2},{0.1,2.},{0.2,1.96},{0.3,1.88317},{0.4,1.77678},{0.5,1.6505},{0.6,1.51429},{0.7,1.37671},{0.8,1.24404},{0.9,1.12023},{1.,1.00728},{1.1,0.905822},{1.2,0.815565},{1.3,0.735748},{1.4,0.665376},{1.5,0.603394},{1.6,0.548781},{1.7,0.500596},{1.8,0.457994},{1.9,0.420238},{2.,0.386684}}
```

Then you need to write a function definition like this:

```
Euler[y0_, f_, {x0_, xend_}, steps_Integer?Positive] := (* body *)
```

Notice the underscores to denote patterns, the `:=`

to denote delayed evaluation and the pattern specification `Integer?Positive`

.

As for the body of the function -- oh my goodness could you have picked a less Mathematica-style approach? Perhaps not. Procedural loops and `Append`

are almost never the best way to do anything in Mathematica.

Here is a better solution.

```
Euler[y_, y0_, f_, {x_, x0_, xend_}, steps_Integer?Positive] :=
With[{h = N[(xend - x0)/steps], ff = Function[{x, y}, f]},
NestList[{#[[1]] + h, ff[#[[1]], #[[2]]]*h + #[[2]]} &, {x0, y0},
steps]]
Euler[y, 2, -x y^2, {x, 0, 2}, 20]
{{0, 2}, {0.1, 2.}, {0.2, 1.96}, {0.3, 1.88317}, {0.4,
1.77678}, {0.5, 1.6505}, {0.6, 1.51429}, {0.7, 1.37671}, {0.8,
1.24404}, {0.9, 1.12023}, {1., 1.00728}, {1.1, 0.905822}, {1.2,
0.815565}, {1.3, 0.735748}, {1.4, 0.665376}, {1.5, 0.603394}, {1.6,
0.548781}, {1.7, 0.500596}, {1.8, 0.457994}, {1.9, 0.420238}, {2.,
0.386684}}
```

If you want something that *outputs* `Euler[y, 2, -x y^2, {x, 0, 2}, 20]`

, then typing it into the notebook is the quickest method.

Similar Questions

I'm not sure I got how to define private functions right. When I'm writing a package mathematica, I just do this: BeginPackage[myPackage`] myPublicFunction::usage=myPublicFunction blahblahblah; Be

How do I write a simple stored procedure in postgres that doesn't return a value at all? Even with a void return type when I call the stored procedure I get a single row back. CREATE FUNCTION somefunc

How do I write this function using the >>= operator? parseNumber2 :: Parser LispVal parseNumber2 = do x <- many1 digit return $ (Number . read) x

how do i write the following sql query in Lambda expression. select rating from ratingtable where movieId=@movieid and userid=@userid

I want to create multiple variables with iterated names in Mathematica, something like this: Do[f <> ToString[i] = i*2, {i, 1, 20}] where I get f1=2, f2=4, f3=6, ... and so on. I the error:

How can I define a abstract odd function, say f[x]. Whenever f[x]+f[-x] appears, mathematica simplifies it to zero.

I'm adding some functionality to an existing function. I need to insert an additional step in the middle of the current routine. I know how to go to the 2nd function but I don't know what to do to ret

I am trying to write a generic convert function but I don't know how do I do that. This is my skeleton: public static T ConvertByGenerics<T>(string value) { //what do I write here? return defaul

Can Mathematica do Bayes Rule conditional probability calculations, without doing the calculation manually? If so how? I have been searching both the Mathemtaica doco and the web for a hint but cannot

I am working on an Asset DB using a lamp stack. In this example consider the following 5 tables: asset, server, laptop, desktop, software All tables have a primary key of id, which is a unique asset i

How do I write text in DOS, setting each character to a different colour?

I would like to pass the parameter values in meters or kilometers (both possible) and get the result in meters/second. I've tried to do this in the following example: u = 3.986*10^14 Meter^3/Second^2;

How do I write to a parent's dictionary? I've given a list of urls to some children to retrieve, which then need to write to the parent's dictionary: from multiprocessing import Pool import random par

Finding it a little difficult to find a good title, apologies if this one is misleading.. I would like to write a function that will take other functions (an their possible arguments) along with other

Say I have a website which shows the user ten images and asks them to categorise each image by clicking on buttons. A button for funny, a button for scary, a button for pretty and so on. These b

I have a question about NDSolve function in Mathematica. I have an oscillator defined by these two equations: x' = v v' = -x - u*v^3 where u is some constant. How to create an NDSolve that resolves t

I want to write a command for Atom that composes two or more pre-existing commands, like Select Line and then Cut. How do I do that?

How can this Mathematica code be ported to Python? I do not know the Mathematica syntax and am having a hard time understanding how this is described in a more traditional language. Source (pg 5): ht

I am attempting to graph the following function and indicate on the plot where the function passes 45 degree slope. I have been able to graph the function itself using the following code: T = 170 Degr

I'm trying to solve a problem: I need to translate an octave code in mathematica but I'm struggling with those lines. function g = NeumannBoundCond(f) [nrow,ncol] = size(f); g = f; g([1 nrow],[1 ncol]

How do I promisify my own function (that lives in another directory)? Here's my code: // app.js // include database var mongo = require('./mongo'); var promise = require('bluebird'); var u = require

Assume the following variable values were set earlier in the code: LSLATHOR = 1780, NRSLATVER = 34 Then I have these two lines of GWBASIC: 100 PITCHHOR=(LSLATHOR/(NRSLATVER+1)) : LSLATHOR=PITCHHOR*(NR

I am trying to change the color of the function swapFE() below and I can't figure out how to write it. I was told to change the color of the phrase node to the color value (155, 102, 102). I tried to

I am trying to import a histogram produced by Stata as an .eps file into Mathematica, but it does not display axes' labels. That is, for some reason, Mathematica does not import .eps as but rather tra

I'd like to add 2 arguments to a dialog box function. The first argument should be for the box title and the second should be for the box content. I'd like to call this function and set the box's titl

What is the best way to define a numerical constant in Mathematica? For example, say I want g to be the approximate acceleration due to gravity on the surface of the Earth. I give it a numerical value

I'm trying to find all Euler paths in a graph. For doing this, I'm using a java code based on this: http://www.sanfoundry.com/java-program-implement-euler-circuit-problem/ (this example finds just one

I'm minimizing a function in Mathematica related to a variable length vector: x = Table[Subscript[X, i], {i, n}] My minimize function is currently: Minimize[{x.\[CapitalSigma].x, \[Mu].x >= R &

If I add a Tilde(~) in the text property of a label,the label doesn't display it,instead it displays an upper tilde. How do I write a normal tilde like in the bracksers(~)?

I am wondering how I can write a function to be used in the Apply function in Mathematica? For example, I want to trivially re-implement the Or function, I found the following Apply[(#1 || #2)&,{a

How do I make command-line Mathematica 7 display graphics? In older versions of Mathematica, Plot[x,{x,1,2}] would popup a window displaying the plot w/ some formatting options (line thickness, equ

I am using the TextWriter to try to write to a hidden file, and it is throwing an exception. I can't seem to figure out how to write to a hidden file. using (TextWriter tw = new StreamWriter(filename)

I want to create a new column. How do I write this in SQL? class mytable(models.Model): created_at = models.DateTimeField(auto_now_add=True)

I am looking to use Mathematica to print equations in TraditionalForm. While I have been successful for some simple ones, I have not for the 2 below.

I've read a couple of similar cases on Stack Overflow, but not one exactly like mine yet. Here's the gist of my jQuery plugin: (function($) { var methods = { init: function() { ... $(this).myPlugin(c

I'd like to write some pixels into an image, and write the image to disk. I've been following the advice I've heard from many Haskellers to follow the type signatures, and essentially play type tetri

How can I define a function f(x) in Mathematica that gives 1 if x is in [-5, -4] or [1, 3] and 0 otherwise? It's probably something simple but I just can't figure it out!

I am writing a function in R that will evaluate the fit of a model, but each model takes the same arguments. How can I avoid repeating the same argument to each call to a model? It is probably more cl

How do I write a function to search for an element in two dimensional array: if exists returns 1, otherwise returns no? #include <stdio.h> int search(int a[3][3],int x); int main () { int Array[

My assignment is to write an overloaded version of iquote(), a method that displays the type of its argument and its argument enclosed in double quotation marks. I am asked to write three versions: on

How do I write a switch statement in Ruby?

I'm trying to find the minimum spanned tree using Mathematica and I want to use the MinimumSpanningTree function from Combinatorica. I'm using the following code. Needs[Combinatorica`] MinimumSpanni

I am using GraphPlot to draw directed graphs with roughly a 100 vertices. I am replacing each vertex with a small rectangular or square image by defining the VertexRenderingFunction. The images often

I want to use an equivalent of Oracle's nvl() function in Ruby. Is there a built in function or do I have to write one myself? Edit: I am using it to rewrite some sql to ruby: INSERT INTO my_table (id

This is probably a simple question ( or I assume to be ) however, I still can't get it to work no matter how much i twinker with it. I am trying to write a variable inside a li tag. This variable shou

I recently answered a question with an attempt at writing a quicksort function in Scala, I'd seen something like the code below written somewhere. def qsort(l: List[Int]): List[Int] = { l match { cas

How do I tell mathematica to do this replacement smartly? (or how do I get smarter at telling mathematica to do what i want) expr = b + c d + ec + 2 a; expr /. a + b :> 1 Out = 2 a + b + c d + ec

So how do I write a handler. Say I had a .bob file on my server. A custom extension and format. Inside the .bob file I might have echo 'Hi'; I want to write a handler so that when a visitor goes to my

Say I have 5 tables, tblBlogs tblBlogPosts tblBlogPostComment tblUser tblBlogMember BlogId BlogPostsId BlogPostCommentId UserId BlogMemberId BlogTitle BlogId CommentText FirstName UserId PostTitle Blo

anyone know how I would write that query with AR? select *, (m.user_id=1) as member from band b join memberships m on m.band_id = g.id; Thanks in advance.