?? c6.cpp
字號:
//C6.cpp
//Adanved Euler's Methom
#include<iostream.h>
const N=20;
double f(double x,double y)
{
return x*y*y*(-1);
}
//n為[0,5]之間欲求離散點個數,將會設為20
void EulerMethom(double x0,double y0,int n)
{
double y1,h;
int i;
h=5/(double)n;
for (i=0;i<n;i++)
{
y1=y0+h*f(x0+i*h,y0);
y1=y0+h*(f(x0+(i+1)*h,y1)+f(x0+i*h,y0))/2;
cout<<"The vaule at "<<(i+1)*h<<"is: "<<y1<<endl;
y0=y1;
}
}
void main()
{
double x0=0,y0=2;
EulerMethom(x0,y0,N);
}
//N=500時,y(2.5)=0.275871
//N=20時,y(2.5)=0.282357,距精確值0.275862相當遠
//迭代次數對結果精確性影響非常大,N=500時,程序耗時2秒多
//且有相當的誤差,Euler法確實不敢恭維
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -