You are on page 1of 30

KIE3005: NUMERICAL ANALYSIS

SEMESTER 2, 2020/2021
ASSIGNMENT 2

BY

Sabbir Ahmed
17146555/1 (KIE180713)
Group-2
Part (i)

(a) Forward Euler:

clear all;
close all;
clc;

N = 5; %iteration
h = 1;

C = 1;%capacitance in Farad
vin = 10;%Vin (V)
R = 5;%resistance in ohm
L = 2;%inductance in henry

i1 = zeros(1,N);
i2 = zeros(1,N);
Q = zeros(1,N);
t = zeros(1,N);

%All 3 equations given


% equ 1: dQ/dt = i1-i2
% equ 2: di1/dt = Vin/L - Q/LC
% equ 3: di2/dt = (i1-i2)/RC

%initial values
i1(1) = 0.01;
i2(1) = 0.002;
Q(1) = 0.01;

for n = 1:N
t(n+1) = n*h;
i1(n+1) = i1(n) + (vin/L - Q(n)/(L*C))*h
i2(n+1) = i2(n) + ((i1(n)-i2(n))/(R*C))*h
Q(n+1) = Q(n) + (i1(n)-i2(n))*h

end

plot(t,i1,'-*',t,i2,'-*',t,Q,'-*')
legend('i1','i2','Q')
(b) Backward Euler False Position:

clear all;
close all;
clc;

vin = 10;
R = 5;%resistance in ohm
L = 2;%inductance in henry
C = 1;%capacitance in Farad
q0 = 0.01;
i10 = 0.01;
i20 = 0.002;

tv = [0 5];
N = 5;
dqdt = @(t,i1,i2) i1 - i2;
di1dt = @(t,q) vin/L - q/(L*C);
di2dt = @(t,i1,i2) (i1 - i2)/(R*C);
[tvals, q, i1, i2]=IEuler_using_False_p(dqdt,di1dt,di2dt,tv,q0,i10,i20,N)

plot(tvals,i1,tvals,i2,tvals,q)
title('Backward Euler FP')
legend('i1','i2','Q')

function [tvals q i1 i2]=IEuler_using_False_p(dqdt,di1dt,di2dt,tv,q0,i10,i20,N)

tvals =tv(1); % initial t


q =q0; % initial y
i1 = i10;
i2 = i20;

tf=tv(2);
tstep = (tf-tvals)/N; % time step
numsteps = N; % number of steps to go
oldq = q(:,end);
oldi1 = i1(:,end);
oldi2 = i2(:,end);

for i=1:numsteps
oldt = tvals(end); % pick the last entry of array
newt = oldt + tstep; % step forward in t
%newy = fsolve(@(y) y - oldy- tstep*dydt(newt,y) , oldy); % backward Euler
f1=@(q) q-tstep*dqdt(newt,i1(end),i2(end))-oldq;
f2=@(i1) i1-tstep*di1dt(newt,q(end))-oldi1;
f3=@(i2) i2-tstep*di2dt(newt,i1(end),i2(end))-oldi2;
newq = False_Position(f1);
newi1 = False_Position(f2);
newi2 = False_Position(f3);
tvals = [tvals newt];
q = [q newq];
i1 = [i1 newi1];
i2 = [i2 newi2];
oldq=newq;
oldi1=newi1;
oldi2=newi2;
end
end

function [xr]=False_Position(f)
iter = 0;
xl = 0;
xu = 100;
fl = f(xl);
fu = f(xu);
e_s = 0.0001;
e_a = 100;
xr = (xl+xu)/2;

while e_a > e_s

xrold = xr;
xr = (fu*xl-fl*xu)/(fu-fl);
fr = f(xr);
iter = iter + 1;
e_a = abs((xr-xrold)/xr)*100;
test = fl*fr;

if test < 0
xu = xr;
fu = fr;
elseif test > 0
xl = xr;
fl = fr;
end
end

end

(C) Backward Euler Secant

clear all;
close all;
clc;

%Given Values
vin = 10;
R = 5;
L = 2;
C = 1;
q0 = 0.01;
i10 = 0.01;
i20 = 0.002;

tv = [0 5];
N = 5;
dqdt = @(t,i1,i2) i1 - i2;
di1dt = @(t,q) vin/L - q/(L*C);
di2dt = @(t,i1,i2) (i1 - i2)/(R*C);

[tvals, q, i1, i2]=IEuler_using_Secant(dqdt,di1dt,di2dt,tv,q0,i10,i20,N)

plot(tvals,i1,tvals,i2,tvals,q)
title('Backward Euler Secant')
legend('i1','i2','Q')

function [tvals, q, i1, i2]=IEuler_using_Secant(dqdt,di1dt,di2dt,tv,q0,i10,i20,N)

tvals =tv(1); % initial t


q =q0; % initial y
i1 = i10;
i2 = i20;

tf=tv(2);
tstep = (tf-tvals)/N; % time step
numsteps = N; % number of steps to go
oldq = q(:,end);
oldi1 = i1(:,end);
oldi2 = i2(:,end);

for i=1:numsteps
oldt = tvals(end); % pick the last entry of array
newt = oldt + tstep; % step forward in t
%newy = fsolve(@(y) y - oldy- tstep*dydt(newt,y) , oldy); % backward Euler
f1=@(q) q-tstep*dqdt(newt,i1(end),i2(end))-oldq;
f2=@(i1) i1-tstep*di1dt(newt,q(end))-oldi1;
f3=@(i2) i2-tstep*di2dt(newt,i1(end),i2(end))-oldi2;
newq = Secant(f1);
newi1 = Secant(f2);
newi2 = Secant(f3);
tvals = [tvals newt];
q = [q newq];
i1 = [i1 newi1];
i2 = [i2 newi2];
oldq=newq;
oldi1=newi1;
oldi2=newi2;
end
end

function [xx] = Secant(f)


x(1) = 0;
x(2) = 5
e_a = 100;
e_s = 0.01;
i = 2;

while e_a > e_s


x(i+1) = x(i) - (x(i)-x(i-1))*f(x(i))/(f(x(i))-f(x(i-1)));
e_a = (x(i+1)-x(i))/x(i)*100;
i = i + 1;
xx = x(:,end)
end
end
(c) Huen

clear all;
close all;
clc;

%Given Values
N = 5;
dt = 1;
T = 5;

C = 1;
vin = 10;
R = 5;
L = 3;

i10 = 0.01;
i20 = 0.002;
q0 = 0.01;

dqdt = @(i1,i2) i1 - i2;


di1dt = @(q) vin/L - q/(L*C);
di2dt = @(i1,i2) (i1 - i2)/(R*C);
t = 0:1:5
[q,i1,i2] = Heun(T,dt,dqdt,di1dt,di2dt,q0,i10,i20)
plot(t,i1,t,i2,t,q)
title('Huen')
legend('i1','i2','Q')
function[q,i1,i2]=Heun(T,dt,dqdt,di1dt,di2dt,q0,i10,i20)

time=0:dt:T; % Time discretization vector


Nt=length(time);

q(:,1)=q0 ; % Initial value


i1(:,1)=i10 ;
i2(:,1)=i20 ;

% Heun method
for i=1:Nt-1

p1=dqdt(i1(:,i),i2(:,i));
p2=dqdt(i1(:,i)+dt*p1,i2(:,i)+dt*p1); % Predictor step = Euler
q(:,i+1)= q(:,i)+dt*(p1+p2)/2 ; % Corrector step
p3=di1dt(q(:,i));
p4=di1dt(q(:,i)+dt*p3); % Predictor step = Euler
i1(:,i+1)= i1(:,i)+dt*(p3+p4)/2 ; % Corrector step
p5=di2dt(i1(:,i),i2(:,i));
p6=di2dt(i1(:,i)+dt*p5,i2(:,i)+dt*p5);

i2(:,i+1)= i2(:,i)+dt*(p5+p6)/2 ; % Corrector step


end

end
Part (ii)
Shooting Method

(a) False Position

(i) Shooting-False Position-Euler

clear all;
close all;
clc;

N = 5;%iteration
h = 1;
tv = [0 5]
t = 0:5
C = 1;%capacitance in Farad
vin = 10;
R = 5;%resistance in ohm
L = 2;%inductance in henry

dqdt = @(i1,i2) i1 - i2;


di1dt = @(q) vin/L - q/(L*C);
di2dt = @(i1,i2) (i1 - i2)/(R*C);

i20 = 0.002;
q0 = 0.01;
i_g = [];

for i10 = 1.01:-0.01:0.105


[q i_1 x] = Euler(dqdt,di1dt,di2dt,q0,i10,i20,N,h);
i_g = [i_g i_1(end)];
end
i10 = 1.01:-0.01:0.105;

eq = polyfit(i10,i_g,1);
f = @(x) eq(1)*x + eq(2);
i10f = False_Position(f)
function [q i1 i2] = Euler(dqdt,di1dt,di2dt,q0,i10,i20,N,h)

i1 = zeros(1,N);
i2 = zeros(1,N);
q = zeros(1,N);
t = zeros(1,N);
i1(1) = i10;
i2(1) = i20;
q(1) = q0;

for i = 1:N

q(:,i+1)= q(:,i)+h*(dqdt(i1(:,i),i2(:,i)));
i1(:,i+1)= i1(:,i)+h*(di1dt(q(:,i)));
i2(:,i+1)= i2(:,i)+h*(di2dt(i1(:,i),i2(:,i)));

end
end

function [xr]=False_Position(f)
iter = 0;
xl = 0;
xu = 100;
fl = f(xl);
fu = f(xu);
e_s = 0.0001;
e_a = 100;
xr = (xl+xu)/2;

while e_a > e_s

xrold = xr;
xr = (fu*xl-fl*xu)/(fu-fl);
fr = f(xr);
iter = iter + 1;
e_a = abs((xr-xrold)/xr)*100;
test = fl*fr;

if test < 0
xu = xr;
fu = fr;
elseif test > 0
xl = xr;
fl = fr;
end
end
end
- (ii) Shooting-FP-IEuler FP

clear all;
close all;
clc;

N = 5;
h = 1;
tv = [0 5]
t = 0:5
C = 1;
vin = 10;
R = 5;
L = 2;

dqdt = @(t,i1,i2) i1 - i2;


di1dt = @(t,q) vin/L - q/(L*C);
di2dt = @(t,i1,i2) (i1 - i2)/(R*C);
i20 = 0.002;
q0 = 0.01;
i_g = [];

for i10 = 1.01:-0.01:0.105


[t, q, i_1, x] =
IEuler_using_False_p(dqdt,di1dt,di2dt,tv,q0,i10,i20,N);
i_g = [i_g i_1(end)];
end
i10 = 1.01:-0.01:0.105;

eq = polyfit(i10,i_g,1);
f = @(x) eq(1)*x + eq(2);
i10f = False_Position(f)
function [tvals, q, i1,
i2]=IEuler_using_False_p(dqdt,di1dt,di2dt,tv,q0,i10,i20,N)

tvals =tv(1); % initial t


q =q0; % initial y
i1 = i10;
i2 = i20;

tf=tv(2);
tstep = (tf-tvals)/N; % time step
numsteps = N; % number of steps to go
oldq = q(:,end);
oldi1 = i1(:,end);
oldi2 = i2(:,end);

for i=1:numsteps
oldt = tvals(end); % pick the last entry of array
newt = oldt + tstep; % step forward in t
%newy = fsolve(@(y) y - oldy- tstep*dydt(newt,y) , oldy); % backward
Euler
f1=@(q) q-tstep*dqdt(newt,i1(end),i2(end))-oldq;
f2=@(i1) i1-tstep*di1dt(newt,q(end))-oldi1;
f3=@(i2) i2-tstep*di2dt(newt,i1(end),i2(end))-oldi2;
newq = False_Position(f1);
newi1 = False_Position(f2);
newi2 = False_Position(f3);
tvals = [tvals newt];
q = [q newq];
i1 = [i1 newi1];
i2 = [i2 newi2];
oldq=newq;
oldi1=newi1;
oldi2=newi2;
end
end

function [xr]=False_Position(f)
iter = 0;
xl = 0;
xu = 100;
fl = f(xl);
fu = f(xu);
e_s = 0.0001;
e_a = 100;
xr = (xl+xu)/2;

while e_a > e_s

xrold = xr;
xr = (fu*xl-fl*xu)/(fu-fl);
fr = f(xr);
iter = iter + 1;
e_a = abs((xr-xrold)/xr)*100;
test = fl*fr;

if test < 0
xu = xr;
fu = fr;
elseif test > 0
xl = xr;
fl = fr;
end

end

end

function [xx] = Secant(f)


x(1) = 1;
x(2) = 5
e_a = 100;
e_s = 0.01;
i = 2;

while e_a > e_s


x(i+1) = x(i) - (x(i)-x(i-1))*f(x(i))/(f(x(i))-f(x(i-1)));
e_a = (x(i+1)-x(i))/x(i)*100;
i = i + 1;
xx = x(:,end)
end
end
(iii) Shooting-FP-IEuler-Secant:
clear all;
close all;
clc;

N = 5;
h = 1;
tv = [0 5]
t = 0:5
C = 1;
vin = 10;
R = 5;
L = 2;

dqdt = @(t,i1,i2) i1 - i2;


di1dt = @(t,q) vin/L - q/(L*C);
di2dt = @(t,i1,i2) (i1 - i2)/(R*C);

i20 = 0.002;
q0 = 0.01;
i_g = [];

for i10 = 1.01:-0.01:0.105


[q i_1 x] = IEuler_using_Secant(dqdt,di1dt,di2dt,tv,q0,i10,i20,N);
i_g = [i_g i_1(end)];
end
i10 = 1.01:-0.01:0.105;

eq = polyfit(i10,i_g,1);
f = @(x) eq(1)*x + eq(2);
i10f = False_Position(f)

function [q, i1, i2]=IEuler_using_Secant(dqdt,di1dt,di2dt,tv,q0,i10,i20,N)

tvals =tv(1); % initial t


q =q0; % initial y
i1 = i10;
i2 = i20;

tf=tv(2);
tstep = (tf-tvals)/N; % time step
numsteps = N; % number of steps to go
oldq = q(:,end);
oldi1 = i1(:,end);
oldi2 = i2(:,end);
for i=1:numsteps
oldt = tvals(end); % pick the last entry of array
newt = oldt + tstep; % step forward in t
%newy = fsolve(@(y) y - oldy- tstep*dydt(newt,y) , oldy); % backward Euler
f1=@(q) q-tstep*dqdt(newt,i1(end),i2(end))-oldq;
f2=@(i1) i1-tstep*di1dt(newt,q(end))-oldi1;
f3=@(i2) i2-tstep*di2dt(newt,i1(end),i2(end))-oldi2;
newq = Secant(f1);
newi1 = Secant(f2);
newi2 = Secant(f3);
tvals = [tvals newt];
q = [q newq];
i1 = [i1 newi1];
i2 = [i2 newi2];
oldq=newq;
oldi1=newi1;
oldi2=newi2;
end
end

function [xx] = Secant(f)


x(1) = 0;
x(2) = 5
e_a = 100;
e_s = 0.01;
i = 2;

while e_a > e_s


x(i+1) = x(i) - (x(i)-x(i-1))*f(x(i))/(f(x(i))-f(x(i-1)));
e_a = (x(i+1)-x(i))/x(i)*100;
i = i + 1;
xx = x(:,end)
end
end

function [xr]=False_Position(f)
iter = 0;
xl = 0;
xu = 100;
fl = f(xl);
fu = f(xu);
e_s = 0.0001;
e_a = 100;
xr = (xl+xu)/2;

while e_a > e_s

xrold = xr;
xr = (fu*xl-fl*xu)/(fu-fl);
fr = f(xr);
iter = iter + 1;
e_a = abs((xr-xrold)/xr)*100;
test = fl*fr;

if test < 0
xu = xr;
fu = fr;
elseif test > 0
xl = xr;
fl = fr;
end

end

end
(iv) Shooting-FP-Huen:

clear all;
close all;
clc;

N = 5;
h = 1;
tv = [0 5]
t = 0:5
C = 1;
vin = 10;
R = 5;
L = 2;

dqdt = @(i1,i2) i1 - i2;


di1dt = @(q) vin/L - q/(L*C);
di2dt = @(i1,i2) (i1 - i2)/(R*C);

i20 = 0.002;
q0 = 0.01;
i_g = [];

for i10 = 1.01:-0.01:0.105


[q, i_1, x] = Heun(tv(2),h,dqdt,di1dt,di2dt,q0,i10,i20);
i_g = [i_g i_1(end)];
end
i10 = 1.01:-0.01:0.105;

eq = polyfit(i10,i_g,1);
f = @(x) eq(1)*x + eq(2);
i10f = False_Position(f)

function[q,i1,i2]=Heun(T,dt,dqdt,di1dt,di2dt,q0,i10,i20)

time=0:dt:T; % Time discretization vector


Nt=length(time);

q(:,1)=q0 ; % Initial value


i1(:,1)=i10 ;
i2(:,1)=i20 ;

% Heun method
for i=1:Nt-1
p1=dqdt(i1(:,i),i2(:,i));
p2=dqdt(i1(:,i)+dt*p1,i2(:,i)+dt*p1); % Predictor step = Euler
q(:,i+1)= q(:,i)+dt*(p1+p2)/2 ; % Corrector step
p3=di1dt(q(:,i));
p4=di1dt(q(:,i)+dt*p3); % Predictor step = Euler
i1(:,i+1)= i1(:,i)+dt*(p3+p4)/2 ; % Corrector step
p5=di2dt(i1(:,i),i2(:,i));
p6=di2dt(i1(:,i)+dt*p5,i2(:,i)+dt*p5);

i2(:,i+1)= i2(:,i)+dt*(p5+p6)/2 ; % Corrector step


end

end

function [xr]=False_Position(f)
iter = 0;
xl = 0;
xu = 100;
fl = f(xl);
fu = f(xu);
e_s = 0.0001;
e_a = 100;
xr = (xl+xu)/2;

while e_a > e_s

xrold = xr;
xr = (fu*xl-fl*xu)/(fu-fl);
fr = f(xr);
iter = iter + 1;
e_a = abs((xr-xrold)/xr)*100;
test = fl*fr;

if test < 0
xu = xr;
fu = fr;
elseif test > 0
xl = xr;
fl = fr;
end

end

end
(B) Secant Method

(i) Shooting-Seccant-Euler:

clear all;
close all;
clc;

N = 5;
h = 1;
tv = [0 5]
t = 0:5
C = 1;
vin = 10;
R = 5;
L = 2;

dqdt = @(i1,i2) i1 - i2;


di1dt = @(q) vin/L - q/(L*C);
di2dt = @(i1,i2) (i1 - i2)/(R*C);

i20 = 0.002;
q0 = 0.01;
i_g = [];

for i10 = 1.01:-0.01:0.105


[q i_1 x] = Euler(dqdt,di1dt,di2dt,q0,i10,i20,N,h);
i_g = [i_g i_1(end)];
end
i10 = 1.01:-0.01:0.105;

eq = polyfit(i10,i_g,1);
f = @(x) eq(1)*x + eq(2);
i10f = Secant(f)
function [q i1 i2] = Euler(dqdt,di1dt,di2dt,q0,i10,i20,N,h)

i1 = zeros(1,N);
i2 = zeros(1,N);
q = zeros(1,N);
t = zeros(1,N);
i1(1) = i10;
i2(1) = i20;
q(1) = q0;

for i = 1:N

q(:,i+1)= q(:,i)+h*(dqdt(i1(:,i),i2(:,i)));
i1(:,i+1)= i1(:,i)+h*(di1dt(q(:,i)));
i2(:,i+1)= i2(:,i)+h*(di2dt(i1(:,i),i2(:,i)));

end
end
function [xx] = Secant(f)
x(1) = 0;
x(2) = 5
e_a = 100;
e_s = 0.01;
i = 2;

while e_a > e_s


x(i+1) = x(i) - (x(i)-x(i-1))*f(x(i))/(f(x(i))-f(x(i-1)));
e_a = (x(i+1)-x(i))/x(i)*100;
i = i + 1;
xx = x(:,end)
end
end

(ii) Shooting-secant-IEuler-FP

clear all;
close all;
clc;

N = 5;
h = 1;
tv = [0 5]
t = 0:5
C = 1;
vin = 10;
R = 5;
L = 2;

dqdt = @(t,i1,i2) i1 - i2;


di1dt = @(t,q) vin/L - q/(L*C);
di2dt = @(t,i1,i2) (i1 - i2)/(R*C);

i20 = 0.002;
q0 = 0.01;
i_g = [];

for i10 = 1.01:-0.01:0.105


[t, q, i_1, x] = IEuler_using_False_p(dqdt,di1dt,di2dt,tv,q0,i10,i20,N);
i_g = [i_g i_1(end)];
end
i10 = 1.01:-0.01:0.105;

eq = polyfit(i10,i_g,1);
f = @(x) eq(1)*x + eq(2);
i10f = Secant(f)
function [tvals, q, i1, i2]=IEuler_using_False_p(dqdt,di1dt,di2dt,tv,q0,i10,i20,N)

tvals =tv(1); % initial t


q =q0; % initial y
i1 = i10;
i2 = i20;

tf=tv(2);
tstep = (tf-tvals)/N; % time step
numsteps = N; % number of steps to go
oldq = q(:,end);
oldi1 = i1(:,end);
oldi2 = i2(:,end);

for i=1:numsteps
oldt = tvals(end); % pick the last entry of array
newt = oldt + tstep; % step forward in t
%newy = fsolve(@(y) y - oldy- tstep*dydt(newt,y) , oldy); % backward Euler
f1=@(q) q-tstep*dqdt(newt,i1(end),i2(end))-oldq;
f2=@(i1) i1-tstep*di1dt(newt,q(end))-oldi1;
f3=@(i2) i2-tstep*di2dt(newt,i1(end),i2(end))-oldi2;
newq = False_Position(f1);
newi1 = False_Position(f2);
newi2 = False_Position(f3);
tvals = [tvals newt];
q = [q newq];
i1 = [i1 newi1];
i2 = [i2 newi2];
oldq=newq;
oldi1=newi1;
oldi2=newi2;
end
end

function [xr]=False_Position(f)
iter = 0;
xl = 0;
xu = 100;
fl = f(xl);
fu = f(xu);
e_s = 0.01;
e_a = 100;
xr = (xl+xu)/2;

while e_a > e_s

xrold = xr;
xr = (fu*xl-fl*xu)/(fu-fl);
fr = f(xr);
iter = iter + 1;
e_a = abs((xr-xrold)/xr)*100;
test = fl*fr;

if test < 0
xu = xr;
fu = fr;
elseif test > 0
xl = xr;
fl = fr;
end
end
end

function [xx] = Secant(f)


x(1) = 0;
x(2) = 5
e_a = 100;
e_s = 0.01;
i = 2;

while e_a > e_s


x(i+1) = x(i) - (x(i)-x(i-1))*f(x(i))/(f(x(i))-f(x(i-1)));
e_a = (x(i+1)-x(i))/x(i)*100;
i = i + 1;
xx = x(:,end)
end
end
(iii) Shooting-Secant-IEuler_Secant:

clear all;
close all;
clc;

N = 5;
h = 1;
tv = [0 5]
t = 0:5
C = 1;
vin = 10;
R = 5;
L = 2;

dqdt = @(t,i1,i2) i1 - i2;


di1dt = @(t,q) vin/L - q/(L*C);
di2dt = @(t,i1,i2) (i1 - i2)/(R*C);

i20 = 0.002;
q0 = 0.01;
i_g = [];

for i10 = 1.01:-0.01:0.105


[q i_1 x] = IEuler_using_Secant(dqdt,di1dt,di2dt,tv,q0,i10,i20,N);
i_g = [i_g i_1(end)];
end
i10 = 1.01:-0.01:0.105;

eq = polyfit(i10,i_g,1);
f = @(x) eq(1)*x + eq(2);
i10f = Secant(f)
function [q, i1, i2]=IEuler_using_Secant(dqdt,di1dt,di2dt,tv,q0,i10,i20,N)

tvals =tv(1); % initial t


q =q0; % initial y
i1 = i10;
i2 = i20;
tf=tv(2);
tstep = (tf-tvals)/N; % time step
numsteps = N; % number of steps to go
oldq = q(:,end);
oldi1 = i1(:,end);
oldi2 = i2(:,end);

for i=1:numsteps
oldt = tvals(end); % pick the last entry of array
newt = oldt + tstep; % step forward in t
%newy = fsolve(@(y) y - oldy- tstep*dydt(newt,y) , oldy); % backward Euler
f1=@(q) q-tstep*dqdt(newt,i1(end),i2(end))-oldq;
f2=@(i1) i1-tstep*di1dt(newt,q(end))-oldi1;
f3=@(i2) i2-tstep*di2dt(newt,i1(end),i2(end))-oldi2;
newq = Secant(f1);
newi1 = Secant(f2);
newi2 = Secant(f3);
tvals = [tvals newt];
q = [q newq];
i1 = [i1 newi1];
i2 = [i2 newi2];
oldq=newq;
oldi1=newi1;
oldi2=newi2;
end
end
function [xx] = Secant(f)
x(1) = 0;
x(2) = 5
e_a = 100;
e_s = 0.01;
i = 2;
while e_a > e_s
x(i+1) = x(i) - (x(i)-x(i-1))*f(x(i))/(f(x(i))-f(x(i-1)));
e_a = (x(i+1)-x(i))/x(i)*100;
i = i + 1;
xx = x(:,end)
end
end

(iv) Shooting-Secant-Huen:

clear all;
close all;
clc;

N = 5;
h = 1;
tv = [0 5]
t = 0:5
C = 1;
vin = 10;
R = 5;
L = 2;

dqdt = @(i1,i2) i1 - i2;


di1dt = @(q) vin/L - q/(L*C);
di2dt = @(i1,i2) (i1 - i2)/(R*C);

i20 = 0.002;
q0 = 0.01;
i_g = [];

for i10 = 1.01:-0.01:0.105


[q i_1 x] = Euler(dqdt,di1dt,di2dt,q0,i10,i20,N,h);
i_g = [i_g i_1(end)];
end
i10 = 1.01:-0.01:0.105;

eq = polyfit(i10,i_g,1);
f = @(x) eq(1)*x + eq(2);
i10f = Secant(f)
function [q i1 i2] = Euler(dqdt,di1dt,di2dt,q0,i10,i20,N,h)

i1 = zeros(1,N);
i2 = zeros(1,N);
q = zeros(1,N);
t = zeros(1,N);
i1(1) = i10;
i2(1) = i20;
q(1) = q0;

for i = 1:N
q(:,i+1)= q(:,i)+h*(dqdt(i1(:,i),i2(:,i)));
i1(:,i+1)= i1(:,i)+h*(di1dt(q(:,i)));
i2(:,i+1)= i2(:,i)+h*(di2dt(i1(:,i),i2(:,i)));
end
end
function [xx] = Secant(f)
x(1) = 0;
x(2) = 5
e_a = 100;
e_s = 0.01;
i = 2;

while e_a > e_s


x(i+1) = x(i) - (x(i)-x(i-1))*f(x(i))/(f(x(i))-f(x(i-1)));
e_a = (x(i+1)-x(i))/x(i)*100;
i = i + 1;
xx = x(:,end)
end
end
(C) Modified Secant Method:

(i) Shooting-ModSec-Euler:

clear all;
close all;
clc;

N = 5;
h = 1;
tv = [0 5]
t = 0:5
C = 1;
vin = 10;
R = 5;
L = 2;

dqdt = @(i1,i2) i1 - i2;


di1dt = @(q) vin/L - q/(L*C);
di2dt = @(i1,i2) (i1 - i2)/(R*C);

i20 = 0.002;
q0 = 0.01;
i_g = [];

for i10 = 1.01:-0.01:0.105


[q i_1 x] = Euler(dqdt,di1dt,di2dt,q0,i10,i20,N,h);
i_g = [i_g i_1(end)];
end
i10 = 1.01:-0.01:0.105;

eq = polyfit(i10,i_g,1);
f = @(x) eq(1)*x + eq(2);
i10f = Modified_Secant(f)
function [q i1 i2] = Euler(dqdt,di1dt,di2dt,q0,i10,i20,N,h)

i1 = zeros(1,N);
i2 = zeros(1,N);
q = zeros(1,N);
t = zeros(1,N);
i1(1) = i10;
i2(1) = i20;
q(1) = q0;

for i = 1:N

q(:,i+1)= q(:,i)+h*(dqdt(i1(:,i),i2(:,i)));
i1(:,i+1)= i1(:,i)+h*(di1dt(q(:,i)));
i2(:,i+1)= i2(:,i)+h*(di2dt(i1(:,i),i2(:,i)));

end
end
function [xx] = Modified_Secant(f)
x(1) = 0;
x(2) = 5 ;
e_a = 100;
e_s = 0.01;
i = 2;
delta = 0.01;

while e_a > e_s


x(i+1) = x(i) - delta*x(i)*f(x(i))/(f(x(i)+delta*x(i))-f(x(i)));
e_a = (x(i+1)-x(i))/x(i)*100;
i = i + 1;
xx = x(:,end)
end
end

(ii) Shooting_Modsec_IEuler_FP

clear all;
close all;
clc;

N = 5;
h = 1;
tv = [0 5]
t = 0:5
C = 1;
vin = 10;
R = 5;
L = 2;

dqdt = @(t,i1,i2) i1 - i2;


di1dt = @(t,q) vin/L - q/(L*C);
di2dt = @(t,i1,i2) (i1 - i2)/(R*C);

i20 = 0.002;
q0 = 0.01;
i_g = [];

for i10 = 1.01:-0.01:0.105


[t, q, i_1, x] = IEuler_using_False_p(dqdt,di1dt,di2dt,tv,q0,i10,i20,N);
i_g = [i_g i_1(end)];
end
i10 = 1.01:-0.01:0.105;

eq = polyfit(i10,i_g,1);
f = @(x) eq(1)*x + eq(2);
i10f = Modified_Secant(f)
function [tvals, q, i1, i2]=IEuler_using_False_p(dqdt,di1dt,di2dt,tv,q0,i10,i20,N)

tvals =tv(1); % initial t


q =q0; % initial y
i1 = i10;
i2 = i20;

tf=tv(2);
tstep = (tf-tvals)/N; % time step
numsteps = N; % number of steps to go
oldq = q(:,end);
oldi1 = i1(:,end);
oldi2 = i2(:,end);

for i=1:numsteps
oldt = tvals(end); % pick the last entry of array
newt = oldt + tstep; % step forward in t
%newy = fsolve(@(y) y - oldy- tstep*dydt(newt,y) , oldy); % backward Euler
f1=@(q) q-tstep*dqdt(newt,i1(end),i2(end))-oldq;
f2=@(i1) i1-tstep*di1dt(newt,q(end))-oldi1;
f3=@(i2) i2-tstep*di2dt(newt,i1(end),i2(end))-oldi2;
newq = False_Position(f1);
newi1 = False_Position(f2);
newi2 = False_Position(f3);
tvals = [tvals newt];
q = [q newq];
i1 = [i1 newi1];
i2 = [i2 newi2];
oldq=newq;
oldi1=newi1;
oldi2=newi2;
end
end

function [xr]=False_Position(f)
iter = 0;
xl = 0;
xu = 100;
fl = f(xl);
fu = f(xu);
e_s = 0.01;
e_a = 100;
xr = (xl+xu)/2;

while e_a > e_s

xrold = xr;
xr = (fu*xl-fl*xu)/(fu-fl);
fr = f(xr);
iter = iter + 1;
e_a = abs((xr-xrold)/xr)*100;
test = fl*fr;

if test < 0
xu = xr;
fu = fr;
elseif test > 0
xl = xr;
fl = fr;
end

end

end

function [xx] = Modified_Secant(f)


x(1) = 0;
x(2) = 5 ;
e_a = 100;
e_s = 0.01;
i = 2;
delta = 0.01;

while e_a > e_s


x(i+1) = x(i) - delta*x(i)*f(x(i))/(f(x(i)+delta*x(i))-f(x(i)));
e_a = (x(i+1)-x(i))/x(i)*100;
i = i + 1;
xx = x(:,end)
end
end

(iii) Shooting-ModSec-IEuler-secant:

clear all;
close all;
clc;

N = 5;
h = 1;
tv = [0 5]
t = 0:5
C = 1;
vin = 10;
R = 5;
L = 2;

dqdt = @(t,i1,i2) i1 - i2;


di1dt = @(t,q) vin/L - q/(L*C);
di2dt = @(t,i1,i2) (i1 - i2)/(R*C);

i20 = 0.002;
q0 = 0.01;
i_g = [];

for i10 = 1.01:-0.01:0.105


[t, q, i_1, x] = IEuler_using_Secant(dqdt,di1dt,di2dt,tv,q0,i10,i20,N);
i_g = [i_g i_1(end)];
end
i10 = 1.01:-0.01:0.105;

eq = polyfit(i10,i_g,1);
f = @(x) eq(1)*x + eq(2);
i10f = Modified_Secant(f)
function [tvals, q, i1, i2]=IEuler_using_Secant(dqdt,di1dt,di2dt,tv,q0,i10,i20,N)

tvals =tv(1); % initial t


q =q0; % initial y
i1 = i10;
i2 = i20;

tf=tv(2);
tstep = (tf-tvals)/N; % time step
numsteps = N; % number of steps to go
oldq = q(:,end);
oldi1 = i1(:,end);
oldi2 = i2(:,end);

for i=1:numsteps
oldt = tvals(end); % pick the last entry of array
newt = oldt + tstep; % step forward in t
%newy = fsolve(@(y) y - oldy- tstep*dydt(newt,y) , oldy); % backward Euler
f1=@(q) q-tstep*dqdt(newt,i1(end),i2(end))-oldq;
f2=@(i1) i1-tstep*di1dt(newt,q(end))-oldi1;
f3=@(i2) i2-tstep*di2dt(newt,i1(end),i2(end))-oldi2;
newq = False_Position(f1);
newi1 = False_Position(f2);
newi2 = False_Position(f3);
tvals = [tvals newt];
q = [q newq];
i1 = [i1 newi1];
i2 = [i2 newi2];
oldq=newq;
oldi1=newi1;
oldi2=newi2;
end
end
function [xr]=False_Position(f)
iter = 0;
xl = 0;
xu = 100;
fl = f(xl);
fu = f(xu);
e_s = 0.01;
e_a = 100;
xr = (xl+xu)/2;
while e_a > e_s

xrold = xr;
xr = (fu*xl-fl*xu)/(fu-fl);
fr = f(xr);
iter = iter + 1;
e_a = abs((xr-xrold)/xr)*100;
test = fl*fr;

if test < 0
xu = xr;
fu = fr;
elseif test > 0
xl = xr;
fl = fr;
end
end
end

function [xx] = Modified_Secant(f)


x(1) = 0;
x(2) = 5 ;
e_a = 100;
e_s = 0.01;
i = 2;
delta = 0.01;

while e_a > e_s


x(i+1) = x(i) - delta*x(i)*f(x(i))/(f(x(i)+delta*x(i))-f(x(i)));
e_a = (x(i+1)-x(i))/x(i)*100;
i = i + 1;
xx = x(:,end)
end
end
(iv) Shooting-ModSec-Huen:

clear all;
close all;
clc;

N = 5;
h = 1;
tv = [0 5]
t = 0:5
C = 1;
vin = 10;
R = 5;
L = 2;

dqdt = @(i1,i2) i1 - i2;


di1dt = @(q) vin/L - q/(L*C);
di2dt = @(i1,i2) (i1 - i2)/(R*C);

i20 = 0.002;
q0 = 0.01;
i_g = [];

for i10 = 1.01:-0.01:0.105


[q, i_1, x] = Heun(tv(2),h,dqdt,di1dt,di2dt,q0,i10,i20);
i_g = [i_g i_1(end)];
end
i10 = 1.01:-0.01:0.105;

eq = polyfit(i10,i_g,1);
f = @(x) eq(1)*x + eq(2);
i10f = Modified_Secant(f)

function[q,i1,i2]=Heun(T,dt,dqdt,di1dt,di2dt,q0,i10,i20)

time=0:dt:T; % Time discretization vector


Nt=length(time);

q(:,1)=q0 ; % Initial value


i1(:,1)=i10 ;
i2(:,1)=i20 ;

% Heun method
for i=1:Nt-1

p1=dqdt(i1(:,i),i2(:,i));
p2=dqdt(i1(:,i)+dt*p1,i2(:,i)+dt*p1); % Predictor step = Euler
q(:,i+1)= q(:,i)+dt*(p1+p2)/2 ; % Corrector step
p3=di1dt(q(:,i));
p4=di1dt(q(:,i)+dt*p3); % Predictor step = Euler
i1(:,i+1)= i1(:,i)+dt*(p3+p4)/2 ; % Corrector step
p5=di2dt(i1(:,i),i2(:,i));
p6=di2dt(i1(:,i)+dt*p5,i2(:,i)+dt*p5);

i2(:,i+1)= i2(:,i)+dt*(p5+p6)/2 ; % Corrector step


end

end

function [xx] = Modified_Secant(f)


x(1) = 0;
x(2) = 5 ;
e_a = 100;
e_s = 0.01;
i = 2;
delta = 0.01;

while e_a > e_s


x(i+1) = x(i) - delta*x(i)*f(x(i))/(f(x(i)+delta*x(i))-f(x(i)));
e_a = (x(i+1)-x(i))/x(i)*100;
i = i + 1;
xx = x(:,end)
end
end

Part (iii)
The choice of root finder and solver is between Heun and False position methods.

Explicit Eulers method which is known as conventional Eulers, is notoriously known for having
a high global (local and truncated) Errors. Whereas, The Improved Euler's method, also known
as the Heun formula or the average slope method, gives a more accurate approximation than
the Euler rule and gives an explicit formula for computing yn+1. The basic idea is to correct
some error of the original Euler's method. Heun's method which is based on conventional
eulers is twice as accurate compared to conventional Eulers having a global error of O(h^2)
due to its predictor and corrector action. Eulers method is first order convergent while Heuns
is second order convergent. Therefore, Heuns method provides more accuracy than
implicit/explicit Euler methods.

In general, Braketing methods such as False position an algorithm for finding roots which
retains that prior estimate for which the function value has opposite sign from the function
value at the current best estimate of the root. In this way, the method of false position keeps
the root bracketed, return more accurate values if the computation power is available. Since
Matlab is available, False position is the choice of root finder. False position is chosen over
secant method because the False position method will converge on a solution given enough
iterations, however the Secant method on certain instances may not converge at all.

Out of the 2 methods in question False position is chosen because it shows a very low
asymptotic error than secant method. If speed is the requirement Secant Method will be
suitable( given that secant method is convergent on the solution).

You might also like