Sunteți pe pagina 1din 11

Trapezoidal Method

The trapezoid method averages the Euler and backward Euler methods, advancing the
approximate solution at each step along a line whose slope is the arithmetic mean of the
derivatives at its endpoints. Specifically, from an approximate solution value y
k
at time t
k
for an
ODE y = f(x, y), the trapezoid method approximates the solution y(x
k+1
) at time x
k+1
= x
k
+ h
k
by
solving the implicit equation y
k+1
= y
k
+ h
k
[f(x
k
, y
k
) + f(x
k+1
, y
k+1
)] 2 for y
k+1
. Since this equation
may be nonlinear, solving it generally requires an iterative solution method. Any iterative
method for solving this equation can be implemented such as bisection method,newton method.
A graphical visualization of trapezoidal integration:


The inegration ( , ) f x y
}
by finding the area under it. So area under the f(x,y) is divided into
large number of sub section. Area of one section [x
o
,x
1
] can be approximated as area of the dark
shaded trapezoidal i.e:

Area=Area of the trapezoid=
0 0 1 1
[ ( , ), ( , )]
2
h
f x y f x y

Differential equation Problem: ( , )
dy
f x y
dx
=

As we know that the solution of ( , )
dy
f x y
dx
= over [x
n
, x
n+1
] is
1
1
( , )
n
n
x
n n
x
y y f x y dx
+
+
= +
}

Applying simple trapezoidal rule we have :
Equation-1
It can also be represented as
1
( ) 0
n
g y
+
=

Now the Equation-1 may be a nonlinear equation with root y
n+1
. So any root finding methods
can be used to solve this equation. Newton method can be used for finding the root of this
equation. The solution of any equation by Newton method needs a good initial guess for
convergence. The initial guess can be y
n+1
from Euler method as

Initial guess :
1
( , )
n n n n
y y h f x y
+
= +

With this initial guess Newton method iteration is started.
Newton Method :

1 1
1 1
1
( )
( )
i
i i n
n n
i
n
g y
y y
g y
+ +
+ +
+
=
'

And
0
1
initial guess from Euler method
n
y
+
= .
| |
1 1 1
( , ) ( , )
2
n n n n n n
h
y y f x y f x y
+ + +
= + +
Algorithm for Trapezoidal Method

















No

Yes

0
1 1
pass ( ) and
n n
g y y
+ +
1
return corrected
n
y
+
Start
Inputs: Step Size(h), Initial Values of X and Y, Final Value of X
for solution, and ( , )
dy
f x y
dx
=

Create a array containing x axis required values. And a null initialization of Y axis values.


Create a initial guess for y
n+1
from Euler method.
Create a function
| |
1 1 1 1 ( ) ( , ) ( , )
2
n n n n n n n
h
g y y y f x y f x y + + + + = +
Call Newton function



Store the y
n+1
corrected value to the Y axis array.
If x
n+1
=final
value of x
Newton function:
1 1
1 1
1
( )
( )
i
i i n
n n
i
n
g y
y y
g y
+ +
+ +
+
=
'

Iterate the above equation till required convergence is achived.
Plot x and y
Exit
Implementation of Algorithm Using MATLAB

Main Program:

f=input('Enter the Function= ','s');
h=input('Enter the step size: ');
srt=input('From which value of x you want to start the solution: ' );
last=input('Input upto how much you want to end the solution: ');
x=srt:h:last;
y=zeros(1,length(x)); % Output Matrix
y(1)=input('Enter the initial condition for y at initial x: ');
f=inline(f);
i=2;
while (i<=(length(x)))
y(i)=eulers(x(i),h,y(i-1),char(f));
%y(i)=y(i-1) + h*f(x(i-1),y(i-1)); % Initial Guess for solving the
Non linera Equation
%f(n)=f(x(n),y(n))
% Trapezoidal Method: y(n)= y(n-1) + stepsize*0.5*(f(n-1)+f(n))
% which is a non linear Equation To be solved by Newton method
% Taking 1st Guess as from Euler Method
%Formulation Of string to call newton method: g(y)=0; i.e :
g(y)=y(n)- y(n-1) - stepsize*0.5*(f(n-1)+f(n))
% For formulation of g(y), a symbolic variable p is defined..so
% g(p)=0 can be sloved using Newton
syms p; % A symbolic variable
k=sym(h*0.5*f(x(i-1),y(i-1)));
k=k+h*0.5*f(x(i),p);

k=p-sym(y(i-1))-k; % k=g(p) The symbolic function

y(i)=newton(char(k),y(i)); % Solution of nonlinear equation

i=i+1;

end
plot(x,y);
grid on;
xlabel('x');
ylabel('y');
title('Solution ' );











Euler Initial Guess Program:

This function is called to calculate the initial guess by dividing the [x
n
,x
n+1
] again in steps
and the return initial guess.

.
function [initial]=eulers(startx,step,starty,f)

f=inline(f);
for x=startx:0.01:(startx+step)
starty=starty+0.01*f(startx,starty);
end
initial=starty;


Newtons Method Program:


function [y]=newton(f,starty)
f=inline(f); % Input String to inline function
df=sym(f); % symbolic function from inlline function
df=diff(df); % Differntial of input function f
df=inline(char(df));
i=true(1); % logical varible with true value 1
k=1;
while(i)
y=starty - f(starty)/df(starty); % Iteration of newton method
er=y-starty;

starty=y;
k=k+1;
if er<0.001
i=false(1);
end

end
y=starty;








Simulation of Electrical Circuits:
Electrical circuit can be simulated for transient analysis using differential equation solution
methods numerically having first order characteristic equation. A given network of passive
element and sources which is initially in a known state with respect to all voltages and currents.
At a reference time t=0 the system is altered in a manner that can be represented by closing and
opening of switch. The objective of simulation is to find current, voltage etc. with respect to time
measured from the time when switch position is changed.
Simulation of RL Series Circuit
After switch is closed and taking the reference time t=0 the circuit will look like the given figure
V
R
L

The Krichhoff voltage equation for the ciruit is

di
Ri L V
dt
+ =

This is a non homogenous linear first order equation and it can be rearranged as
di V R
i
dt L L
=

Now we have a differential equation in the form of dy/dx=f(x,y).
R=1, L=1mH,V=1V and Solving:
Note : T
c
=1ms. So step size should be less than step size. And final value of t should be atleast
4T
c
to analyze the circuit properly.
1000 1000
di
i
dt
=


Simulation of the circuit can be done with different initial current. Taking initial condition as
0A,-2A,3A the plot between current and time is


Fig-1. I
0
=0A Fig 2.I
0
=-2A









Fig-3 I
0
=3A
It can be observed that after some time the current is settled to V/R value. With any initial
condition stead state value of current will be same as long as the values of components remain
same.






Simulation Parallel RC Circuit


After switch is closed at t=0 the circuit diagram of parallel RC circuit is











Krichhoff s current equation to the upper node will give the equation as

dv v
C I
dt R
+ =
It can be rearranged as
dv I v
dt C RC
= .
Taking R=1,I=1A, C=10mF, RC=T
c
=10ms. The differential equation is
100 100
dv
v
dt
=

This can be solved to obtain the plot between values of voltages and current. Simulation can also
be done using different initial values of capacitor voltages.







Fig-4 V
c0
=0V Fig-5 V
c0
=5V
















Fig-6 V
c0
= -3V



After some time voltage value always goes to steady state i.e IR irrespective of initial
capacitor voltage.










Transient Voltage Across The Switch

Below circuit is in the steady state and with the current of V/R. Now switch is suddenly
opened at t=0 instant. Voltage across switch is calculated. All switches are non ideal in
nature. Semiconductor switches have certain turn off time. After turnoff time switch will
stop conducting. During turn off there will be very high voltage appearing across the
switch due to decay in current as inductor will oppose the change in this current.
Example :

R=20,V=100V,L=2mH
T
off
=10s



Voltage across the switch is 1100V during turn off time. The switch should be designed to
withstand such voltages otherwise it will be damaged during operation.


3
5
5 0
100 2 10
10
1100
s
s
s
s
di
V V L
dt
i
V V L
t
V
V V

= +
A
= +
A

= +
=
Bibilography
1. An Introduction To Numerical Analysis 2
nd
Ed. by Kendall E. Atkinson.
2. Network Analysis 3
rd
Ed by M.E Valkenburg
3. MATLAB Documentation, www.mathwork.in

S-ar putea să vă placă și