如果你不介意的话,我想调整你的代码,使其更具活力,更加用户友好的阅读。
让我们先从一些准备工作。如果你想让你的脚本真正动态,那么我会建议你使用符号数学工具箱。这样,您就可以使用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功能......所以,让我们定义x
和y
作为变量。该方法调用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
一件事...我们可以把公式为向量或矩阵,并使用subs
到x
和y
所有值同时代入每个方程。
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
。
现在,当你运行这个函数时,它应该给你你正在寻找的东西。我没有测试过这个代码,但我很肯定这会起作用。
很高兴回答您可能遇到的任何问题。希望这可以帮助。
干杯!
看起来不错,但我不明白'J = [diff(f1,'x')diff(f1,'y'); diff(f2,'x')diff(f2,'y')];'。是不是有一个简单的命令来计算雅可比?我有一个概括n维... – bela83
@ bela83有一个雅可比函数,但我做了你在'J'行中看到的明确显示计算。 http://www.mathworks.com/help/symbolic/jacobian.html - 顺便说一句,如果你发现这个答案有用,你总是可以upvote我的答案:D。 – rayryeng
Got it!谢谢。 – bela83