牛顿迭代法的matlab程序出错,不知错在哪里,烦请各路大神指教

function [x]=Newtondd(f,x0,x1,e)
j=1;
flag=0;
for n=10^j
for i=0:n
x_a=x0+((x1-x0)/n)*i;
x_b=x_a-subs(f,x_a)/subs(diff(f),x_a);
if abs(x_a-x_b)>e
continue;
else
x=x_b;
flag=1;
break;
end
end
if flag==1
break;
end
j=j+1;
end
>> syms x;
>> f=(3*x^2-exp(x))

f =

3*x^2 - exp(x)

>> [x]=Newtondd(f,0,1,10^-3)
Error in Newtondd (line 2)
j=1;

Output argument "x" (and maybe others) not assigned during call to
"C:\Users\chen\Documents\MATLAB\Newtondd.m>Newtondd".

第1个回答  2013-08-21
我也认为是
if abs(x_a-x_b)>e
continue;

这一句可能一直被执行,所以没有给出x值而报错。而else 没有一次命中。待会儿再仔细看看
第2个回答  2013-08-07
是积分么?追问

已知函数在[0,1]中有解,通过牛顿迭代分别进行10等分100等分。。。。的求解,找到那个解

追答

x_b=x_a-subs(f,x_a)/subs(diff(f),x_a);这一句不大明白你用横坐标减去,纵坐标与他倒数比是什么意思,如果xa=x0,你这样减岂不是直接超出范围之外了?

相似回答