利用matlab编写一个求解函数零点的二分法程序

利用matlab编写一个求解函数零点的二分法程序,第1张

%%二分法clcclearformat longsyms x;f1=sin(x)-xx/2;%给定求解区间a=1;b=2;er=1;%误差初值k=0;%迭代次数初值while (er>5e-6) ; c=(a+b)/2; if subs(f1,a)subs(f1,c)>0; a=c; else b=c; end er=b-a;%求出误差 k=k+1;%计迭代次数enddisp('解为')disp(c)%给出解disp('迭代次数')disp(k)disp('误差')disp(er)

这是源代码:

在matlab中保存为:bisectionm

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 fcfa<0

xb=xc;

else xa=xc;

end

if (xb-xa)<delta,break,end

end

>>f='x^3-x-1';

>>bisection(f,1,15,20,10^(-3))

[ n xa xb xc fc ]

10000 10000 15000 12500 -02969

20000 12500 15000 13750 02246

30000 12500 13750 13125 -00515

40000 13125 13750 13438 00826

50000 13125 13438 13281 00146

60000 13125 13281 13203 -00187

70000 13203 13281 13242 -00021

80000 13242 13281 13262 00062

90000 13242 13262 13252 00020

从结果可以看出,

这个解为:13262

a=1;

b=2;

f =@(x)x^3-x-1;

c=(a+b)/2;

while abs(b-a)>1e-5

if f(c)f(b)<0

a=c;

else

b=c;

end

c=(a+b)/2;

x=c;

end

fprintf('\n x = %5f, f(x) = %5f \n', x, f(x));

MATLAB 产品系列重要功能:

MATLAB®: MATLAB 语言的单元测试框架

Trading Toolbox™: 一款用于访问价格并将订单发送到交易系统的新产品

Financial Instruments Toolbox™: 赫尔-怀特、线性高斯和 LIBOR 市场模型的校准和 Monte Carlo 仿真

Image Processing Toolbox™: 使用有效轮廓进行图像分割、对 10 个函数实现 C 代码生成,对 11 个函数使用 GPU 加速

Image Acquisition Toolbox™: 提供了用于采集图像、深度图和框架数据的 Kinect® for Windows®传感器支持

Data Acquisition Toolbox™: 为 Digilent Analog Discovery Design Kit 提供了支持包

Vehicle Network Toolbox™: 为访问 CAN 总线上的 ECU 提供 XCPSimulink 产品系列重要功能

Simulink®: Simulation Performance Advisor,链接库模块的封装,以及通过逻辑表达式控制有效变量

Simulink: 除 LEGO® MINDSTORMS® NXT、Arduino®、Pandaboard 和 Beagleboard 外,还为 RaspberryPi™ 和 Gumstix® Overo® 硬件提供了内置支持

SimRF™: 针对快速仿真和模型加载时间的电路包络求解器

SimMechanics™: 发布了用于从 CAD 和其他系统导入模型的 XML 架构

Simulink Design Verifier™: 数组超出边界检查

以上就是关于利用matlab编写一个求解函数零点的二分法程序全部的内容,包括:利用matlab编写一个求解函数零点的二分法程序、利用matlab 编程 1.求用方程求根的二分法求方程x3-x-1=0在区间[1,1.5]内的一个实根,要求误差小于0.005。2.、如何用matlab编写二分法问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/9412031.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-28
下一篇2023-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存