You are on page 1of 5

%Script that demonstrates Euler integration for a second order problem using

Matlab.
% The problem to be solved is:
%y''(t)+100*y'(t)+1E4*y(t)=1E4*abs(sin(377t))
h=0.0001;
t=0:h:0.08;
clear x;

%h is the time step.
%initialize time variable.
%wipe out old variable.

x(1,:)=[0 0]; %initial condition are zero.
%Note that x is two dimensional since there are two state variables.
for i=2:length(t),

%Set up "for" loop.

%Calculate the values of k1 (note that it is a vector of length 2).
k1=[x(i-1,2) 1E4*abs(sin(377*t(i)))-100*x(i-1,2)-1E4*x(i-1,1)];
%Calculate derivatives;
x(i,:)=x(i-1,:)+k1*h;
%Estimate new value of x;
end
%Plot approximate and exact solution.
plot(t,x(:,1),'r',t,abs(sin(377*t)),'b');
legend('Output','Input');
title('Euler Approximation, Second order system');
xlabel('Time');
ylabel('Input and Output');

but it may not be obvious how to apply it to the approximation of differential equations of higher order. Let x1(t)=y(t). The circuit parameter are R=400. and Vm=1. L=4H. We will introduce two new variables. . The first thing we must do is to recast the problem in terms of first order equations. Note: this differential equation represents the rectifier circuit from section 7-3 of Circuits Devices and Systems.The Euler Method for Higher Order Differential Equations The method described above is often sufficient to approximate first order differential equations. These equations can also be expressed as a first order matrix equation. This technique is also called state variable analysis. Consider the differential equation: We will assume initial conditions are all zero. The trick here is to break down the higher order differential equation into several first order differential equations. but poses no particular problem for Euler's method. by Smith (your E11 text). Taking the absolute value of the sine wave is equivalent to rectification. This problem would be extremely hard to solve analytically. C=25F. and y(t) is equal to the output voltage. x1(t) and x2(t). =377 rad/sec(60 Hz). We can then write two coupled first order equations.

3) From this value find an approximate value for each xi*(to+h). . 4) Let to=to+h. Call these k1i. choose a value for h. x2(to). . 1) Starting at time to. and find initial conditions for all state variables x1(to).We can now use Euler's method to solve both first order equations simultaneously by following the following procedure (which is a simple modification of the procedure for first order equations).. and for each xi. let xi(to)=xi*(to+h). 5) Repeat steps 2 through 4 until the solution is finished. 2) From the values of xi(to) calculate derivatives for each xi(t) at t=to..

00001.:)=[0]. t=0:h:2.668*heaviside(t(i)-0.:)=x(i-1.1).5+(1/20))-heaviside(t(i)0. clear x. clear x y. for i=2:length(t) k1=[x(i-1.2)-208.:)+k1*h. t=0:h:2.001.5.1)].1).5. x(i.'r'). struja tacan kod h=0.1).6889*x(i-1. t=0:h:2.2612*x(i-1.1)]. end plot(t. clear x.001.:)+k1*h.'r').5)-39. Struja h=0.:)=[0 0]. y(i.2) 1712.:)+k1*h.6207*x(i1.2)-207. x(1.:)=[0 0].668*heaviside(t(i)-0.2222*10*heaviside(t(i)-0.2222*(1/(1/10)*(heaviside(t(i)-0.2) 1712.2)-208.2) 2.5)-39*y(i-1)-2.Brzina h=0. . for i=2:length(t) k1=[x(i-1. x(1. for i=2:length(t) k2=[2.6207*x(i1. end plot(t.:)=[0 0].:)=x(i-1. y(1.:)=x(i-1. end x(:. x(i.2612*x(i-1. x(i.2328*x(i-1.5)-39. for i=2:length(t) k1=[x(i-1.:)+k2*h. x(1.x(:.5-1/20))) -39*x(i-1.5.x(:.1)].:)=y(i-1.1)].

end plot(t.1).'r'). .y(:.