没有合适的资源?快使用搜索试试~ 我知道了~
实现牛顿插值,很好的资源,能够实现牛顿插值的C语言
资源推荐
资源详情
资源评论
C 语言编程习题
第二章
习题 2-2
5. 用二分法编程求 6x
4
-40x
2
+9=0 的所有实根。
#include <stdio.h>
#include <math.h>
#dene N 10000
double A,B,C;
double f(double x)
{
return (A*x*x*x*x+B*x*x+C);
}
void BM(double a,double b,double eps1,double eps2)
{
int k;
double x,xe;
double valuea = f(a);
double valueb = f(b);
if (valuea > 0 && valueb > 0 || valuea <0 && valueb < 0) return;
printf("Finding root in the range: [%.3lf, %.3lf]", a, b);
for(k=1;k<=N;k++) {
x=(a+b)/2;
xe=(b-a)/2;
if(fabs(xe)<eps2 || fabs(f(x))<eps1) {
printf("The x value is:%g",x);
printf("f(x)=%g",f(x));
return;
}
if(f(a)*f(x)<0) b=x;
else a=x;
}
printf("No convergence!");
}
1
int main()
{
double a,b,eps1,eps2,step,start;
printf("Please input A,B,C:\n");
scanf("%lf %lf %lf",&A,&B,&C);
printf("Please input a,b, step, eps1,eps2:\n");
scanf("%lf %lf %lf %lf %lf",&a,&b,&step,&eps1,&eps2);
for (start=a; (start+step) <= b; start += step) {
double left = start;
double right = start + step;
BM(left, right, eps1, eps2);
}
return 0;
}
运行:
Please input A,B,C:
6 -40 9
Please input a,b, step, eps1,eps2:
-10 10 1 1e-5 1e-5
Finding root in the range: [-3.000, -2.000]
The x value is:-2.53643
f(x)=-0.00124902
Finding root in the range: [-1.000, 0.000]
The x value is:-0.482857
f(x)=0.00012967
Finding root in the range: [0.000, 1.000]
The x value is:0.482857
f(x)=0.00012967
Finding root in the range: [2.000, 3.000]
The x value is:2.53643
f(x)=-0.00124902
有时若把判别语句
2
if(fabs(xe)<eps2 || fabs(f(x))<eps1)
改为
if(fabs(xe)<eps2 && fabs(f(x))<eps1)
会提高精度,对同一题运行结果:
Finding root in the range: [-3.000, -2.000]
The x value is:-2.53644
f(x)=-4.26496e-007
Finding root in the range: [-1.000, 0.000]
The x value is:-0.482861
f(x)=-7.3797e-006
Finding root in the range: [0.000, 1.000]
The x value is:0.482861
f(x)=-7.3797e-006
Finding root in the range: [2.000, 3.000]
The x value is:2.53644
f(x)=-4.26496e-007
习题 2-3
5. 请用埃特金方法编程求出 x=tgx 在 4.5(弧度)附近的根。
#include <stdio.h>
#include <math.h>
#dene N 100
#dene PI 3.1415926
void SM(double x0,double eps)
{
int k;
double x;
double x1,x2;
for(k=1;k<=N;k++) {
x1=sin(x0)/cos(x0);
x2=sin(x1)/cos(x1);
x=x0-(x1-x0)*(x1-x0)/(x2-2*x1+x0);
if(fabs(x-x0)<eps) {
3
printf("The x value is:%g\n",x);
return;
}
x0=x;
}
printf("No convegence!\n");
}
int main()
{
double eps,x0;
printf("Please input eps,x0:\n");
scanf("%lf %lf",&eps,&x0);
SM(x0,eps);
return 0;
}
运行:
Please input eps,x0:
1e-5 4.5
The x value is:4.49341
习题 2-4
11.请编出用牛顿法求复根的程序,并求出
P(z)=z
4
-3z
3
+20z
2
+44z+54=0
接近于 z
0
=2.5+4.5i 的零点。
#include <cstdio>
#include <cmath>
#define MAX_TIMES 1000
typedef struct {
double real, image;
} COMPLEX;
COMPLEX Aa[5]={{54,0},{44,0},{20,0},{-3,0},{1,0}};
COMPLEX Bb[4]={{44,0},{40,0},{-9,0},{4,0}};
COMPLEX zero = {0, 0};
4
double eps1=1e-6;
double eps2=1e-6;
COMPLEX multi(COMPLEX a,COMPLEX b)
{
COMPLEX result;
result.real = a.real * b.real - a.image * b.image;
result.image = a.image * b.real + a.real * b.image;
return result;
}
COMPLEX Div(COMPLEX a,COMPLEX b){
COMPLEX z3;
double s;
s=(b.real*b.real)+(b.image*b.image);
z3.real=b.real;
z3.image=-b.image;
z3=multi(a,z3);
z3.real=z3.real/s;
z3.image=z3.image/s;
return z3;
}
COMPLEX add(COMPLEX a,COMPLEX b)
{
COMPLEX result;
result.real = a.real + b.real;
result.image = a.image + b.image;
return result;
}
COMPLEX subtract(COMPLEX a, COMPLEX b)
{
COMPLEX result;
result.real = a.real - b.real;
result.image = a.image - b.image;
return result;
}
COMPLEX times(COMPLEX z,int n){
int i;
COMPLEX result={1, 0};
for (i=0;i<n;i++) result=multi(result,z);
return result;
}
5
剩余43页未读,继续阅读
资源评论
haohaoduo10
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功