C ++中的Euler方法帮助 [英] Help in Euler method in c++

查看:92
本文介绍了C ++中的Euler方法帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
我是c ++的初学者,请帮忙.我实现了用于求解简单ODE(y''= x-y,y(0)= 1)的Euler方法,并且它在时间上是向前的(从t = 0到t = 1),并且效果很好,我的问题是是:我想及时向后运行此代码(t = 1到t = 0).

我必须在代码中进行哪些更改?

Hi everybody!
I am a beginner in c++ and I need your help please. I implemented Euler''s method for solving simple ODEs (y'' = x - y, y(0)=1)and it is forward in time (from t=0 to t=1) and it worked well, my question is : I want to run this code backward in time (t=1 to t=0).

What do I have to change in my code?

/* Euler for a set of first order differential equations */

#include <stdio.h>
#include <math.h>
#define dist 0.2               /* stepsize in t*/
#define MAX 1.0                /* max for t */
int N=1;

void euler(double x, double y[], double step); /* Euler function */

double f(double x, double y[], int i);          /* function for derivatives */

main()
{
    double t, y[N];
    int j;

    y[0]=1.0;                                       /* initial condition */

    for (j=0; j*dist<=MAX ;j++)                     /* time loop */
    {
       t=j*dist;
       printf("j =  %d,t = %f y[0] = %f\n", j,t, y[0]);
       euler(t, y, dist);
    }
}

void euler(double x, double y[], double step)
{
    double  s[N];      /* for euler */
    int i;

    for (i=0;i<N;i++)
    {     
        s[i]=step*f(x, y, i);
    }

    for (i=0;i<N;i++) 
        y[i]+=s[i];
}

double f(double x, double y[], int i)
{
    if (i==0) 
        return(x-y[0]);                 /* derivative of first equation */
}



提前非常感谢!



Many thanks in advance!

推荐答案

1.与C ++相比,这更是一个算法问题,因为如果不了解Euler算法,就很难给出正确的答案.您应该已经将算法"添加为关键字.

2. Eulers方法需要一个起始值.您有t=1.0的起始值吗?还是要从t=0.0开始并向后运行到t=-1.0?

3.函数f()应该始终返回一个值.当前仅对i==0执行此操作.您的编译器应对此发出警告-不要忽略它!即使此刻没有发生任何不良情况,但如果您更改N的值,那将会成功!

关于您的问题,您只需要使用负dist值并修改for循环,以使t递减.请注意,除非您确实希望在打印输出中对步骤编号,否则不需要变量j.对于从t=1.0向后到0.0的循环,您可以定义for循环,如下所示:
1. This is more a question of algorithm than C++, since without knowledge of the Euler algorithm it''s not easy to give a correct answer. You should have added ''algorithm'' as a keyword.

2. Eulers method requires a starting value. Do you have the starting value for t=1.0? Or do you want to start at t=0.0 and run backward to t=-1.0?

3. Your function f() should always return a value. It currently does so only for i==0. Your compiler should have issued a warning about that - do not ignore it! Even though at the moment nothing bad happens, it will if you ever change your value for N!

Regarding your question, you only need to use a negative dist value and modify the for loop so that t gets decremented. Note that unless you really want your steps numbered in your printout, you do not need the variable j. For a loop from t=1.0 backwards to 0.0 you can define the for loop just like this:
#define dist -0.2
for (t = 1.0; t >= 0.0; t += dist) // as dist is negative you actually decrement t here
{
  printf(...);
  euler(...);
}






[edit: fixed the loop to run from 1.0 to 0.0]
[edit 2: fixed dist value to make sure euler() gets the correct, negative step value]


目前还不清楚您要计算的内容:
Its very unclear what you are calculating:
y' = x - y
y' = dx/dy
dx = -y
dy = x

-y / x  = x - y
xy - y  = x²
y*(x-1) = x²
y       = x²/(x-1)
y' = x - x²/(x-1)
   = x² - x - x² / (x-1)
   = -x / (x-1)



最后一行是您的问题.您的代码计算出的值不正确.
Excel的简短表格:



The last line is your problem. The calculated values from your code are not correct.
Short table from Excel:

x           y           y'
0.000000    0.000000    0.000000
0.100000    -0.011111   0.111111
0.200000    -0.050000   0.250000
0.300000    -0.128571   0.428571
0.400000    -0.266667   0.666667
0.500000    -0.500000   1.000000
0.600000    -0.900000   1.500000
0.700000    -1.633333   2.333333
0.800000    -3.200000   4.000000
0.900000    -8.100000   9.000000
1.000000    #DIV/0!   #DIV/0!



公式:y''= x-y
您想要设置x = t和y = f(t),应该得到上面的结果.
问候.



formula: y'' = x - y
you want to set x = t and y = f(t), you should get the results above.
Regards.


这篇关于C ++中的Euler方法帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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