MATLAB为什么总出Attempt to execute SCRIPT my as a function:
a=[50,101,90,89,156,0,0,10,7,5];n=9;x0=1;nn=1000;eps1=1e-8;b=1;y=newton(a,n,x0,nn,eps...
a=[50,101,90,89,156,0,0,10,7,5];
n=9;
x0=1;
nn=1000;
eps1=1e-8;
b=1;
y=newton(a,n,x0,nn,eps1,b);
function y=newton(a,n,x0,nn,eps1,b)
y=sym('x(i)',n);
x(1)=x0;
i=1;
x(i)=sym('x(i)');
while(abs(b)>eps1*x(i))
i=i+1;
x(i)=x(i-1)-n_f(a,n,x(i-1))/n_df(a,n,x(i-1));
b=x(i)-x(i-1);
if(i>nn)
return;
end
end
end
function y=n_f(a,n,x)%待求根的实数代数方程的函数
y=0.0;
for i=1:(n+1)
y=y+a(i)*x^(n+1-i);
end
end
function y=n_df(a,n,x)%方程一阶导数的函数
y=0.0;
for i=1:n
y=y+a(i)*(n+1-i)*x^(n-i);
end
end
程序就是这样各位大神帮看看谢谢了 展开
n=9;
x0=1;
nn=1000;
eps1=1e-8;
b=1;
y=newton(a,n,x0,nn,eps1,b);
function y=newton(a,n,x0,nn,eps1,b)
y=sym('x(i)',n);
x(1)=x0;
i=1;
x(i)=sym('x(i)');
while(abs(b)>eps1*x(i))
i=i+1;
x(i)=x(i-1)-n_f(a,n,x(i-1))/n_df(a,n,x(i-1));
b=x(i)-x(i-1);
if(i>nn)
return;
end
end
end
function y=n_f(a,n,x)%待求根的实数代数方程的函数
y=0.0;
for i=1:(n+1)
y=y+a(i)*x^(n+1-i);
end
end
function y=n_df(a,n,x)%方程一阶导数的函数
y=0.0;
for i=1:n
y=y+a(i)*(n+1-i)*x^(n-i);
end
end
程序就是这样各位大神帮看看谢谢了 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询