二分法求方程解

一、此法要求函数连续,在给定区间内仅有一个根,通过循环十几次即可获得精度要求较高的根(0.00001),精度可自己设定

理论依据为函数连续性,在根附近有:f(start) * f(end) < 0

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define E 2.718
float f(float);

int main(){
float start, end, mid, precision;
int i, count;

//三个初值,每次都要赋值
start = 0; //区间左起点
end = 1; //右起点
precision =1e-5; //精度

//获取需要循环次数
count = ceil((log(end - start)-log(2*precision))/log(2));
for(i=0; i<count; i++){
mid =0.5*(start + end);
if(f(mid)*f(start)<0){
end = mid;
}
else if(f(mid)*f(start)>0){
start = mid;
}else{
//等于0,即为 mid 值,直接退出
printf("%.4f 循环次数%d\n", mid, count);
exit(0);
}
}

mid =0.5*(start + end);
printf("解为%.4f,循环次数%d\n", mid, count);

return 0;
}

//待判断的函数表达式,每次都要修改
float f(float x){
//return (x*x*x - 2*x*x -4*x -7);
//return (x*x - x - 1);
//return (x*x*x - x - 1);
//return (x*x*x - 2);
//return (x - pow(2, -x));
return (pow(E, x) - x*x + 3*x - 2);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mathematica 是数学领域的一个巨人,发布的 Mathematica 软件在各个前沿行业的科学计算,仿真,建模等应用中发挥着关键作用。 我下载了45个典型的 Mathematica 演示范例,供打包下载学习。 AreTheseLinesStraight-source.nb BoilingPointOfWaterOnSeveralPlanets-source.nb CassiniSpaceflight-source.nb CellularAutomataOnTrivalentNetworks-source.nb CIEChromaticityDiagram-source.nb CogwheelDrive-source.nb ComplexAdditionOfHarmonicMotionsAndThePhenomenonOfBeats-source. CosineOffsetCurves-source.nb CurlicueFractal-source.nb DayAndNightWorldClock-source.nb DigitalTiltShiftPhotography-source.nb Emoticon-source.nb IcosahedronBall-source.nb InsulinMolecule-source.nb JackLewSignatureFunction-source.nb KeyboardAndComposer-source.nb KomanVariations-source.nb LengthScalesInTheSolarSystem-source.nb LeonardoDaVincisOrnithopter-source.nb LookingAtACube-source.nb MicrowaveOven-source.nb MosaicEffectForPhotographicImages-source (1).nb MosaicEffectForPhotographicImages-source.nb Mountainscape-source.nb PicturePuzzle-source.nb PicturePuzzle.cdf PolynomialAndDerivative-source.nb RadialEngine-source.nb ReliefShadedElevationMap-source.nb SaturnsSeasonalSundial-source.nb SegmentingAMedicalImage-source.nb SmileyChanger-source.nb SpringReturnButton-source.nb SunflowerSeedArrangements-source.nb SunflowerSeedArrangements-source.pdf SurfaceWithBranchCuts-source.nb TermWeightingWithTFIDF-source.nb TheUlamPrimeSpiral-source.nb ThickeningAPolygonMeshForRapidPrototyping3DPrinting-source.nb TravelingSalesmanArt-source.nb TreeBender-source.nb Tries-source.nb TwentyDodecahedronTowers-source.nb VoronoiImage-source.nb WhirlingPolygons-source.nb

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值