Runge-Cute Method for solving system of differentional equat
Posted: 17.03.2008, 19:02
Would you be so kind to change algorithm for solving this system
d2_x/dt_2=-GMx/(x^2+y^2)^(3/2)
d2_y/dy_2=-GMy/(x^2+y^2)^(3/2)
algorithm name is Runge-Cute Method
#include <stdio.h>
#include <stdafx.h>
#include <math.h>
#include <iostream>
using namespace std;
double f(int i,double x,double y[4]){
switch (i){
case 1:return y[4];break;
case 2: return y[1];break;
case 3:return y[2];break;
case 4:return y[3];break;
default : break;
}
}
void step(double x,double h,int n,double y[4])
{
int i=0;
double yt[4];
double k1[4];
double k2[4];
double k3[4];
double k4[4];
for(i = 1; i <= n; i++)
{
k1[i] = h*f(i, x, y);
}
for(i = 1; i <= n; i++)
{
yt[i] = y[i]+0.5*k1[i];
}
for(i = 1; i <= n; i++)
{
k2[i] = h*f(i, x+h*0.5, yt);
}
for(i = 1; i <= n; i++)
{
yt[i] = y[i]+0.5*k2[i];
}
for(i = 1; i <= n; i++)
{
k3[i] = h*f(i, x+h*0.5, yt);
}
for(i = 1; i <= n; i++)
{
yt[i] = y[i]+k3[i];
}
for(i = 1; i <= n; i++)
{
k4[i] = h*f(i, x+h, yt);
}
for(i = 1; i <= n; i++)
{
y[i] = y[i]+(k1[i]+2.0*k2[i]+2.0*k3[i]+k4[i])/6;
}
}
void solvesystemrungekutta(double x,double x1,int steps,double result[4]){
for(int i = 1; i <= steps-1; i++)
{
step(x+i*(x1-x)/steps, (x1-x)/steps,4, result);
}
}
int _tmain(){
//???µ?€???°?
d2_x/dt_2=-GMx/(x^2+y^2)^(3/2)
d2_y/dy_2=-GMy/(x^2+y^2)^(3/2)
algorithm name is Runge-Cute Method
#include <stdio.h>
#include <stdafx.h>
#include <math.h>
#include <iostream>
using namespace std;
double f(int i,double x,double y[4]){
switch (i){
case 1:return y[4];break;
case 2: return y[1];break;
case 3:return y[2];break;
case 4:return y[3];break;
default : break;
}
}
void step(double x,double h,int n,double y[4])
{
int i=0;
double yt[4];
double k1[4];
double k2[4];
double k3[4];
double k4[4];
for(i = 1; i <= n; i++)
{
k1[i] = h*f(i, x, y);
}
for(i = 1; i <= n; i++)
{
yt[i] = y[i]+0.5*k1[i];
}
for(i = 1; i <= n; i++)
{
k2[i] = h*f(i, x+h*0.5, yt);
}
for(i = 1; i <= n; i++)
{
yt[i] = y[i]+0.5*k2[i];
}
for(i = 1; i <= n; i++)
{
k3[i] = h*f(i, x+h*0.5, yt);
}
for(i = 1; i <= n; i++)
{
yt[i] = y[i]+k3[i];
}
for(i = 1; i <= n; i++)
{
k4[i] = h*f(i, x+h, yt);
}
for(i = 1; i <= n; i++)
{
y[i] = y[i]+(k1[i]+2.0*k2[i]+2.0*k3[i]+k4[i])/6;
}
}
void solvesystemrungekutta(double x,double x1,int steps,double result[4]){
for(int i = 1; i <= steps-1; i++)
{
step(x+i*(x1-x)/steps, (x1-x)/steps,4, result);
}
}
int _tmain(){
//???µ?€???°?