峰值并计算runge kutta的误差 [英] Peak value and calculate error for the runge kutta

查看:93
本文介绍了峰值并计算runge kutta的误差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用runge-kutta方法计算相对于时间的微分方程。我附加了我的代码,但我需要进一步的帮助,以便在代码中的43秒时找到峰值。我需要有关如何接近Z()峰值的帮助。

  #include   <   stdio.h   >  
#include < conio.h >
#include < time .h >
#include < math.h > ;
#include < stdlib.h >
#include winbgi2.h
#define n 3
#define dist 0.1
#define max 50
void rungekutta( double x, double y [],步骤);
double fun( double x, double y [], int i);

void main()
{
graphics( 50 30 );
double t,z [n];
int j;
FILE * f;
fopen_s(& f, padela.txt w +);
t = 0 0 ;
z [ 0 ] = 0 ;
z [ 1 ] = 0 ;
z [ 2 ] = 0 ;
fprintf(f, time\t\tZ-axis\\\
);
fprintf(f, %lf \t \ t%lf \ n,t,z [ 2 ]);
for (j = 1 ; j * dist< = max; j ++)
{
t = j * dist;
rungekutta(t,z,dist);
fprintf(f, %lf \t \ t%lf \ n,t,z [ 2 ]);
circle(t + 200,z [ 2 ] + 200, 7 );
}

fclose(f);
getch();
}

void rungekutta( double x, double z [], double 步骤)
{
double h = step / 2 0
t1 [n ],t2 [n],t3 [n],
k1 [n],k2 [n],k3 [n],k4 [n];
int i;
for (i = 0 ; i< n; i ++)
{
t1 [i] = z [i] + 0 5 *(k1 [i] = step * fun(x,z,i));
}
for (i = 0 ; i< n; i ++)
{
t2 [i] = z [i] + 0 5 * (k2 [i] = step * fun(x + h,t1,i));
}
for (i = 0 ; i< n; i ++)
{
t3 [i] = z [i] +(k3 [i] = step * fun(x + h,t2,i));
}
for (i = 0 ; i< n; i ++)
{
k4 [i] = step * fun(x + step,t3,i);
}
for (i = 0 ; i< n; i ++)
{
z [i] + =(k1 [i] + 2 * k2 [i] + 2 * k3 [i] + k4 [i])/ 6 0 ;
}
}

double fun( double x, double z [], int i)
{
double a = 0 2 ,b = 0 2 ,c = 5 7 ;
if (i == 0
{
return -z [ 1 ] - z [ 2 ]。
}
if (i == 1
{
return z [ 0 ] +(a * z [ 1 ]);
}
if (i == 2
{
return b +(z [ 2 ] *(z [ 0 ] - C));
}
}





我的尝试:



我需要有关如何接近Z()峰值的帮助。

解决方案

你必须存储你的最大值和检查它,而不是只更新新的最大值。最好是存储时间值。



更好地编写代码,因为它更清晰,因此避免了错误:

< pre lang =c ++> for (i = 0 ; i< n; i ++)
{
k2 [i] = step * fun(x + h,t1,i);
t2 [i] = z [i] + 0 5 *(k2 [i ]);
}

使用调试器并输出更多。


I had to calculate differential equation with respect to time using the runge-kutta method. I have attached my code but i need further help to also find the peak value at time 43 secs in the code. I need help on how to approach for peak value of Z().

#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include "winbgi2.h"
#define n 3
#define dist 0.1
#define max 50
void rungekutta(double x, double y[], double step);     
double fun(double x, double y[], int i);

void main()
{
   graphics(50,30);
   double t, z[n];
   int j;
   FILE *f;
   fopen_s(&f, "padela.txt", "w+");          
   t = 0.0;
   z[0] = 0;            
   z[1] = 0;
   z[2] = 0;
   fprintf(f,"time\t\tZ-axis\n");
   fprintf(f, "%lf\t\t%lf\n",t,z[2]);
   for (j = 1; j*dist <= max; j++)
   {
      t = j*dist;
      rungekutta(t,z,dist);
      fprintf(f, "%lf\t\t%lf\n",t,z[2]);
      circle(t+200,z[2]+200,7);
   }

   fclose(f);
   getch();
}

void rungekutta(double x, double z[], double step)
{ 
   double h = step / 2.0,        
   t1[n], t2[n], t3[n],          
   k1[n], k2[n], k3[n], k4[n];   
   int i;
   for (i = 0; i<n; i++)
   {
      t1[i] = z[i] + 0.5*(k1[i] = step*fun(x, z, i));
   }
   for (i = 0; i<n; i++)
   {
      t2[i] = z[i] + 0.5*(k2[i] = step*fun(x + h, t1, i));
   }
   for (i = 0; i<n; i++)
   {
      t3[i] = z[i] + (k3[i] = step*fun(x + h, t2, i));
   }
   for (i = 0; i<n; i++)
   {
      k4[i] = step*fun(x + step, t3, i);
   }
   for (i = 0; i<n; i++)
   {
      z[i] += (k1[i] + 2 * k2[i] + 2 * k3[i] + k4[i]) / 6.0;
   }
}

double fun(double x, double z[], int i)
{
   double a=0.2,b=0.2,c=5.7;
   if (i == 0)
   {
      return -z[1]-z[2];
   }
   if (i == 1)
   {
      return z[0]+(a*z[1]);
   }
   if (i == 2)
   {
      return b+(z[2]*(z[0]-c));
   }
}



What I have tried:

I need help on how to approach for peak value of Z().

解决方案

You must store your max value and check against it, than only update the new max. Best is to store also the time value.

Write your code better like this, because it is clearer and so avoids bugs:

for (i = 0; i<n; i++)
  {
     k2[i] = step*fun(x + h, t1, i);
     t2[i] = z[i] + 0.5*(k2[i]);
  }

Use the debugger and make more output.


这篇关于峰值并计算runge kutta的误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆