作业帮 > 数学 > 作业

function [r,n]=mulSimNewton(F,x0,eps)

来源:学生作业帮 编辑:神马作文网作业帮 分类:数学作业 时间:2024/11/12 18:43:15
function [r,n]=mulSimNewton(F,x0,eps)
% 用简化牛顿法求非线性方程组的一组解
% 非线性方程组:F
% 初始解:x0
% 解的精度:eps
% 求得的一组解:r
% 迭代步数:n
% 初始迭代一组解:x0=[x1:xn]
if nargin==2
eps=1.0e-6;
end
x0 = transpose(x0);
Fx = subs(F,findsym(F),x0);
dF = Jacobian(F);
c = subs(dF,findsym(dF),x0);
r=x0-inv(c)*Fx;
n=1;
tol=1;
while tol>eps
x0=r;
Fx = subs(F,findsym(F),x0);
r=x0-inv(c)*Fx; %核心迭代公式
tol=norm(r-x0);
n=n+1;
if(n>100000) %迭代步数控制
disp('迭代步数太多,可能不收敛!');
return;
end
end
求方法组 z=[0.5*sin(x)+0.1*cos(x*y)-x;0.5*cos(x)-0.1*cos(y)-y];的解;
>>syms x y;
>> z=[0.5*sin(x)+0.1*cos(x*y)-x;0.5*cos(x)-0.1*cos(y)-y];
>> [r,n]=mulSimNewton(z,[0 0])
Error using ==> error
Not enough input arguments.
Error in ==> jacobian at 6
error
Error in ==> mulSimNewton at 15
dF = Jacobian(F);
注释:这是龚纯那版的matlab与科学计算时怎么有错误啊?求高手告知下,
function [r,n]=mulSimNewton(F,x0,eps)
function [r,n]=mulNewton(F,x0,eps)
if nargin==2
eps=1.0e-4;
end
x0 = transpose(x0);
Fx = subs(F,findsym(F),x0);
var = sym(symvar(findsym(F)));%var is string 要变换下
dF = jacobian(F,var);
dFx = subs(dF,findsym(dF),x0);
r=x0-inv(dFx)*Fx;
n=1;
tol=1;
while tol>eps
x0=r;
Fx = subs(F,findsym(F),x0);
dFx = subs(dF,findsym(dF),x0);
r=x0-inv(dFx)*Fx; %核心迭代公式
tol=norm(r-x0);
n=n+1;
if(n>1000) %迭代步数控制
disp('迭代步数太多,可能不收敛!');
return;
end
end

syms x y;
z=[0.5*sin(x)+0.1*cos(x*y)-x;0.5*cos(x)-0.1*cos(y)-y];
[r,n]=mulNewton(z,[0 0])
r =
0.1981
0.3980
n =
3