2013-12-09 86 views
4

我想写一个实现牛顿法在两个维度和,而我已经这样做了如下功能的功能,我到现在调整我的脚本,这样的输入参数我的功能必须在一个列向量被F(X),的f(x)雅可比矩阵,初始猜测x0和公差其中函数f(x)和其雅可比矩阵是在单独的.m文件。使用MATLAB编写实现牛顿法在两个维度

为脚本我写了一个实现牛顿方法的一个例子,我有:

n=0;   %initialize iteration counter  
eps=1;   %initialize error  
x=[1;1];  %set starting value 

%Computation loop  
while eps>1e-10&n<100 
    g=[x(1)^2+x(2)^3-1;x(1)^4-x(2)^4+x(1)*x(2)];   %g(x)  
    eps=abs(g(1))+abs(g(2));        %error  
    Jg=[2*x(1),3*x(2)^2;4*x(1)^3+x(2),-4*x(2)^3+x(1)]; %Jacobian  
    y=x-Jg\g;           %iterate  
    x=y;             %update x  
    n=n+1;            %counter+1  
end 

n,x,eps  %display end values 
这个脚本

所以,我已经实现的功能和雅可比矩阵为实际的剧本,我很努力研究如何使用所需的输入参数实际创建脚本。

谢谢!

回答

5

如果你不介意的话,我想调整你的代码,使其更具活力,更加用户友好的阅读。

让我们先从一些准备工作。如果你想让你的脚本真正动态,那么我会建议你使用符号数学工具箱。这样,您就可以使用MATLAB为您处理函数的衍生物。您首先需要使用syms命令,然后使用所需的任何变量。这告诉MATLAB你现在将把这个变量当作“符号”(即不是常量)。让我们先从一些基础知识:

syms x; 
y = 2*x^2 + 6*x + 3; 
dy = diff(y); % Derivative with respect to x. Should give 4*x + 6; 
out = subs(y, 3); % The subs command will substitute all x's in y with the value 3 
        % This should give 2*(3^2) + 6*3 + 3 = 39 

因为这是2D,我们将需要2D功能......所以,让我们定义xy作为变量。该方法调用subs命令会稍有不同:

syms x, y; % Two variables now 
z = 2*x*y^2 + 6*y + x; 
dzx = diff(z, 'x'); % Differentiate with respect to x - Should give 2*y^2 + 1 
dzy = diff(z, 'y'); % Differentiate with respect to y - Should give 4*x*y + 6 
out = subs(z, {x, y}, [2, 3]); % For z, with variables x,y, substitute x = 2, y = 3 
           % Should give 56 

一件事...我们可以把公式为向量或矩阵,并使用subsxy所有值同时代入每个方程。

syms x, y; 
z1 = 3*x + 6*y + 3; 
z2 = 3*y + 4*y + 4; 
f = [z1; z2]; 
out = subs(f, {x,y}, [2, 3]); % Produces a 2 x 1 vector with [27; 25] 

我们可以为矩阵做同样的事情,但为了简洁起见,我不会告诉你如何做到这一点。我会推迟到代码,然后你可以看到它。

现在我们有建立,让我们来解决你的代码一块在同一时间,真正使这一动态。您的功能需要初始猜测x0,作为列向量的函数f(x),雅可比矩阵作为一个2×2矩阵和公差tol

之前运行脚本,你将需要生成参数:

syms x y; % Make x,y symbolic 
f1 = x^2 + y^3 - 1; % Make your two equations (from your example) 
f2 = x^4 - y^4 + x*y; 
f = [f1; f2]; % f(x) vector 

% Jacobian matrix 
J = [diff(f1, 'x') diff(f1, 'y'); diff(f2, 'x') diff(f2, 'y')]; 

% Initial vector 
x0 = [1; 1]; 

% Tolerance: 
tol = 1e-10; 

现在,使你的脚本到一个函数:

% To run in MATLAB, do: 
% [n, xout, tol] = Jacobian2D(f, J, x0, tol); 
% disp('n = '); disp(n); disp('x = '); disp(xout); disp('tol = '); disp(tol); 

function [n, xout, tol] = Jacobian2D(f, J, x0, tol) 

% Just to be sure... 
syms x, y; 

% Initialize error 
ep = 1; % Note: eps is a reserved keyword in MATLAB 

% Initialize counter 
n = 0; 

% For the beginning of the loop 
% Must transpose into a row vector as this is required by subs 
xout = x0'; 

% Computation loop  
while ep > tol && n < 100 
    g = subs(f, {x,y}, xout); %g(x) 
    ep = abs(g(1)) + abs(g(2)); %error  
    Jg = subs(J, {x,y}, xout); %Jacobian 
    yout = xout - Jg\g;   %iterate  
    xout = yout;    %update x  
    n = n + 1;     %counter+1  
end 

% Transpose and convert back to number representation 
xout = double(xout'); 

我应该告诉你,当你”重新使用符号数学工具箱进行计算,计算它们时的数字数据类型为sym对象。您可能想要将这些转换回实数,因此您可以使用double将它们转换回来。但是,如果您将它们留在sym格式中,它会将您的数字显示为整齐的分数,如果这正是您想要的。如果您想要小数点表示形式,请将其转换为double

现在,当你运行这个函数时,它应该给你你正在寻找的东西。我没有测试过这个代码,但我很肯定这会起作用。

很高兴回答您可能遇到的任何问题。希望这可以帮助。

干杯!

+0

看起来不错,但我不明白'J = [diff(f1,'x')diff(f1,'y'); diff(f2,'x')diff(f2,'y')];'。是不是有一个简单的命令来计算雅可比?我有一个概括n维... – bela83

+0

@ bela83有一个雅可比函数,但我做了你在'J'行中看到的明确显示计算。 http://www.mathworks.com/help/symbolic/jacobian.html - 顺便说一句,如果你发现这个答案有用,你总是可以upvote我的答案:D。 – rayryeng

+0

Got it!谢谢。 – bela83