如果您不介意,我想重新构建您的代码,使其更具动态性和更易于阅读。
让我们从一些预备开始。如果您想让您的脚本真正动态化,那么我建议您使用 Symbolic Math Toolbox。这样,您可以使用 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 x 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。
现在,当您运行此函数时,它应该会为您提供所需的内容。我没有测试过这段代码,但我很确定这会起作用。
很高兴回答您可能有的更多问题。希望这会有所帮助。
干杯!