使用Polyline(),C ++绘制欧拉积分 [英] Plotting Euler Integration using Polyline(), C++

查看:216
本文介绍了使用Polyline(),C ++绘制欧拉积分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图绘制这个Euler集成函数的输出:

So I'm trying to plot the output of this Euler integration function:

typedef double F(double,double);
using std::vector;

void euler(F f, double y0, double a, double b, double h,vector<POINT> Points)
{

POINT Pt;
double y_n = y0;
double t = a;
for (double t = a; t != b; t += h )

{
    y_n += h * f(t, y_n); 

    Pt.x = t; // assign the x value of the point to t.
    Pt.y = y_n; // assign the y value of the point to y_n.
    Points.push_back(Pt);

}


}


// Example: Newton's cooling law
double newtonCoolingLaw(double, double t) 
{
    return t; // return statement ends the function; here, it gives the time derivative y' = -0.07 * (t - 20)
}

我试图在Win32应用程序中使用Polyline()函数,所以我在 WM_PAINT 下执行此操作:

I'm trying to use the Polyline() function in a Win32 application, so I do this under the case WM_PAINT:

case WM_PAINT:
    {

    hdc = BeginPaint(hWnd, &ps);

  //Draw lines to screen.

    hPen = CreatePen(PS_SOLID, 1, RGB(255, 25, 5));
    SelectObject(hdc, hPen);


    using std::vector;
    vector<POINT> Points(0);

    euler(newtonCoolingLaw, 1, 0, 20, 1,Points);

    POINT tmp = Points.at(0);
    const POINT* elementPoints[1] = { &tmp };

    int numberpoints = (int) Points.size() - 1 ;


    Polyline(hdc,elementPoints[1],numberpoints);

当我将I / O重新路由到控制台时,下面是变量的输出:

When I reroute my I/O to console, here are the outputs for the variables:

>

我可以使用 MovetoEx(hdc,0,0,NULL) LineTo(hdc,20,20),但是由于某些原因,这些函数都不能与我的向量< POINT> Points 。有任何建议吗?

I'm able to draw the expected lines to the screen using MovetoEx(hdc,0,0,NULL) and LineTo(hdc,20,20), but for some reason none of these functions will work with my vector<POINT> Points. Any suggestions?

推荐答案

有多种情况对我来说似乎有误:

There are multiple things that seem erroneous to me:

1)您应该通过引用传递向量或作为返回值:

1) You should pass the vector by reference or as a return value:

void euler(/*...*/,vector<POINT>& Points)

目前你只是将一个副本传递给函数,因此原始向量不会被修改。

Currently you are only passing a copy into the function, so the original vector will not be modified.

2)不要在for-loop标头中比较(in-)相等的双精度。双精度有一个有限的精度,所以如果b比h大得多,你的循环可能永远不会终止,因为t可能不会完全匹配b。比较较小代替:

2) Don't compare doubles for (in-)equality in your for-loop header. Doubles have a limited precision, so if b is much bigger than h, your loop might never terminate, as t might never exactly match b. Compare for "smaller" instead:

for (double t = a; t < b; t += h )

3)为什么要将elementPoints声明为大小为1的指针数组?不是一个简单的指针:

3) Why are you declaring elementPoints as an array of pointers of size 1? Wouldn't a simple pointer do:

const POINT* elementPoints =  &tmp ; //EDIT: see point 5)

4)调用时出现一个错误 Polyline

4) You have an of-by-one error when calling Polyline. If you want to stick with the array at all use.

Polyline(hdc,elementPoints[0],numberpoints);

编辑:对不起,我忘了一个重要的:

Sorry, I forgot an important one:

5)在您的代码中, elementPoints [0] 指向单个 double tmp ),而不是向量中的数组。如果您声明 tmp 作为参考,这可能会奏效:

5) In your code, elementPoints[0] points to a single double (tmp) and not to the array inside of the vector. This would probably work, if you declared tmpas a reference:

POINT& tmp = Points.at(0); //I'm wondering why this doesn't throw an exception, as the vector should actually be empty here

但是,我想你实际想要做的是摆脱 tmp elementPoints 最后一行:

However, I think what you actually want to do is to get rid of tmp and elementPoints altogether and write in the last line:

Polyline(hdc,&Points[0],(int) Points.size()-1);
//Or probably rather:
Polyline(hdc,&Points[0],(int) Points.size());

Btw .: -1

这篇关于使用Polyline(),C ++绘制欧拉积分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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