MATLAB Program to solve differential equation using Euler's method
%Program to solve Differential equation using Euler's method
%The euation is: dI1/dt = I1*
%Mapping with the equations from network to the program:
%I = I1*
%Consider initial value of I as 2 and performing 50 iterations to solve the
%equation.
clear all;
I0= 2; %Initial value of I
iterations = 50;
dt = 0.1; %Delta t as Step Size
I = zeros(iterations,1); % this initializes the vector I to being all zeros
t = zeros(iterations,1);
I(1) = I0; % the initial condition
t(1) = 0.0;
for step=1:iterations-1 % Performing iterations
I(step+1) = I(step) + dt*(I(step));
t(step+1) = t(step) + dt;
end
I %print values of I
plot(t,I,'b'); %Plotting the result
title('Solution of First Differential Equation')
xlabel('Time')
ylabel('Current')
hold on; %keep the previously plotted lines
%Program to solve Differential equation using Euler's method
%The euation is: dI1/dt = -I1
%Mapping with the equations from network to the program:
%I = I1
%Consider initial value of I as 2 and performing 50 iterations to solve the
%equation.
Iini= 1; %Initial value of I
I1 = zeros(iterations,1); % this initializes the vector I to being all zeros
t = zeros(iterations,1);
I1(1) = Iini; % the initial condition
t(1) = 0.0;
for step=1:iterations-1 % Performing iterations
I1(step+1) = I1(step) - dt*(I1(step));
t(step+1) = t(step) + dt;
end
I1 %print values of I
plot(t,I1,'r'); %Plotting the result
title('Solution of First Differential Equation')
xlabel('Time')
ylabel('Current')
hold on; %keep the previously plotted lines
No comments