Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
_Разное / math_kurs.doc
Скачиваний:
3
Добавлен:
16.04.2013
Размер:
230.91 Кб
Скачать

Приложение 1.

Программа численного решения системы дифференциальных уравнений (6).

#include <fstream.h>

#include <math.h>

//------------------------Output file------------------------------------------

ofstream out_file;

//-------------------Global constants & variables-----------------------

double t=0,

h=0.5;

double q[4]={0,0,0,1},

psi[4]={0,0,0,1};

double eps=1e-4;

int k=0;

//------------------------Runge-Kutta-method----------------------------

double m_func1(double m_q, double m_psi, double time)

{

return (m_psi-m_q-0.05*m_psi*m_psi*m_psi);

}

double m_func2(double m_q, double m_psi, double time)

{

double tmp=cos(time);

return ( (-0.5)*m_q-( tmp+fabs(tmp) ) );

}

void Runge_Kutta()

{

double z1[2],

z2[2],

z3[2],

z4[2];

z1[0]=m_func1(q[3],psi[3],t);

z1[1]=m_func2(q[3],psi[3],t);

z2[0]=m_func1( (q[3]+h*z1[0]/2), (psi[3]+h*z1[1]/2), (t+h/2) );

z2[1]=m_func2( (q[3]+h*z1[0]/2), (psi[3]+h*z1[1]/2), (t+h/2) );

z3[0]=m_func1( (q[3]+h*z2[0]/2), (psi[3]+h*z2[1]/2), (t+h/2) );

z3[1]=m_func2( (q[3]+h*z2[0]/2), (psi[3]+h*z2[1]/2), (t+h/2) );

z4[0]=m_func1( (q[3]+h*z3[0]), (psi[3]+h*z3[1]), (t+h) );

z4[1]=m_func2( (q[3]+h*z3[0]), (psi[3]+h*z3[1]), (t+h) );

q[2]=q[3]+(z1[0]+2*z2[0]+2*z3[0]+z4[0])/12;

psi[2]=psi[3]+(z1[1]+2*z2[1]+2*z3[1]+z4[1])/12;

t+=h;

k++;

out_file<<"\t\t k = "<<k<<"\n";

out_file<<"t = "<<t;

out_file<<"\t"<<" q = "<<q[2];

out_file<<"\t"<<" psi = "<<psi[2]<<"\n";

z1[0]=m_func1(q[2],psi[2],t);

z1[1]=m_func2(q[2],psi[2],t);

z2[0]=m_func1( (q[2]+h*z1[0]/2), (psi[2]+h*z1[1]/2), (t+h/2) );

z2[1]=m_func2( (q[2]+h*z1[0]/2), (psi[2]+h*z1[1]/2), (t+h/2) );

z3[0]=m_func1( (q[2]+h*z2[0]/2), (psi[2]+h*z2[1]/2), (t+h/2) );

z3[1]=m_func2( (q[2]+h*z2[0]/2), (psi[2]+h*z2[1]/2), (t+h/2) );

z4[0]=m_func1( (q[2]+h*z3[0]), (psi[2]+h*z3[1]), (t+h) );

z4[1]=m_func2( (q[2]+h*z3[0]), (psi[2]+h*z3[1]), (t+h) );

q[1]=q[2]+(z1[0]+2*z2[0]+2*z3[0]+z4[0])/12;

psi[1]=psi[2]+(z1[1]+2*z2[1]+2*z3[1]+z4[1])/12;

t+=h;

k++;

q[0]=q[1];

psi[0]=psi[1];

out_file<<"\t\t k = "<<k<<"\n";

out_file<<"t = "<<t;

out_file<<"\t"<<" q = "<<q[0];

out_file<<"\t"<<" psi = "<<psi[0]<<"\n";

}

//--------------Additional functions for Gear method---------------------------

double g_func1(double g_q, double g_psi, double time)

{

return (g_q-(18*q[1])/11+(9*q[2])/11-(2*q[3])/11-(6*h*m_func1(g_q, g_psi, time))/11);

}

double g_func2(double g_q, double g_psi, double time)

{

return (g_psi-(18*psi[1]/11)+(9*psi[2]/11)-(2*psi[3]/11)-(6*h*m_func2(g_q, g_psi, time)/11));

}

double jackobian(double g_psi)

{

return ( (1+6*h/11)+(18*h*h/121)*(1-0.15*g_psi*g_psi) );

}

double delta1(double g_q, double g_psi, double time)

{

return ( g_func1(g_q, g_psi, time) + g_func2(g_q, g_psi, time)*(6*h/11)*(1-0.15*g_psi*g_psi));

}

double delta2(double g_q, double g_psi, double time)

{

return ( (1+6*h/11)*g_func2(g_q, g_psi, time) - (3*h/11)*g_func1(g_q, g_psi, time) );

}

double max(double x,double y)

{

if (fabs(x)>=fabs(y)) return(fabs(x));

else return(fabs(y));

}

void Newton_Raffson()

{

double d_bug[2];

double q_k1[2]={q[0], 0},

psi_k1[2]={psi[0], 0},

delta[2];

do

{

q_k1[1]=q_k1[0];

psi_k1[1]=psi_k1[0];

delta[0]=delta1(q_k1[1], psi_k1[1], t)/jackobian(psi_k1[1]);

delta[1]=delta2(q_k1[1], psi_k1[1], t)/jackobian(psi_k1[1]);

q_k1[0]=q_k1[1]-delta[0];

psi_k1[0]=psi_k1[1]-delta[1];

d_bug[0]=g_func1(q_k1[0],psi_k1[0],t);

d_bug[1]=g_func2(q_k1[0],psi_k1[0],t);

}

while ( (max(d_bug[0], d_bug[1]))>eps );

q[0]=q_k1[0];

psi[0]=psi_k1[0];

}

void Gear()

{

while (t<=15)

{

t+=h;

k++;

Newton_Raffson();

out_file<<"\t\t k = "<<k<<"\n";

out_file<<"t = "<<t;

out_file<<"\t"<<" q = "<<q[0];

out_file<<"\t"<<" psi = "<<psi[0]<<"\n";

q[3]=q[2];

psi[3]=psi[2];

q[2]=q[1];

psi[2]=psi[1];

q[1]=q[0];

psi[1]=psi[0];

}

}

//--------------------------MAIN-----------------------------------------------

void main()

{

out_file.open("report.txt");

out_file<<"\t\t k = "<<k<<"\n";

out_file<<"t = "<<t;

out_file<<"\t"<<" q = "<<q[3];

out_file<<"\t"<<" psi = "<<psi[3]<<"\n";

Runge_Kutta();

Gear();

out_file.close();

}

Соседние файлы в папке _Разное