Custom Euler's Method for Second Order ODE
4 次查看(过去 30 天)
显示 更早的评论
Hello, I am trying to develop a way to solve a specific differential equation using Euler's method. I have some code so far, but it is not giving the correct approximation and I cannot see where I made an error. The ODE represents a second order RLC circuit.
The ODE I am trying to approximate: v'' + 5v' + 1000v = u(t-1) - u(t-3) where u(t) is the unit step function.
The approximation is supposed to look something like this:

Instead, my code gives me something like this:

Here is my code:
% Euler's Method Code:
v(1,1) = 0; v(2,1) = 0; time(1)=0; %Initial conditions
h = .0001; %step size
N = 5/h; %Set up recursions
%Set up A matrix
A = [0,1;-5,-1000];
for k = 1:N
time(k+1) = time(k)+h; %concatenates new time position to time vector
v(:,k+1)=v(:,k) + h * A * v(:,k) + ...
h * [0; heaviside(time(k)-1)-heaviside(time(k)-3)];
end
figure('Name','Part 1 Question 11')
plot(time,v(1,:));
xlabel('time (s)')
ylabel('Capacitor Voltage')
xlim([0,5]);
I have a feeling it may be something to do with the driving function. The driving function is made up of the difference of the two unit step/ heaviside functions.
I have tried using changing the matrix inside the for loop from this:
[0; heaviside(time(k)-1)-heaviside(time(k)-3)]
To this:
[0; (time(k)>1)-(time(k)>3)]
But then the graphed solution is 0 for the duration of the graph. I've been staring at this for hours trying to figure out where my solution is messed up.
Thank you in advance for any help or suggestions.
0 个评论
采纳的回答
James Tursa
2020-4-24
编辑:James Tursa
2020-4-24
Your derivative matrix is wrong. It should be this (the -5 and -1000 are switched):
A = [0,1;-1000,-5];
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
