您当前的位置:首页 > 养生 > 内容

二分法matlab(matlab二分法编程)

本文目录

  • matlab二分法编程
  • 用matlab程序写用二分法求方程根
  • matlab怎么用二分法求方程的根
  • matlab编程题:用二分法求方程x^3-3*x-1=0的根
  • 用matlab如何编写二分法问题,谢谢
  • MATLAB二分法
  • matlab如何实现用二分法求代数方程在区间内的解
  • 怎么用matlab程序写二分法求方程根

matlab二分法编程

这是源代码:

在matlab中保存为:bisection.m

function rtn=bisection(fx,xa,xb,n,delta)

% 二分法解方程

% fx是由方程转化的关于x的函数,有fx=0。

% xa 解区间上限

% xb 解区间下限

%解区间人为判断输入

% n 最多循环步数,防止死循环。

%delta 为允许误差

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)《delta,break,end

end

例子:用二分法求方程x3-x-1=0在区间[1,1.5]内的一个实根,要求两次近似值之间的误差不超过0.001。

》》f=’x^3-x-1’;

》》bisection(f,1,1.5,20,10^(-3))

[   n        xa          xb          xc        fc  ]

    1.0000    1.0000    1.5000    1.2500   -0.2969

    2.0000    1.2500    1.5000    1.3750    0.2246

    3.0000    1.2500    1.3750    1.3125   -0.0515

    4.0000    1.3125    1.3750    1.3438    0.0826

    5.0000    1.3125    1.3438    1.3281    0.0146

    6.0000    1.3125    1.3281    1.3203   -0.0187

    7.0000    1.3203    1.3281    1.3242   -0.0021

    8.0000    1.3242    1.3281    1.3262    0.0062

    9.0000    1.3242    1.3262    1.3252    0.0020

从结果可以看出,

这个解为:1.3262 

本题的结果如下图:我选的区间为[0,1.5],你可以换个区间,再算出另一个根来(如果有的话)

x=0.0015

用matlab程序写用二分法求方程根

二分法在很多地方应该都会见到,这里是通过二分法迭代逼近的方法求出一个方程的根。

function xc = bisection(f,a,b,tol)% use the bisection method to find the root of the function% Page 30,computer problem 7(Bisection method)% input:% f:the function that transform from the equation% a,b:the left and right value of the interval which the root is in% tol:the accuracy% output:% xc:the solution of the equationif sign(f(a)) * sign(f(b)) 》=0    error(’f(a)f(b)《0 not satisfied!’)endif nargin 《 3    disp(’The function should at least include 3 parameters’);endif nargin == 3    tol = 10^-6;endwhile (b-a)/2 》 tol    c = (a + b)/2;         if f(c) == 0 % when f(c) == 0,c is a root of the function        break     end    if f(a) * f(c) 《 0                                 % a and c form a new interval        b = c;  else  % c and b form a new interval        a = c;  endendxc = (a+b)/2; % the mid_rang is the root that we find

编程问题,已经给出完整代码,没有相关配图可以配,希望谅解。

参考资料

CSDN.CSDN[引用时间2018-1-9]

matlab怎么用二分法求方程的根

  • matlab源程序如下:

  • function erfenfa(a,b)%a,b为区间,s=(a+b)/2;,while b-a》1e-5  if fun(a)*fun(s)》0。  a=s; elseif fun(a)*fun(s)《0

  • function y=fun(x)

  • 二分法 即一分为二的方法。设[a,b]为R的紧区间, 逐次二分法就是造出如下的区间序列:a0=a,b0=b,且对任一自然数n,[an+1,bn+1]或者等于[an,cn],或者等于[cn,bn],其中cn表示[an,bn]的中点。

  • 一般地,对于函数f(x),如果存在实数c,当x=c时,若f(c)=0,那么把x=c叫做函数f(x)的零点。

  • 解方程即要求f(x)的所有零点。

  • 先找到a、b属于区间(x,y),使f(a),f(b)异号,说明在区间(a,b)内一定有零点,然后求f[(a+b)/2],

  • 现在假设f(a)《0,f(b)》0,a《b

  • 如果f[(a+b)/2]=0,该点就是零点,

  • 如果f[(a+b)/2]《0,则在区间((a+b)/2,b)内有零点,(a+b)/2赋给a,从①开始继续使用中点函数值判断。

  • 如果f[(a+b)/2]》0,则在区间(a,(a+b)/2)内有零点,(a+b)/2赋给b,从①开始继续使用中点函数值判断。

  • 通过每次把f(x)的零点所在小区间收缩一半的方法,使区间的两个端点逐步迫近函数的零点,以求得零点的近似值,这种方法叫做二分法。

matlab编程题:用二分法求方程x^3-3*x-1=0的根

先建立二分法的fun.m文件,代码如下:function fun(a,b,e)%f是自定义的函数%a为隔根区间左端点,b为隔根区间右端点,e为绝对误差限if nargin==2 e=1.0e-6;elseif nargin《2 input(’变量输入错误!’); return;endif a》=b input(’隔根区间输入错误!’); return;enda1=a;b1=b;c1=(a1+b1)/2;n=0; %迭代计数器,初值为0while (b-a)/(2^(n)) 》= 1/2*e c1 if f(c1)==0 c1 elseif f(a1)*f(c1)》0 a1=c1; c1=(a1+b1)/2; n=n+1; elseif f(b1)*f(c1)》0 b1=c1; c1=(a1+b1)/2; n=n+1; endendn再建立所要求函数的f.m文件:function y=f(x)y=x^3-3*x-1;运行:fun(-100,100,10^(-4))-100 100 为根所在该区间,10^(-4)表示精度要求。结果:c1 = 0 c1 = 50 c1 = 25 c1 = 25/2 c1 = 25/4 c1 = 25/8 c1 = 25/16 c1 = 75/32 c1 = 125/64 c1 = 225/128 c1 = 475/256 c1 = 975/512 c1 = 1925/1024 c1 = 988/529 c1 = 2494/1331 c1 = 640/341 c1 = 1189/633 c1 = 171/91 c1 = 1357/722 c1 = 109/58 c1 = 1013/539 c1 = 701/373 n = 22 最后结果为 701/373 欢迎指错。

用matlab如何编写二分法问题,谢谢

a=1;b=2;

f =@(x)x^3-x-1;c=(a+b)/2;

while abs(b-a)》1e-5if f(c)*f(b)《0a=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二分法

    在dichotomy.m保存的目录下,执行窗口命令

    dichotomy_fun=inline(’x^3-3*x^2-x+3’,’x’);

    dichotomy(dichotomy_fun,2,4,1e-4)

    n =

         1

    ans =

         3

    matlab如何实现用二分法求代数方程在区间内的解

    1、在MATLAB中,求解符号微分方程通解的指令格式为:y=dsolve(’equation’,’x’)%equation指符号微分方程,x为符号变量。

    2、如:》》 syms a bfun=’Dy=a*x+b’;y=dsolve(fun,’x’)。

    3、符号微分方程的特解y=dsolve(’equation’,’codition’,’x’)%equation为符号微分方程condition为微分方程的定解条件,x为符号变量。

    4、符号方程组的通解[y1,y2,...]=dsolve(’eq1’,’eq2’,...,’x’)通过eq1,eq2等构成符号微分方程组;x为符号变量。

    5、符号微分方程组的特解[y1,y2,...]=dsolve(’eq1’,’eq2’,...,’con1’,’con2’,...,’x’)其中,eq1,eq2等构成符号微分方程组;conq,con2为定解条件,X为符号变量。就完成了。

    怎么用matlab程序写二分法求方程根

    二分法在很多地方应该都会见到,这里是通过二分法迭代逼近的方法求出一个方程的根。

    function xc = bisection(f,a,b,tol)% use the bisection method to find the root of the function% Page 30,computer problem 7(Bisection method)% input:% f:the function that transform from the equation% a,b:the left and right value of the interval which the root is in% tol:the accuracy% output:% xc:the solution of the equationif sign(f(a)) * sign(f(b)) 》=0    error(’f(a)f(b)《0 not satisfied!’)endif nargin 《 3    disp(’The function should at least include 3 parameters’);endif nargin == 3    tol = 10^-6;endwhile (b-a)/2 》 tol    c = (a + b)/2;         if f(c) == 0 % when f(c) == 0,c is a root of the function        break     end    if f(a) * f(c) 《 0                                 % a and c form a new interval        b = c;  else  % c and b form a new interval        a = c;  endendxc = (a+b)/2; % the mid_rang is the root that we find

    编程问题,已经给出完整代码,没有相关配图可以配,希望谅解。

    参考资料

    CSDN.CSDN[引用时间2018-1-9]


    声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,谢谢。

    上一篇: 如何发挥自己的专业优势,如何在岗位上发挥自己的优势(5.16赛事分析:周六大放送)

    下一篇: 婚后以孩子为中心的星座女,喜欢女儿的星座女(3星座给孩子打造)



    猜你感兴趣

    推荐阅读

    网站内容来自网络,如有侵权请联系我们,立即删除! | 软文发布 | 粤ICP备2021106084号