改進的歐拉方法求解微分方程
題目要求:
已知微分方程的初值問題如下:
求y(1)的值。
#include "stdio.h"
#include "math.h"
double NewtonFun(double x)
{
return x-(x-exp(-x))/(1 x);
}
double getResult(double x1,double accuracy)
{
double x2 = 0.0;
x2 = NewtonFun(x1);
while(fabs(x2-x1)>=accuracy)
{
x1 = x2;
x2 = NewtonFun(x1);
}
return x2;
}
main()
{
double x1,accuracy;
printf("Please input the initial value\n"); /*輸入叠代初值*/
scanf("%lf",&x1);
printf("Please input accuracy\n"); /*輸入叠代精度*/
scanf("%lf",&accuracy);
printf("The result of function is %lf\n",getResult(x1,accuracy));
getche();
}
雅可比叠代公式求解線性方程組
題目要求:
求解線性方程組:
#include "stdio.h"
float func(float x,float y)
{
return y - 2*x / y;
}
float getResult(float x0,float y0,float h,float xn)
{
float yn;
while(x0<xn)
{
yn = y0 h*func(x0,y0);
y0 = yn;
x0 = x0 h;
}
return yn;
}
main()
{
float x0,y0,h,xn;
printf("Please input the initial value x0 & y0\n");
/*輸入初始值x0和y0*/
scanf("%f %f",&x0,&y0);
printf("Please input the step length\n"); /*輸入步長值h*/
scanf("%f",&h);
printf("Please input Xn\n"); /*輸入要求的y(xn)的xn*/
scanf("%f",&xn);
printf("The result of the function is %f\n",getResult(x0,y0,h,xn));
getche();
}
更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!