计算数学函数-MATLAB之和的导数

时间:2014-11-02 10:12:19

标签: matlab function derivative

在Matlab中我想创建一个名为J(theta_0, theta_1) 的成本函数的偏导数(为了进行梯度下降所需的计算)。

enter image description here

函数J(theta_0, theta_1)定义为:

enter image description here

让我们说h_theta(x) = theta_1 + theta_2*x。另外:alpha已修复,我们会给出theta_1theta_2的起始值。我们在这个例子中说:alpha = 0.1 theta_1 = 0theta_2 = 1此外,我在两个不同的向量中包含x和y的所有值

VectorOfX = 
5
5
6

VectorOfX = 
6
6
10

我试图在Matlab中解决这个问题的步骤:我不知道如何在matlab中解决这个问题。所以我开始尝试在Matlab中定义一个函数并尝试这个:

theta_1 = 0
theta_2 = 1
syms x;
h_theta(x) = theta_1 + t2*x;

这很有用,但不是我真正想要的。我想得到x ^(i),它在一个向量中。接下来我尝试的是:

theta_1 = 0
theta_2 = 1
syms x;
h_theta(x) = theta_1 + t2*vectorOfX(1);

这会出现以下错误:

Error using sym/subsindex (line 672)
Invalid indexing or function definition. When defining a
function, ensure that the body of the function is a SYM
object. When indexing, the input must be numeric, logical or
':'.

Error in prog1>gradientDescent (line 46)
h_theta(x) = theta_1 + theta_2*vectorOfX(x);

我查了这个错误,并且不知道如何解决这个特定的例子。我觉得我让matlab对我不利,而不是对我有利。

1 个答案:

答案 0 :(得分:1)

当我必须执行符号计算时,我更喜欢使用Mathematica。在那种环境中,这是获取您正在寻找的偏导数的代码。

J[th1_, th2_, m_] := Sum[(th1 + th2*Subscript[x, i] - Subscript[y, i])^2, {i, 1, m}]/(2*m)
D[J[th1, th2, m], th1]
D[J[th1, th2, m], th2]

并给出

回到MATLAB,我们可以使用以下代码解决这个问题

%// Constants.
alpha = 0.1;
theta_1 = 0;
theta_2 = 1;
X = [5 ; 5 ; 6];
Y = [6 ; 6 ; 10];

%// Number of points.
m = length(X);

%// Partial derivatives.
Dtheta1 = @(theta_1, theta_2) sum(2*(theta_1+theta_2*X-Y))/2/m;
Dtheta2 = @(theta_1, theta_2) sum(2*X.*(theta_1+theta_2*X-Y))/2/m;

%// Loop initialization.
toll = 1e-5;
maxIter = 100;
it = 0;
err = 1;
theta_1_Last = theta_1;
theta_2_Last = theta_2;

%// Iterations.
while err>toll && it<maxIter
    theta_1 = theta_1 - alpha*Dtheta1(theta_1, theta_2);
    theta_2 = theta_2 - alpha*Dtheta2(theta_1, theta_2);

    it = it + 1;
    err = norm([theta_1-theta_1_Last ; theta_2-theta_2_Last]);
    theta_1_Last = theta_1;
    theta_2_Last = theta_2;
end

不幸的是,对于这种情况,迭代不会收敛。

MATLAB对符号计算不是很灵活,但是获得这些偏导数的方法如下:

m = 10;
syms th1 th2
x = sym('x', [m 1]);
y = sym('y', [m 1]);
J = @(th1, th2) sum((th1+th2.*x-y).^2)/2/m;
diff(J, th1)
diff(J, th2)