楼主: hrbatanu
2535 11

[问答] 受不了了 [推广有奖]

  • 17关注
  • 4粉丝

已卖:2份资源

讲师

41%

还不是VIP/贵宾

-

威望
0
论坛币
199 个
通用积分
0.3600
学术水平
2 点
热心指数
6 点
信用等级
1 点
经验
4807 点
帖子
264
精华
0
在线时间
679 小时
注册时间
2011-6-23
最后登录
2022-6-4

楼主
hrbatanu 发表于 2013-7-1 14:13:59 |AI写论文
100论坛币
QQ截图20130701004739.png


画图我会,题目里德epsilon_a公式如下:图中(5.5) photo.jpg
matlab里有bisection这个function吗?为什么我每次用bisect都说undefined function?
好像要自己倒入?
我根据这一页导入的 photo.jpg

我的code是:

[size=12.800002098083496px]function [root,fx,ea,iter]=bisect(func,x1,xu,es,maxit,varargin)
[size=12.800002098083496px]if nargin<3,error('at least 3 input arguments required'),end
[size=12.800002098083496px]test = func(x1,varargin{:})*func(xu,varargin{:});
[size=12.800002098083496px]if test>0,error('no sign change'),end
[size=12.800002098083496px]if nargin<4|isempty(es),es=0.0001;end
[size=12.800002098083496px]if nargin<5|isempty(maxit),maxit=50;end
[size=12.800002098083496px]iter=0;xr=x1;ea=100;
[size=12.800002098083496px]while(1)
[size=12.800002098083496px]    xrold=xr;
[size=12.800002098083496px]    xr=(x1+xu)/2;
[size=12.800002098083496px]    iter=iter+1;
[size=12.800002098083496px]    if xr ~=0,ea=abs((xr-xrold)/xr)*100;end
[size=12.800002098083496px]    test=func(x1,varargin{:})*func(xr,varargin{:});
[size=12.800002098083496px]    if test<0
[size=12.800002098083496px]        xu=xr;
[size=12.800002098083496px]    elseif test>0
[size=12.800002098083496px]        x1=xr;
[size=12.800002098083496px]    else
[size=12.800002098083496px]        ea=0;
[size=12.800002098083496px]    end
[size=12.800002098083496px]    if ea<=es | iter >=maxit,break,end
[size=12.800002098083496px]end
[size=12.800002098083496px]root = xr; fx=func(xr,varargin{:});

[size=12.800002098083496px]fm=sin(m)-m.^3;
[size=12.800002098083496px][~, fx ea iter]=bisect(fm,0.5,1);[size=12.727272033691406px]

[size=12.727272033691406px]然后怎么操作?把这个M-file保存后再编辑另一个即可?
我试着模仿下面这个例题,未遂。。。 photo.jpg


求高手解答!
我从网上看的很多地方用abs(b-a)/2<2%作为停止的标准,a,b是初始区间断点。虽然code中间已经重置了a,b,但我还是看不出为什么abs(b-a)/2和abs((x_new-x_old)/x_new)的联系。


谢谢!

最佳答案

24578901 查看完整内容

针对你的问题,这样就可以啦 fm=@(m)sin(m)-m^3; [mass, fx ea iter]=bisect(fm,0.5,1) 结果是: mass = 0.9286 fx = -4.5446e-007 ea = 5.1349e-005 iter =20
关键词:受不了 bisection Undefined function Defined undefined function change matlab error

本帖被以下文库推荐

沙发
24578901 在职认证  发表于 2013-7-1 14:14:00
hrbatanu 发表于 2013-7-2 00:16
I am with you, but I need to fit the code into my question: sin(x)-x^3,  thx anyway~
针对你的问题,这样就可以啦
fm=@(m)sin(m)-m^3;
[mass, fx ea iter]=bisect(fm,0.5,1)
结果是:
mass = 0.9286
fx = -4.5446e-007
ea = 5.1349e-005
iter =20

藤椅
24578901 在职认证  发表于 2013-7-1 14:28:09
新建一个bisect.m就可以啦

板凳
24578901 在职认证  发表于 2013-7-1 14:41:44
fm=@(m)sin(m)-m^3;[mass, fx ea iter]=bisect(fm,0.5,1)
mass = 0.9286
fx = -4.5446e-007
ea = 5.1349e-005
iter =20

fm=@(m)sqrt(9.81*m/0.25)*tanh(sqrt(9.81*0.25/m)*4)-36;
[mass, fx ea iter]=bisect(fm,40,200)
mass =142.7377
fx =4.6089e-007
ea =5.3450e-005
iter =21

报纸
hrbatanu 发表于 2013-7-1 23:18:42
24578901 发表于 2013-7-1 14:41
fm=@(m)sin(m)-m^3;[mass, fx ea iter]=bisect(fm,0.5,1)
mass = 0.9286
fx = -4.5446e-007
Undefined function 'bisect' for input arguments of type 'function_handle'.

地板
hrbatanu 发表于 2013-7-1 23:19:53
24578901 发表于 2013-7-1 14:41
fm=@(m)sin(m)-m^3;[mass, fx ea iter]=bisect(fm,0.5,1)
mass = 0.9286
fx = -4.5446e-007
is bisection built in matlab? or we should implement by ourself?

7
24578901 在职认证  发表于 2013-7-1 23:27:54
hrbatanu 发表于 2013-7-1 23:19
is bisection built in matlab? or we should implement by ourself?
1、要想知道Matlab有没有bisection,可以在help(软件界面上的?号)查询

2、当你在命令窗口运行你的代码,发现提示Undefined function 'bisect',这时候你应该建立一个bisect.m之后在运行就可以了

8
hrbatanu 发表于 2013-7-1 23:43:21
yes, but how to build a bisect.m? I've tried that as I said in the question, I copy from my textbook, but I do not fully understand that and thus fail to build it.

9
24578901 在职认证  发表于 2013-7-1 23:47:40
hrbatanu 发表于 2013-7-1 23:43
yes, but how to build a bisect.m? I've tried that as I said in the question, I copy from my textbook ...
步骤:
1、点击软件左上角的新建

2、把以下内容粘贴下去
function [root,fx,ea,iter]=bisect(func,x1,xu,es,maxit,varargin)
if nargin<3,error('at least 3 input arguments required'),end
test = func(x1,varargin{:})*func(xu,varargin{:});
if test>0,error('no sign change'),end
if nargin<4|isempty(es),es=0.0001;end
if nargin<5|isempty(maxit),maxit=50;end
iter=0;xr=x1;ea=100;
while(1)
    xrold=xr;
    xr=(x1+xu)/2;
    iter=iter+1;
    if xr ~=0,ea=abs((xr-xrold)/xr)*100;end
    test=func(x1,varargin{:})*func(xr,varargin{:});
    if test<0
        xu=xr;
    elseif test>0
        x1=xr;
    else
        ea=0;
    end
    if ea<=es | iter >=maxit,break,end
end
root = xr; fx=func(xr,varargin{:});

3、按保存,名字会自动保存为bisect.m

4、在命令窗口输入
fm=@(m)sqrt(9.81*m/0.25)*tanh(sqrt(9.81*0.25/m)*4)-36;
[mass, fx ea iter]=bisect(fm,40,200)

5、done!!

10
hrbatanu 发表于 2013-7-2 00:16:30
24578901 发表于 2013-7-1 23:47
步骤:
1、点击软件左上角的新建
I am with you, but I need to fit the code into my question: sin(x)-x^3,  thx anyway~

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-2-1 18:07