# Error in Euler method

Page 1 of 1

## 4 Replies - 2140 Views - Last Post: 17 September 2009 - 11:21 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=126366&amp;s=5b362e80a110148a834e202a0e62663b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 aries0152

Reputation: 0
• Posts: 29
• Joined: 13-September 09

# Error in Euler method

Posted 17 September 2009 - 01:19 AM

I tried to solve an assignment which is like this-
The predator-prey equations are:
dx/dt = ax − txy and

dy/dt = −cy + wxy

where, x and y are the populations of the prey and predator, respectively, at time t . a is the growth rate of the prey and c is the the death rate of the predator.And t and g are measures of the effect of the interaction between the two species.
Consider the initial conditions x0 = 1.0 and y0 = 0.5 , a=0.5, t=0.05, c=1.0, g=0.9.

1)Solve it by using improved Euler method.

2)plot x(t) & y(t) vs t.

That was my assignment . And I have come this far-

```#include <iostream>
#include<cmath>

using namespace std;

FILE * ppstream;

int main()
{
int i=0, N=1000; double x=0.0, y=0.0, xp=0.0, t=0.0, h=0.001;
double a=0.5, t=0.05, c=1.0, w=0.9, xprev=0.0, yprev=0.0;

ppstream = fopen("predatorprey.data","w");

for(i=0; i<=N; i++)
{
xprev =0.0;

xprev = a * x - t * x * y;

xprev += a * (x + h *(a * x - t * x* y) );

xprev += - t*(x+h*(a*x - t *x* y))*(y + h *(-c*y + w *x* y) );

x = x + 0.5 * (xprev)* h;

y = y + 0.5 * ( -c * y + w * x * y )* h;
}

fprintf(ppstream,"%8.4lf %10.8lf %10.8lf \n",t,x,y);

fclose(ppstream);

return 0;
}
```

but still there are some bug in the code, it didnt prints the right values to obtain a graph. I have no hint what is the wrong in my code. Please help me to debug it...
And This is the algo... of improved Euler Method

Is This A Good Question/Topic? 0

## Replies To: Error in Euler method

### #2 poncho4all

Reputation: 123
• Posts: 1,422
• Joined: 15-July 09

## Re: Error in Euler method

Posted 17 September 2009 - 05:30 AM

```ppstream = fopen("predatorprey.data","w");
```

You seem to be coding in C so you need the library stdio.h

on another subject:
Are you getting any erros?
could you post them?

### #3 aries0152

Reputation: 0
• Posts: 29
• Joined: 13-September 09

## Re: Error in Euler method

Posted 17 September 2009 - 06:00 AM

poncho4all, on 17 Sep, 2009 - 04:30 AM, said:

Are you getting any erros?
could you post them?

I have added stdio.h. But the file "predatorprey.data" shows 0.0000 0.00000000 0.00000000 .....

### #4 poncho4all

Reputation: 123
• Posts: 1,422
• Joined: 15-July 09

## Re: Error in Euler method

Posted 17 September 2009 - 11:43 AM

as an error that i can see you have declared t to times double t=0.0 and double t=0.05 wich one do you want to use?

http://www.cprogramm.......t=30&page=0

And this

http://www.alglib.ne...ions/eulerm.php

Hope that helps

This post has been edited by poncho4all: 17 September 2009 - 11:52 AM

### #5 aries0152

Reputation: 0
• Posts: 29
• Joined: 13-September 09

## Re: Error in Euler method

Posted 17 September 2009 - 11:21 PM

poncho4all, on 17 Sep, 2009 - 10:43 AM, said:

as an error that i can see you have declared t to times double t=0.0 and double t=0.05

Upsss!! I have made a few mistake while posting my question and my code. Actually my eqns were in terms of alpha and gamma. I couldnt find the way to insert them in forum . I am really Sorry for that.

correction :

dx/dt= ax-(alpha)xy

dy/dt= -cy+(gamma)xy

where, x and y are the populations of the prey and predator, respectively, at time t . a is the growth rate of the prey and c is the the death rate of the predator.And alpha and gamma are measures of the effect of the interaction between the two species.
Consider the initial conditions x0 = 1.0 and y0 = 0.5 , a=0.5, t=0.05, c=1.0, g=0.9.

1)Solve it by using improved Euler method.

2)plot x(t) & y(t) vs t.

And as your suggestion I have made a few changes in my code.

```#include <iostream>
#include<cmath>
#include<stdio.h>

using namespace std;

FILE * ppstream;

int main()
{
int i=0, N=1000; double x=0.0, y=0.0, xp=0.0, t=0.0, h=0.001;
double a=0.5, alpha=0.05, c=1.0, gamma=0.9, xprev=0.0, yprev=0.0;

ppstream = fopen("predatorprey.data","w");

for(i=0; i<=N; i++)
{
xprev =0.0;

xprev = a * x - alpha * x * y;

xprev += a * (x + h *(a * x - alpha* x* y) );

xprev += - alpha*(x+h*(a*x - alpha *x* y))*(y + h *(-c*y + gamma *x* y) );

x = x + 0.5 * (xprev)* h;

y = y + 0.5 * ( -c * y + gamma * x * y )* h;

fprintf(ppstream,"%8.4lf %10.8lf %10.8lf \n",t,x,y);
}

fclose(ppstream);

return 0;
}

```

Now I have a data file which prints a list of 0.0000, 0.00000000, 0.00000000 I have attached the valuse of "predatorprey.data" with this post . Please check......
I wont be able to plot any graph if it only prints 0.00000