Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
![]() |
#1 |
初级会员
注册日期: 2008-04-20
年龄: 45
帖子: 1
声望力: 0 ![]() |
![]()
各位好!我正练习用matlab做数值计算,我从书上抄了一个用二分法求函数根的程序,但不知老师提醒我输入函数无效请问该怎么弄?源程序如下:
function [c,err,yc]=bisect(f,a,b,delta) %Input -f is the function input as a string 'f' % -a and b are the left and right end points % -delta is the tolerance %-Output -c is the zero % -yc=f(c) % -err is the error estimate for c ya=feval(f,a); yb=feval(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=feval(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=feval(f,c); 我在命令窗口输入:bisect('x^3-2*x+2',-10,10,0.001) 结果显示 ??? Invalid function name 'x^3-2*x+2'. Error in ==> bisect at 8 ya=feval(f,a); |
![]() |
![]() |
![]() |
#2 |
初级会员
注册日期: 2013-09-11
帖子: 2
声望力: 0 ![]() |
![]()
改成这样
f = @(x)x^3-2*x^2+2; >> bisect(f,-10,10,0.001) 即可 |
![]() |
![]() |
![]() |
主题工具 | |
显示模式 | |
|
|
![]() |
||||
主题 | 主题作者 | 版面 | 回复 | 最后发表 |
二维热传导问题数值解 | yidao1984 | MATLAB论坛 | 1 | 2008-04-28 01:53 |
大家别讲理论头头是道的,一遇到实际问题就哑火。谁能拿出可行性方案解决下面的问题让我看看 | mhahahahamhamha | MATLAB论坛 | 0 | 2008-04-18 15:04 |
[求助]freely download | andrealily | MATLAB论坛 | 2 | 2008-04-11 18:31 |
【求助】二进制通信系统的蒙特卡罗仿真实现(用Matlab实现) | 红将 | MATLAB论坛 | 0 | 2008-02-20 22:00 |