用二分法解方程,求在[-10,10]之间的解 2*x^3-4*x^2+3*x-6=0用c++该如何编写? 10
展开全部
#include <stdio.h>
#include <math.h>
typedef double (*Fun)(double);
int sign(double x)
{
return x > 0.0 ? 1 : x < 0.0 ? -1 : 0;
}
double bisection(double a, double b, Fun f) //二分法
{
double m, fa, fb, fm;
fa = f(a); fb = f(b);
if(sign(fa) == sign(fb))
{
printf("error\n");
return 0.0;
}
while(fabs(b - a) > 1e-12)
{
m = a + (b - a) * 0.5;
fm = f(m);
if(sign(fa) == sign(fm))
{
a = m;
fa = fm;
}
else
{
b = m;
fb = fm;
}
}
return m;
}
double secant (double x0, double x1, Fun f) //割线法
{
double f0, f1, x2;
f0 = f(x0); f1 = f(x1);
x2 = x1 - f1 * (x1 - x0) / (f1 - f0);
while(fabs(x2 - x1) > 1e-12)
{
x0 = x1, f0 = f1;
x1 = x2, f1 = f(x1);
x2 = x1 - f1 * (x1 - x0) / (f1 - f0);
}
return x2;
}
double newtown(double x0, Fun f, Fun df) //牛顿法
{
double x1 = x0 - f(x0) / df(x0);
while(fabs(x0 - x1) > 1e-12)
{
x0 = x1;
x1 = x0 - f(x0) / df(x0);
}
return x1;
}
double fun(double x)
{
return x * (2.0 * x * (x - 2.0) + 3.0) -6.0;
}
double dfun(double x)
{
return x * (6.0 * x - 8.0) + 3.0;
}
int main()
{
double ret;
ret = bisection(-10.0, 10.0, fun);
printf("%.10f\n", ret);
printf("f(%.10f) = %.10f\n", ret, fun(ret));
ret = secant(1.0, 2.0, fun);
printf("%.10f\n", ret);
printf("f(%.10f) = %.10f\n", ret, fun(ret));
ret = newtown(1.0, fun, dfun);
printf("%.10f\n", ret);
printf("f(%.10f) = %.10f\n", ret, fun(ret));
return 0;
}
#include <math.h>
typedef double (*Fun)(double);
int sign(double x)
{
return x > 0.0 ? 1 : x < 0.0 ? -1 : 0;
}
double bisection(double a, double b, Fun f) //二分法
{
double m, fa, fb, fm;
fa = f(a); fb = f(b);
if(sign(fa) == sign(fb))
{
printf("error\n");
return 0.0;
}
while(fabs(b - a) > 1e-12)
{
m = a + (b - a) * 0.5;
fm = f(m);
if(sign(fa) == sign(fm))
{
a = m;
fa = fm;
}
else
{
b = m;
fb = fm;
}
}
return m;
}
double secant (double x0, double x1, Fun f) //割线法
{
double f0, f1, x2;
f0 = f(x0); f1 = f(x1);
x2 = x1 - f1 * (x1 - x0) / (f1 - f0);
while(fabs(x2 - x1) > 1e-12)
{
x0 = x1, f0 = f1;
x1 = x2, f1 = f(x1);
x2 = x1 - f1 * (x1 - x0) / (f1 - f0);
}
return x2;
}
double newtown(double x0, Fun f, Fun df) //牛顿法
{
double x1 = x0 - f(x0) / df(x0);
while(fabs(x0 - x1) > 1e-12)
{
x0 = x1;
x1 = x0 - f(x0) / df(x0);
}
return x1;
}
double fun(double x)
{
return x * (2.0 * x * (x - 2.0) + 3.0) -6.0;
}
double dfun(double x)
{
return x * (6.0 * x - 8.0) + 3.0;
}
int main()
{
double ret;
ret = bisection(-10.0, 10.0, fun);
printf("%.10f\n", ret);
printf("f(%.10f) = %.10f\n", ret, fun(ret));
ret = secant(1.0, 2.0, fun);
printf("%.10f\n", ret);
printf("f(%.10f) = %.10f\n", ret, fun(ret));
ret = newtown(1.0, fun, dfun);
printf("%.10f\n", ret);
printf("f(%.10f) = %.10f\n", ret, fun(ret));
return 0;
}
展开全部
表1
函数
的零点取值区间表
中点
中点函数值
取值区间
f(-10)<0,f(10)>0
(-10,10)
0
f(0)<0
(0,10)
5
f(5)>0
(0,5)
2.5
f(2.5)>0
(0,2.5)
1.25
f(1.25)<0
(1.25,2.5)
……下面的自己算吧……看该题是要精确到多少位,来确定算到多少位,
用matlab就好算了
function
rtn=bisection(fx,xa,xb,n,delta)
%
bisection
method
%
the
first
parameter
fx
is
a
external
function
with
respect
to
viable
x.
%
xa
is
the
left
point
of
the
initial
interval
%
xb
is
the
right
point
of
the
initial
interval
%
n
is
the
number
of
iterations.
x=xa;fa=eval(fx);
x=xb;fb=eval(fx);
disp('
[
n
xa
xb
xc
fc
]');
for
i=1:n
xc=(xa+xb)/2;x=xc;fc=eval(fx);
x=[i,xa,xb,xc,fc];
disp(x),
if
fc*fa<0
xb=xc;
else
xa=xc;
end
if
(xb-xa)
>f='2*x*x*x-4*x*x+3*x-6';
>>bisection(f,-10,10,20,10^(-3))
10^(-3)看你精确到几位……
函数
的零点取值区间表
中点
中点函数值
取值区间
f(-10)<0,f(10)>0
(-10,10)
0
f(0)<0
(0,10)
5
f(5)>0
(0,5)
2.5
f(2.5)>0
(0,2.5)
1.25
f(1.25)<0
(1.25,2.5)
……下面的自己算吧……看该题是要精确到多少位,来确定算到多少位,
用matlab就好算了
function
rtn=bisection(fx,xa,xb,n,delta)
%
bisection
method
%
the
first
parameter
fx
is
a
external
function
with
respect
to
viable
x.
%
xa
is
the
left
point
of
the
initial
interval
%
xb
is
the
right
point
of
the
initial
interval
%
n
is
the
number
of
iterations.
x=xa;fa=eval(fx);
x=xb;fb=eval(fx);
disp('
[
n
xa
xb
xc
fc
]');
for
i=1:n
xc=(xa+xb)/2;x=xc;fc=eval(fx);
x=[i,xa,xb,xc,fc];
disp(x),
if
fc*fa<0
xb=xc;
else
xa=xc;
end
if
(xb-xa)
>f='2*x*x*x-4*x*x+3*x-6';
>>bisection(f,-10,10,20,10^(-3))
10^(-3)看你精确到几位……
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream>
using namespace std;
double func(double x);
int main()
{ double a=1,b=1.5,p=0.005,y0,x,y;
y0=f(a);
for(int k=0;b-a>p;k++)
{
x=0.5*(a+b);
y=func(x);
if(y*y0>0) a=x;
else b=x;
cout<<"k ak bk xk"<<endl;
cout<<k<<" "<<a<<" "<<b<<" "<<x<<endl;
return 0;
}
}
double func(double x)
{ double f;
f=2*x*x*x-4*x*x+3*x-6;
return f;
}
using namespace std;
double func(double x);
int main()
{ double a=1,b=1.5,p=0.005,y0,x,y;
y0=f(a);
for(int k=0;b-a>p;k++)
{
x=0.5*(a+b);
y=func(x);
if(y*y0>0) a=x;
else b=x;
cout<<"k ak bk xk"<<endl;
cout<<k<<" "<<a<<" "<<b<<" "<<x<<endl;
return 0;
}
}
double func(double x)
{ double f;
f=2*x*x*x-4*x*x+3*x-6;
return f;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |