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

发布时间:2024-10-13 11:39:36 来源:君肯网

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

2、如:&gt&gtsyms 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程序写用二分法求方程根

matlab源程序如下:

function erfenfa(a,b)%a,b为区间,s=(a+b)/2,while b-a&gt1e-5  if fun(a)*fun(s)&gt0。  a=s elseif fun(a)*fun(s)&lt0

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)&lt0,f(b)&gt0,a&ltb

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

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

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

通过每次把f(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)) &gt=0

    error(’f(a)f(b)&lt0 not satisfied!’)endif nargin &lt 3

    disp(’The function should at least include 3 parameters’)endif nargin == 3

    tol = 10^-6endwhile (b-a)/2 &gt 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) &lt 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

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

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

参考资料

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

function [P]=zhidao7()

%%

% 二分法求解方程sinx-cosx在[0 2pi]上面的所有解,并存到数组P中

% 首先对方程做一些处理,如求导之类的。然后知道了有2个解。大致在什么范围【a,b】

% 或者,先分很多个区间,找出有值的区间。但这种理论上不一定准确

% 程序使用命令:[P]=zhidao7()

%%

%初值

precision = 1e-6 %精度

a =[0, pi]%因为f(0)*f(pi)&lt0

b = [pi,2*pi]

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)&lt0,f(max)&gt0para =-1,f(min)&gt0,f(max)&lt0

t =0

while abs(b-a) &gtprecision

x = (a +b)/2

if f(x)*para &gt0

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如何实现用二分法求代数方程在区间内的解?全部的内容,如果了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

更多相关资讯

1、在MATLAB中,求解符号微分方程通解的指令格式为:y=dsolve(’equation’,’x&#…
查看详情
1、在MATLAB中,求解符号微分方程通解的指令格式为:y=dsolve(’equation’,’x&#…
查看详情
1、在MATLAB中,求解符号微分方程通解的指令格式为:y=dsolve(’equation’,’x&#…
查看详情
相关文章
推荐游戏
风之谷
风之谷
游戏资讯 10.5M
下载
斗罗大陆3
斗罗大陆3
游戏资讯 566.9M
下载
冠军网球
冠军网球
游戏资讯 148.1M
下载
最佳炮手
最佳炮手
游戏资讯 68.1M
下载
如梦下弦月
如梦下弦月
游戏资讯 840.1M
下载
富甲封神传
富甲封神传
游戏资讯 263.0M
下载