Introduction and Euler\'s Method

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to matlab programming for numerical computations we are in week number seven in module seven we are going to cover ordinary differential equations initial value problem in this module we are going to tackle methods for solving ordinary differential equations in lecture 7.1 we are going to go over introducing ourselves to odes and take up one example and solve it using euler's method euler's method is one of the simplest methods for solving odes so let's let's look into the ode problem so introduction what is an od ode is when we want to solve an equation of the form dy by d t equal to f of t comma y subject to initial conditions y at some time t equal to zero is given as y zero we start with that initial condition and march forward in time at each time finding the value of y at that corresponding time instant in this way we will get the overall curve y as a function of t which i have plotted over here now the function f of t comma y is nothing but the slope of this v versus speaker at any point in the domain okay so if we are going to start with our y 0 t 0 we will then march on and find y 1 t 1 y 2 t 2 y 3 t 3 so on and so forth is what we are going to actually find eventually we will develop the entire curve y as a function of t and that is the actual solution that we are intending to find using a numerical scheme the discussion and theory for numerical methods for solving ode ivp initial value problems is discussed in computational techniques course module 7. the link for which is given over here in this lecture or for that matter in this course we are not going to cover any theory or discussions regarding ode ivp rather we will use matlab in order to solve the initial value problems let us get started with one of the simplest methods which is known as the euler's method before we go on to euler's methods method let's look at how would we want to solve this ode we can write d y by dt in a finite difference form y i minus y i plus 1 minus y i divided by h where h is known as the step size okay if we remove the limit h tends to 0 and take h as a very very small value we can rewrite this equation in the form y i plus 1 equal to y i plus h multiplied by function f calculated at t comma y now if the function f is calculated at y i and t i we get what is known as euler's forward difference or soil then we get what is known as euler's explicit method in general numerical methods for ode ivp we've tried to find certain slope s i which is the best estimate that can be used in order to march forward along this curve so s i is going to be dependent on y y's and t's and we are going to find that particular s i such that we get y i plus 1 as close as possible to the true solution in the simplest case we can consider s i as nothing but f calculated at t i comma y i and that's what euler's explicit method is y i plus 1 equal to y i plus h times f of t i y i is the formula that we will use for euler's explicit method at the initial time we have y 0 t 0 we can use this in order to compute now y 0 is known t 0 is known and y 0 is known therefore f at t 0 y 0 is known we can use this to compute y 1 once y 1 is known and t 1 is known we can use them to compute y 2 that we can use to compute y 3 so on and so forth ok so this becomes a fairly simple straightforward method in order to solve oda initial value problems the problem with euler's explicit method are two the first problem is that the method is not very accurate to tackle this problem we are going to talk about runge-kutta methods in lecture 7.2 and thereafter the second problem is uh regarding the step size that we need to choose in order to get the system to be to be stable uh stability of numerical methods is something that i will introduce in this lecture but that is beyond the scope of this course i will introduce the concepts of stability and so on mainly for the sake of completeness although its not strictly a part of this matlab course okay so the example that we will solve is d y by d t equal to minus 2 t y with the initial condition y 0 equal to 1. if we solve this analytically the solution we are going to get y at any time t is going to be equal to e exponential of minus p square what we want to do is use euler's explicit method and compare the solution using euler's explicit method to the analytical solution that we have written down over here let us go on to matlab to solve this problem using euler's explicit method okay let us create a file called euler explicit solve ode idp okay so we have put down some parameters t 0 y 0 was what was given to us let's say that we want to solve it until time t equal to 5 and let us say the step size that we are going to use is 0.1 so i have put this down all of this over here initializing ok i have put a transpose over here because we want our t and y to be column vectors y is the solution vector and that we will initialize as zeros of size n plus one and y one which is the first value is equal to y0 keep in mind uh you would have realized by now that matlab doesn't have array location 0 the arrays start with index 1 and therefore we will use index 1 for time t equal to 0 okay in addition to this we also need to specify the value of n so n is going to be nothing but t and minus p 0 t 0 divided by h okay so let's save and run this to see that we don't get any errors okay so we are not getting any errors over here let's now continue solving using euler's explicit method for okay f i is going to be nothing but minus 2 multiplied by t i multiplied by y i that's our f i and y i plus 1 is y i plus h multiplied by f i so that's our euler's explicit method so let's go and look at this y i plus 1 is y i plus h multiplied by f computed at t i comma y i okay our f was minus 2 t y so f at time i is going to be nothing but minus 2 multiplied by uh the vector t the ith location multiplied by vector y the ith location okay that's going to be our f i y i plus 1 is nothing but y i plus h times f i okay and and this should do so that completes our euler's explicit method let's plot this plot t comma y so let's hope that this runs without an error you can run and this is the numerical solution using euler's explicit method let's also compute the errors so plot results and obtain errors y true equal to exp minus t square if i remember correct yeah exp of minus t squared minus t dot carrot 2 we need dot because it's element by element squaring and e r r equal to abs absolute value of y true minus y okay so that's what we have let's clear our screen and let's call euler's explicit here solves this and let us look at error err let's call max let's find the max value of err and the max error is 0.035 if h was reduced to 0.01 let us run this okay and see max err okay and the maximum error went from 0.03 to 0.003 so by reducing h by one order of magnitude the maximum error also reduced by one order of magnitude let's save this by reducing h further and we find max error and the max error fell by another order of magnitude we now instead of 3 into 10 to the power minus 3 we now have 3 into 10 to the power minus 4 so from here you would have probably guessed that the order of accuracy for the global truncation error rather for euler's explicit method is of the order of h to the power 1. this is something that we will cover in a little bit more details in one of the later lectures in this module okay okay so let us go back change our h to our old value and let us go back to our power point okay so this was the example that we solved using euler's explicit method now in euler's explicit method over f we calculated at t i comma y i we could have very well calculated f at t i plus 1 y i plus 1 instead of t i comma y i if we were to do that we will get what is known as euler's implicit method in the explicit method t i and y i were already known and you could directly calculate this and assign it to y i plus 1. however that is not possible in euler's implicit method because our y i itself depends on y sorry our y i plus 1 depends on itself through this function f so this is a nonlinear equation which we need to solve we can do it by using a non-linear solver such as epson f solve was something that we covered in module 5 of this course so if we were to rewrite this we will rewrite this as y i plus 1 minus h times f at t i plus 1 comma y i plus 1 minus y i equal to 0 and this we can assign it to f solve and we can solve this okay now euler's implicit method is not a part of this particular course however for the sake of completeness i have already created euler's implicit code and i will display that to you clear all close all clc edit euler's implicit okay so this is the code that i have created earlier for same values of y naught t naught t n and h everything else is remaining same okay now our f is calculated at t i plus 1 y i plus 1. so first i will calculate t i plus 1 okay y i plus 1 is yet unknown and therefore we need to use f solve in order to solve this okay so y i plus 1 is the unknown quantity so this is unknown this over here is unknown so we have used what is known as anonymous functions in matlab in order to give this particular f of x so that f is nothing but y minus h times my h multiplied by minus 2 multiplied by t multiplied by y minus y i so this is what is written over here okay and the initial guess that we are going to give is capital y i okay uh now anonymous functions is another thing that we have not covered in uh in this in this course we are not going to cover anonymous functions in matlab in this this particular course so these are two concepts which are actually beyond the syllabus if you will okay i've just shown it for the sake of completeness so that you can uh understand how we can do this let's also plot and obtain errors as before plot t comma y e r r equal to abs y y true minus minus y we also need y to y true is exp of minus t square and there is a carrot dot carrot sorry instead of a carrot okay so this is basically what we have done for oil is implicit let us run this and hope that we won't get any error okay one thing you will notice is that euler's implicit method takes a little bit longer than euler's explicit method in order to run that's because we need to solve fsolve at each time okay the figure looks approximately similar to what we had in euler's explicit method let us go and find out the max error max error is again of the order of 0.01 if you recall in euler's explicit method also with h equal to 0.1 the error v bar was 0.03 okay so the order of accuracy of euler's implicit method is similar to that of euler's explicit method so now the question is if the order of accuracy is similar between euler's implicit and explicit and implicit method is much more difficult to solve than euler's explicit method why are we interested in implicit methods in the first case the reason why implicit methods are very useful is because implicit methods are globally stable this was covered in the computational techniques course module 7 part 5 link for which is given over here basically what that means is if we start increasing the step size we will need to sometimes increase step size in order to speed up the overall computation if we start increasing the step size there will come a threshold value of h beyond which euler's method explicit method will become unstable which means that you will not get a stable solution euler's implicit method on the other hand is globally stable which means you can choose any value of edge of of your liking and euler's implicit method will remain stable the stability is the reason why implicit methods are popular especially for solving tough ode problems okay let's kind of let's now show what loss of stability means okay we'll just clear this and we'll go back to euler's explicit method so now if h instead of 0.1 let us say if we were to take 0.25 and we run this okay we get the solution now the solution qualitatively looks similar to that we saw earlier only thing is it looks more bumpy and that's because the errors are higher with step size of 0.25 while the errors are higher the solution is still stable now let us increase this to 0.5 let's run oil is explicit and we see that there's something funny that's happening there's nothing that's happened in the first first step first two steps rather in the third step the solution directly went from 1.0 to 0.0 okay and that is uh the reason why that happens is h equal to 0.5 is at the border of the stability limit if we increase our edge beyond 0.5 we will make the overall solution unstable so let us go and increase this to h equal to 1 and we will see what instability really means let's run this okay as you can see the value of ti sorry value of y i at t equal to 5 is greater than 100 and this particular series solution is diverging okay the reason is because euler's explicit method in this particular example has the limit of stability at h equal to 0.5 any value of h greater than 0.5 will result in unstable solution let us actually go and change h to 2.5 and see what happens okay and our solution has gone to minus twelve and if again same thing if we were to change it to five we will see a similar behavior let us change this to ten and see what the behavior is okay and you see we have gone the solution has reached something like minus 1 e to 10 to the power 4 so we have very clearly gone this to take in the solution to an unstable region let us compare that with euler's implicit we will use h equal to two point five and t and equal to ten to keep things the same as before h equal t n equal to 10 h equal to 2.5 let's save this okay and let's run this and see okay although the solution is very approximate we do not have a very good approximation using ode uh using the ode solver euler's method we still have the solution to be stable the reason is that euler's implicit method is globally stable so we can choose the size the step size age as large as we want and the solution will still remain stable okay so with that i will come to the end of lecture 7.1 what we covered in lecture 7.1 is introduce ourselves to ode initial value problems took up one example and solved it using euler's methods euler's method was the simplest method that we we could choose and we compared euler's implicit and euler's explicit method in the rest of this module we are going to work with more accurate explicit methods known as the rangakutta family of methods in the next lecture i am going to introduce the second order rangiputra method so that's our plan for remainder of this module see you in the next lecture thank you you
Info
Channel: MATLAB Programming for Numerical Computation
Views: 28,895
Rating: 4.7762237 out of 5
Keywords: Lecture, Introduction, and, Eulers, Method
Id: DN7bJVyAnDA
Channel Id: undefined
Length: 22min 28sec (1348 seconds)
Published: Sun Feb 21 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.