Trying to develop a program to implement Euler's Method for a second order system

9 views (last 30 days)
Basically, I need to write a program in which I can input a second order ODE in and have it solve over a certain number of time steps. I am 2 years out of my last MATLAB course and I have absolutely no idea where to even begin with this. It has to be able to solve the following equation as a test: y''=0.5(-3y'-y+1-t)
  2 Comments
James Tursa
James Tursa on 11 Oct 2018
Edited: James Tursa on 11 Oct 2018
What code have you written so far? What specific problems are you having with it? Are you asking the user to input an equation and then have your code work with that? Or ...?
Gavin Stockstill
Gavin Stockstill on 11 Oct 2018
I don't really have any code yet because I am not sure where to start. I know the process begins by converting to two first order ODE's which I can do by hand. I just don't know how to go about solving both of them in MATLAB. I understand that it will require a function file, which is something I do not understand how to use. I've tried multiple different online examples and I can't get any of them to work properly with my equation. As far as the input, the assignment only calls for it to solve the equation I posted so it doesn't really need to have the user input an equation. I can just put it in the program.

Sign in to comment.

Answers (1)

James Tursa
James Tursa on 11 Oct 2018
Here's a start:
Create a file called deriv.m in your working directory. Since you will have two first order equations, define a two element vector y as follows:
y(1) = The original y in your equation
y(2) = The original derivative y' in your equation
Then the derivatives of these elements will be
dy(1) = d(y) = y' = y(2)
dy(2) = d(y') = y'' = your expression on the rhs of your 2nd order equation
Thus, you function file will be
function dy = deriv(t,y)
dy = zeros(2,1); % pre-allocate your derivative
dy(1) = y(2); % y'
dy(2) = ____; % you fill this in based on your 2nd order equation for y'' from above
end
Then just use the basic Euler formula:
ynext = y + dt*deriv(t,y)
tnext = t + dt
and write some looping code around this to get the solution for the range you want.
  4 Comments
Gavin Stockstill
Gavin Stockstill on 11 Oct 2018
Ok, I think I got the function correct:
function dy = deriv(t,y)
dy = zeros(2,1);
dy(1) = y(2);
dy(2) = 0.5*(-3*y(2)-y(1)+1-t);
end
So in another file, I put this:
y = y0;
t = t0;
y0 = [4;-2];
t0 = 0;
ynext = y + dt*deriv(t,y);
t next = t + dt;
And that gives me errors in line 1. Also, what should my looping code look like? I'm sorry these are simple questions, but I have much less experience in MATLAB than my professor thinks.
James Tursa
James Tursa on 12 Oct 2018
Edited: James Tursa on 12 Oct 2018
Think about the order of the lines in your above code. How can you assign y0 to y before you define what y0 is? You need to define y0 and t0 before you use them on the rhs of an assignment.
The ynext and tnext stuff is just conceptual. You need to define the stepsize dt, and then define how many steps you will be taking, and then write a for-loop to do the stepping of the Euler code. Do you know how to write a for-loop?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!