二分法matlab(二分法MATLAB实现)_方程_区间_符号

本文目录

  • 二分法MATLAB实现
  • matlab如何实现用二分法求代数方程在区间内的解
  • Matlab编写程序用二分法求解非线性方程的根
  • 二分法matlab程序问题
  • matlab用二分法求解
  • MATLAB二分法
  • matlab怎么用二分法求方程的根
  • matlab中的用法(二分法)
  • 用matlab程序写用二分法求方程根

二分法MATLAB实现

》》 f=inline(’x^2-x-2’);
》》 =bisect(f,0,3,0.01)
c =
2.0010
err =
0.0059
yc =
0.0029
-----------
%使用二分法 求解上面超越方程
%下面是二分法的函数文件,你直接设置输入参数就可以了
function =bisect(f,a,b,delta)
%Input - f is the function
% - a and b are the left and right endpoints
% - delta is the tolerance
%Output - c is the zero
% - yc= f(c)
% - err is the error estimate for c
%If f is defined as an M-file function use the @ notation
% call =bisect(@f,a,b,delta).
%If f is defined as an anonymous function use the
% call =bisect(f,a,b,delta).
% NUMERICAL METHODS: Matlab Programs
% (c) 2004 by John H. Mathews and Kurtis D. Fink
% Complementary Software to accompany the textbook:
% NUMERICAL METHODS: Using Matlab, Fourth Edition
% ISBN: 0-13-065248-2
% Prentice-Hall Pub. Inc.
% One Lake Street
% Upper Saddle River, NJ 07458
ya=f(a);
yb=f(b);
if ya*yb 》 0,return,end
max1=1+round((log(b-a)-log(delta))/log(2));
for k=1:max1
c=(a+b)/2;
yc=f(c);
if yc==0
a=c;
b=c;
elseif yb*yc》0
b=c;
yb=yc;
else
a=c;
ya=yc;
end
if b-a 《 delta, break,end
end
c=(a+b)/2;
err=abs(b-a);
yc=f(c);

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、符号方程组的通解=dsolve(’eq1’,’eq2’,...,’x’)通过eq1,eq2等构成符号微分方程组;x为符号变量。

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

Matlab编写程序用二分法求解非线性方程的根

用二分法求方程x*x-x-1=0的正根,要求精确到小数点后四位。(matlab)
l1
计算公式
f(ak)*f(bk)《0;
bk-ak=1/2k-1*(b-a);
a1《=a2《=…
《=ak《=…,b1《=b2《=…《=bk《=…。
l2
算法分析
设f(x)∈C,继续以上过程,直到精度达到要求为止。
l3
源程序
function
f1=fun(x);
f1=x-cos(x);
function
=erfen(a,b,s)
%a,b为根区间,s为精度
a=0;b=1;s=1e-4;k=0;
while
abs(a-b)》s
x=(a+b)/2;
if
fun(a)*fun(x)《0
b=x;
else
a=x;
end
k=k+1;
end
x=(a+b)/2
%x为方程的解
k
%
k为计算次数
实验结果讨论和分析
本题使用二分法得到的x=0.7391,满足基本要求,题目要求精确到小数点后四位,告诉了本题二分法得应达到得精确度;计算次数为14,二分法收敛性很好,收敛速度不快。

二分法matlab程序问题

function test
a=1;b=2;tol=1e-4;N=10000;
k=0;fa=f(a);
for k=1:N
p=(a+b)/2; fp=f(p);
if( fp==0 || (b-a)/2《tol)
break
end
if fa*fp《0 b=p; else a=p; end
end
disp(k)
disp(p)
function ydot=f(x)
% 内嵌函数,matlab7.0及以上版本支持
ydot=x^3+2*x^2-7;
end
end
打字不易,如满意,望采纳。

matlab用二分法求解

function =zhidao7()
%%
% 二分法求解方程sinx-cosx在上面的所有解,并存到数组P中
% 首先对方程做一些处理,如求导之类的。然后知道了有2个解。大致在什么范围【a,b】
% 或者,先分很多个区间,找出有值的区间。但这种理论上不一定准确
% 程序使用命令:=zhidao7()
%%
%初值
precision = 1e-6; %精度
a =;%因为f(0)*f(pi)《0
b = ;
P(1) = twofind(a(1),a(2),1,precision);
P(2) = twofind(b(1),b(2),-1,precision);
end
function y = twofind(a,b,para,precision)
%para =1时,f(min)《0,f(max)》0;para =-1,f(min)》0,f(max)《0;
t =0;
while abs(b-a) 》 precision
x = (a +b)/2;
if f(x)*para 》 0
b = x;
else
a = x;
end
t = t+1;
end
fprintf(’迭代次数:%d\n’,t);
y = x;
end
function y = f(x)
y=sin(x)-cos(x);
end

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怎么用二分法求方程的根

  • 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)

  • 二分法 即一分为二的方法。设的中点。

  • 一般地,对于函数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,

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

  • 如果f=0,该点就是零点,

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

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

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

matlab中的用法(二分法)

function =bisection(fun,a,b,tol,nmax,varargin)
x=;
fx=feval(fun,x,varargin{:});
if fx(1)*fx(3)》0
error{’the sign of the at the extrema of the interval must be different’};
elseif fx(1)==0
zero=a;res=0;niter=0;
return
elseif fx(3)==0
zero=b;res=0;niter=0;
return
end
niter=0;
I=(b-a)*0.5;
while I》=tol&niter《=nmax
niter=niter+1;
if sign(fx(1))*sign(fx(2))《0
x(3)=x(2);x(2)=x(1)+(x(3)-x(1))*0.5;
fx=feval(fun,x,varargin{:});I=(x(3)-x(1))*0.5;
elseif sign(fx(2))*sign(fx(3))《0
x(1)=x(2);x(2)=x(1)+(x(3)-x(1))*0.5;
fx=feval(fun,x,varargin{:});I=(x(3)-x(1))*0.5;
else
x(2)=x(find(fx==0));I=0;
end
end
if niter》nmax
fprintf(’bisection stopped without converging to the desired tolerance’,’because the maxinum number of iterations was reached\n’);
end
zero=x(2);x=x(2);res=feval(fun,x);%这里估计是feval,没有reval这个函数
据个例子看看,我用匿名函数做的,需要matlab7以上版本
M=6000;v=1000;
f=@(l,M,v)(M-v*(1+l).*((1+l).^5-1)./l);
fun=@(l)f(l,M,v);
fplot(fun,);
bisection(fun,0.05,0.1,0,36)

用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

特别声明

本文仅代表作者观点,不代表本站立场,本站仅提供信息存储服务。

分享:

扫一扫在手机阅读、分享本文