Solve Floating Point Error: 4th Order Runge-Kutta Method

In summary, this program uses the fourth order Runge-Kutta method to solve for thedy/dx=x2+y2 equation when y(0)=0, h=0.2. The problem is that if x2==0.4, the program will not stop and will keep iterating.
  • #1
Vagrant
195
1
This is a program to slove differential equation using fourth order Runge-Kutta method. The question is dy/dx=x2+y2
when y(0)=0, h=0.2
find y(0.4) (Ans= 0.021360)

#include<stdio.h>
#include<conio.h>
float f(float x, float y)
{
return (x*x+y*y);
}
void main()
{
float x1,y1,x2,y2,h=0.2,m1,m2,m3,m4;
clrscr();
printf("Enter the initial values of x and y\n");
scanf("%f%f",&x1,&y1);
while(1)
{
m1=f(x1,y1);
m2=f(x1+h/2,y1+(m1*h)/2);
m3=f(x1+h/2,y1+(m2*h)/2);
m4=f(x1+h,y1+m3*h);
y2=y1+(h/6)*(m1+(2*m2)+(2*m3)+m4);
x2=x1+h;
if(x2==0.4)
break;
else
{
x1=x2;
y1=y2;
}
}
printf("The value of y at x= 0.4 is %f\n",y2);
getch();
}

However this program is not working and giving the message Abnormal value: Floating point overflow. Can anyone please find out the error in this?
Thanks
 
Technology news on Phys.org
  • #2
right away i notice that you will never get out of that while(1) loop unless x2==0.4
is that guaranteed to happen for every possible input?
if not, you've got an infinite loop. bad.
 
  • #3
No, it will only be satisfied for this particular question, i.e. initial value of x=0, h=0.2.
It's not a very good program but it should work at least for this case, which it is not doing.
 
  • #4
These are float values, chances that x2 will be EXACTLY 0.4 are slim.

You should get information about the line where the error occurs. If you don't have a debugger that will let you trace code step by step, put something like printf("x2 = %f\n") into code to see how x2 values change. You may check every variable this way.
 
  • #5
I did that. The loop is not stopping after x2=0.4, why is break not working?
 
  • #6
Try if (x>=0.4).
 
  • #7
Yes, it works now.

Borek said:
These are float values, chances that x2 will be EXACTLY 0.4 are slim.
Was this the problem?

Thank you.
 
  • #8
shramana said:
Was this the problem?

As you see. Generally you can't compare floats with x==y, rather with abs(x-y)<treshold.
 
  • #9
I'll keep this in mind from now on. Thank you for the time and effort you put into this.
 

Related to Solve Floating Point Error: 4th Order Runge-Kutta Method

1. What is a floating point error?

A floating point error occurs when a computer is unable to represent a real number accurately due to the limited number of bits used in its floating point representation.

2. How does the 4th Order Runge-Kutta method solve floating point errors?

The 4th Order Runge-Kutta method is a numerical method used to solve differential equations. It uses a combination of calculations and approximations to reduce the error in the final result, thus minimizing the effects of floating point errors.

3. Why is the 4th Order Runge-Kutta method commonly used to solve floating point errors?

The 4th Order Runge-Kutta method is known for its accuracy and efficiency in solving differential equations. It is also robust in handling a wide range of problems, making it a popular choice for solving floating point errors.

4. Are there any limitations to using the 4th Order Runge-Kutta method to solve floating point errors?

While the 4th Order Runge-Kutta method is generally effective in minimizing floating point errors, it is not a foolproof solution. In some cases, it may still produce inaccurate results due to the inherent limitations of floating point arithmetic.

5. Are there any alternative methods to solve floating point errors?

Yes, there are other numerical methods that can be used to mitigate floating point errors, such as the Taylor series method or the Euler method. The choice of method depends on the specific problem and the desired level of accuracy.

Similar threads

  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
Replies
61
Views
1K
  • Introductory Physics Homework Help
Replies
6
Views
806
Replies
7
Views
2K
Back
Top