Newton Raphson method Matlab

The aim of this session is to use a basic example to illustrate how to use the Newton Raphson method in Matlab.

We will make use of the ode45 solver, and use boundary conditions in the following example.

Here come the exercise

Newton-Raphson-method-matlab

With boundary conditions

Newton-Raphson-method-matlab

Here is the route we will take

Newton Raphson method in Matlab

The code below solve this initial value problem (IVP) using the function ode45

it used the Newton Raphson method in the iteration process to approach the exact solution and finally end the iteration when y(1) is accurately converged up to the third decimal.

In the beginning of the problem we divide the ODE (ordinary differential equation) to a set of first order equations and we use 1 as initial guess for y'(0)

Newton-Raphson-method-matlab

Here is the code

ua=0;
s(1)=1; %First guess for the derivative
s(2)=1.1; %second guess for the derivative

tb=1;  %Second time
target_ub=0;  % target

f=@(t,y) [y(2); 1./(1+y(1))^2]; % set of 1st order ODE
rb=@(v) (v-target_ub)  % boundary conditions at b

[t,y]=ode45(f,[0 tb],[ua; s(1)]);
ub(1)=y(end,1);

for (j=2:50) % stop after 50 iteration even if the accuracy is not reached
[t,y]=ode45(f,[0 tb],[ua; s(j)]);
ub(j)=y(end,1);
if abs(rb(ub(j)))<0.001 % quit the for loop if accuracy reached
break
end
s(j+1)=s(j)-rb(ub(j))*(s(j)-s(j-1))/(rb(ub(j))-rb(ub(j-1)));
end

plot(t,y(:,1)) % plot the last solution
xlabel('t')
ylabel('y')
grid on

The Plot

Newton-Raphson-method-matlab

tchouken2

Related Posts