如何使用内置函数numjac将Newton-Raphson方法应用于Backward Euler方法?

时间:2015-05-04 21:10:58

标签: matlab numerical-methods newtons-method

我正在开发一个程序,通过theta方法解决微分方程组的初值问题。

我的代码如下:

function [T,Y] = ivpSolver(f, S, y0, theta, h)
%INPUT
%f: a function-handle which describes the right-hand side f(t; u) of
%the differential equation
%S: a vector with requested points in time,
%u0: a column vector with initial conditions %u0
%theta: the parameter θ
%h: the step size h.
%OUTPUT
%T: a column vector of times T
%U: a matrix U with in every row the solution at the time corresponding to
%that row in T.

if length(S) == 2
    a = S(1);
    b = S(2);
end

T = zeros((b-a)/h,1);
Y = zeros((b-a)/h,1);
T(1) = t0;
Y(1) = y0;

tol = eps;
maxiter = 10;

for i = a:h:b
    T(i+1) = T(i) + h;

    %Calculation of new Y using Newton's method
    n_f = @(x) x - Y(i) - h*f(T(i+1),x);
    n_df = @(x)numjac(n_f, 0, Y(i) ,n_f(0, Y(i)), eps);
    ynew = newton(n_f,n_df,Y(i),tol,maxiter);

    Y(i+1) = Y(i) + h * ((1-theta)* f(T(i),Y(i))+ theta* f(T(i+1),ynew));
end

end

但是,我想用牛顿方法计算y的新值的部分不能正常工作。我认为这是因为衍生物,牛顿的方法需要,没有正确插入。

我想使用numjac来计算这个派生。 numjac的输入需要什么?

1 个答案:

答案 0 :(得分:0)

根据https://de.mathworks.com/matlabcentral/newsreader/view_thread/23776numjac专门用于格式doty = f(t,y)的ODE功能。 n_df应该是n_f的衍生物,因此在迭代点f中使用x的导数。因此

n_f = @(x) x - Y(i) - h*f(T(i+1),x);
n_df = @(x) 1 - h*numjac(f, T(i+1), x ,f(T(i+1), x), eps);

您的代码中还有其他一些奇怪的东西,如果上面的代码没有导致工作代码:

  • 检查将i in a:h:bT(i)Y(i)结合使用的逻辑。第一个是实数的算术序列,而第二个是整数。

  • 处理这样一个事实:通常无法以a的长度b步长hN = floor((b-a)/h)。如果b-a-N*h,则可能需要最后一步h=(b-a)/N。或者重新定义S

  • 此时您的代码仅在y有2个元素并且update()为标量时才有效。