% Suppose a new problem of y'' = -ty^2, % with boundary conditions y(0) = -1 and y(1) = 2 % In the ODE from lecture 17 that would imply: % p(t) = 0, q(t) = -t*y and r(t) = 0. % Resulting in % A(t) = 1 % B(t) = -[2 + q(t)dt^2] % C(t) = 1 % rhs = 0; % Suppose we want this for N times tn, then we % construct a matrix of size N-2: N = 10; % number of total points n = N-2; % number of interior points dt = 1/(n+1); % time step size t = (0:dt:1)'; % Construct b including boundary conditions b = zeros(n,1); b(1) = 0 - 1*(-1); b(end) = 0 - 1*2; % Initialize x for the initial guess to the solution xnp1 = ones(n,1); At = zeros(n,n); dx = ones(n,1); while (norm(abs(dx)) > 10^(-3)) xn = xnp1 At = A(t,xn,n,dt); f = F(xn,At,b); J = Jac(xn,At,b); dx = J\(-f); xnp1 = xn + dx end sol = zeros(N,1); sol(1) = -1; sol(end) = 2; sol(2:N-1) = xn(1:n); plot(t,sol,'g*-'); hold on;