二分法求非线性方程根matlab(matlab画图:用二分法求方程x^3cosx+6sin^5x-3=0)

本文目录
matlab画图:用二分法求方程x^3cosx+6sin^5x-3=0
function =bisect(fun,a,b,eps,varargin)
% 二分法求解非线性方程的根
% 输入参数:
% ---fun:待求根方程的函数描述
% ---a,b:初始区间的端点
% ---eps:精度要求,默认值为1e-6
% ---p1,p2,...:求根函数附加参数
% 输出参数:
% ---x:非线性方程的近似根
% ---fx:根x处的函数值
% ---iter:迭代次数
% ---X:每一步迭代的结果
if nargin《3,error(’输入参数至少需要3个!’),end
if nargin《4|isempty(eps),eps=1e-6;end
fa=feval(fun,a,varargin{:});fb=feval(fun,b,varargin{:});
% fa=fun(a,varargin{:});fb=fun(b,varargin{:});
k=1;
if fa*fb》0 % 不满足二分法使用条件
warning();
elseif fa==0 % 区间左端点为根
x=a;fx=fa;
elseif fb==0 % 区间右端点为根
x=b;fx=fb;
else
while abs(b-a)》eps; % 控制二分法结束条件
x=(a+b)/2; % 二分区间端点
fx = feval(fun,x,varargin{:}); % 计算中点的函数值
if fa*fx》0; % 条件
a = x; % 端点更新
fa = fx; % 端点函数值更新
elseif fb*fx》0; % 条件
b = x; % 端点更新
fb = fx; % 端点函数值更新
else
break
end
X(k)=x;k=k+1;
end
end
iter=k;
使用这个函数时候先利用隐函数绘图,找到有根区间,然后确定取值区间,这个函数好像只能得到单根,不能得到多个根值,所以得一个个的得到取值区间。
隐函数绘图,得到取值区间:
》》 syms x
》》 f=x^3*cos(x)+6*(sin(x))^5-3
》》 ezplot(f);grid on
调用函数:
》》 fun=@(x)x.^3*cos(x)+6*(sin(x)).^5-3;
》》 =bisect(fun,0,1.5)
xx =
0.9919
fx =
3.2727e-007
iter =
22
X =
Columns 1 through 11
0.7500 1.1250 0.9375 1.0313 0.9844 1.0078 0.9961 0.9902 0.9932 0.9917 0.9924
Columns 12 through 21
0.9921 0.9919 0.9920 0.9919 0.9920 0.9919 0.9919 0.9919 0.9919 0.9919
有没有二分法解非线性方程的MATLAB程序
建议楼主遇到关于matlab 的问题就到 mathworks网站的file exchange里找 。
下面是二分法的函数文件,你直接设置输入参数就可以了
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的当前路径里。
举个例子:
》》 format long
》》 =bisect(@(x)x-cos(x),0,1,1e-8)
answer =
0.739085134118795
error =
7.450580596923828e-009
value =
1.512334035780327e-009
answer即是方程 x-cos(x)=0 的根,error 是实际误差,value是计算结果回代到方程左边的值
用二分法求解非线性方程(c,c++,matlab都可以)
去我的空间里,有
#include《iostream》
#include《cmath》
using namespace std;
double fun(double x){
double y;
y=exp(-x)+exp(x);
return y;
}
int main(){
double a,b,u,v,e;
a=-1.0;
b=1.0;
int k=0;
cout《《"请输入精度 L:"《《endl;
cin》》e;
while(abs(u-b)》e||abs(a-v)》e){
++k;
cout《《"第"《《k《《"次"《《endl;
u=a+0.382*(b-a);cout《《"u的值:"《《u《《" "《《"fun(u)的值"《《fun(u)《《endl;
v=a+0.618*(b-a);cout《《"v的值:"《《v《《" "《《"fun(v)的值"《《fun(v)《《endl;
if(fun(u)》=fun(v))
a=u;
else
b=v;
}
}

更多文章:
delphi数据库案例(请提供一个简单的DELPHI查询数据库的实例,包括所有操作)
2026年4月19日 13:00
ui培训学校排名(请问:惠州UI培训学校排名比较靠前的有哪些)
2026年8月23日 10:15
学生座位随机数生成器(用excel随机排座位号码不要重复的)
2025年6月14日 13:30
用solidworks画大白(大神们这个用solidworks怎么画)
2026年4月17日 21:30
apache恢复默认配置(配置php以后,重启Apache,配置失效了,但重启电脑以后又好了,怎么)
2026年9月5日 07:45
onmousemove怎么停止(js的onmousemove)
2025年8月2日 16:45
三角函数值对照表全部初中(特殊角度的三角函数值是怎么样的呢)
2026年1月3日 15:15













