 So, in today's class what we will do is we will look at a one sided derivative for the first derivative representation right and then we will look at again we will go back look at the second derivative and if we have time we will go on to applying it to a set of simple problems because by the time we have finished here we will know how to represent functions, we know how to represent derivatives. So, we might as well try out a few simple problems to see if we are able to solve differential equations that are of interest to us okay fine, is that okay? Right, so if we want a second order meaning truncation error is a second order first derivative this is what we want right. The last class as I said I have already done a higher order let us look at for a look for the second order. So, if I have the points i, i plus 1, i plus 2 these are located at xi, xi plus 1, xi plus 2 the function values there are fi, fi plus 1, fi plus 2 and right now first what we will do is we will assume that h is like h in fact equals xi plus 1 minus xi which equals xi minus xi minus 1. We will assume that they are equal intervals right now okay later we will do unequal intervals because unequal intervals are of interest to us though except here you will see it here in this class and then you will never see me use unequal intervals again. So, this class is important in that sense unequal intervals are important but for this course I am not going to do unequal intervals outside this class okay is that fine okay. So, it is a simple game now. So, now we are going to do it right clean from the start simple game now we want to find the derivative at i and I am going to use the points i plus 1 and i plus 2 we will use Taylor series. So, f at i plus 1 is f at i plus h times f prime at i the prime indicates differentiation with respect to x plus h squared by 2 factorial f double prime at i plus h cube by 3 factorial f triple prime at i plus h to the fourth by 4 factorial f fourth derivative at i and then so on okay. In a similar fashion f i plus 2 is f i minus h times f prime at i I am sorry plus 2 h times f prime at i you can see in my mind I did an i minus 1 okay you have to be very careful okay fine plus 4 h squared by 2 factorial f double prime at i plus 8 h cubed by 3 factorial f triple prime at i the reason why I do not succumb to the temptation to expand this out and cancel and so on is because I know I am going to combine these 2 terms right. So, I leave whatever is common between them I leave them as they are okay. There is a temptation when you are writing this to cancel terms and so on do not do it plus 16 h to the fourth by 4 factorial the fourth derivative and so on. So, we look at these 2 our objective is to extract out the f prime at i. So, what do I need to do? If I multiply the first equation by 4 I will get a 4 h squared here and I will get a 4 h squared here. So, if I multiply the first equation by 4 and subtract the second equation I will knock out the h squared term. So, 4 f i plus 1 minus f i plus 2 is 4 f i minus f i which is 3 f i 4 h f prime right minus 2 that is plus 2 h f prime at i this will cancel that is the whole point of multiplying by 4 and this gives me minus 4 h cubed by 3 factorial the third derivative minus 12 h cubed by 4 factorial fourth derivative did I make a mistake h power 4 by and so on okay. So, it is very clear it is very easy to make mistakes you have to be really careful when you are doing this fine. So, let us solve for f prime at i. So, if we come here and you solve for f prime at i that derivative is 4 f i plus 1 minus 3 f i minus f i plus 2 divided by 2 h and what does that leave me 2 h squared by 3 factorial f double prime plus higher order terms f triple prime plus higher order terms. So, if I throw this out I say this is the representation for the derivative I throw this out right. So, this is the truncation error. Truncation error is of the order of h squared. So, it is 1 by 3 h squared f triple prime at i that fine truncation error is of that order and again like I did last time I make a quick check I had 4 minus 3 plus minus 1 that is 0 why do I say that you ask yourself the question why do I test even last time I tested by adding up all the coefficients to make sure they added up to 0 why do I do that constant function it should work yes constant function it should work for a constant function it should give me a should give me 0 right constant function it should give me 0. And of course in the limiting process in the limiting process the numerator is supposed to go to numerator supposed to go to f at i if I actually take the limit remember this is a finite difference if I go back to the infinite process which means I take the limit h going to 0 right the numerator supposed to independently go to 0 and then denominator goes independently to 0 it is a ratio that gives me the derivative you understand right so from how should I put it from operational point of view yes I have a derivative I have a representation for the derivative and the constant function should give me a 0 derivative right that is from an operational point of view the other process is of course I have a infinite process for which I have replaced by a finite process I stop I do not divide by I do not take the limit h going to 0 but if I do go take h going to 0 this should go to the derivative it has to converge to the derivative right we wanted to converge to the derivative is that clear okay so the summation so that is one sanity check that you can always do add to make sure that they all add up to 0 okay this is fine now what if they were unequal intervals what if they were unequal intervals so we have these weights we have seen this is very nice we have the truncation error but what if the intervals were unequal intervals what if the intervals were unequal intervals and just for the fun of it just for the fun of it we can do you can we can still do a comparison but just for the fun of it we will do i i-1 and i-2 okay and in this case this length happens to be h and to keep our algebra simple I will call this some alpha times h is that fine just to keep our algebra simple and this kind of a relationship you may actually get future applications so I will leave this as alpha times h and we will just quickly repeat this process okay we will just quickly repeat this process so what do we have f at i-1 is f at i-h times f prime at i plus h squared by 2 factorial second derivative at i-h cubed by 3 factorial f triple prime at i plus h to the 4th by 4 factorial the 4th derivative at i and so on f at i-2 is f at i-1 plus alpha 1 plus alpha times h f prime at i plus 1 plus alpha squared 1 plus alpha whole squared h squared by 2 factorial f the second derivative at i so it is very clear it is not it is not that bad plus so on right 1 plus alpha 4 factorial h to the 4th and that will be multiplying the 4th derivative and you have all the other terms what do I do now so if I multiply the first equation by 1 plus alpha squared I should be able to cancel out the second derivative term okay so 1 plus alpha squared f i-1 1 plus alpha squared f i-1 minus I am going to subtract out this term f i-2 equals so you should be happy if alpha over 1 1 plus alpha squared is 2 squared which is 4 which is what we did earlier right so that is not bad f i-2 equals we have 1 plus alpha squared oh I keep doing that minus 1 f i what happens to this term so you get 1 plus alpha squared minus 1 plus alpha h f prime and what else no by 2 no by 2 that is the first derivative then what is the third term third term goes away so and then you get minus 1 plus alpha squared minus 1 plus alpha cubed h cube by 3 factorial f triple prime and I would not bother with the fourth term right the fourth term something that you can work out because normally when I do it when I am sitting alone I sort of simplify this a little faster than this right so but it is fine we will let us see what we get here so you have 1 plus alpha squared f i-1-f i-2 equals alpha into 2 plus alpha is that fine and if alpha equals 1 that gives you 3 so we are happy it is working out so that is f i okay and what is the next term I can factor out the I can factor out 1 plus alpha so that gives me a minus 1 plus alpha into alpha h f prime at i in the alpha equals 1 that is 2 okay that also works plus or minus 1 plus alpha squared into 1-1-alpha h cubed by 3 factorial f triple prime i and so on okay actually I made a mistake here there is a 3 factorial at that point is that okay so what does it give me for f prime at i 1 plus alpha squared-alpha 2 plus alpha f i f i-1 f i-f i-2 divided by alpha into 1 plus alpha do you have a sign problem there should be a negative sign here okay and this what is the truncation error now minus alpha into 1 plus alpha squared h squared by 3 factorial and I have to divide by alpha times 1 plus alpha so it gives you on 1 plus alpha f triple prime at i it has to be a plus no it is right there is a negative sign here there is a negative sign here there is a negative sign here there is a negative sign here and there is one more negative sign it will turn out right see I have another way I have another way by which I checked if you notice I glanced at the other board what was the truncation error for the forward difference that was positive so this will be negative right there are some simple sanity checks that you can do fine okay so and you can see if you set alpha equals alpha equals 1 whether it works out so clearly if they are unequal clearly if they are unequal it is going to become very messy yes. No no it is the same thing you have three points they are unequal they are unequal distances but I can still eliminate I can still eliminate the second derivative term and get an expression for the first derivative leaving a truncation error which has only a third derivative term you want to calculate the derivative at the next point and that could be it could be you know some beta h or something of that that could be something else all together you would still have you would still have unequal intervals you could still have unequal no no it need not always be alpha there is a there is a process for geometric stretching where it is always alpha right it is a constant that is a nice situation but if it is not it need not always be the same okay so the point next to it could be some ratio is that fine right it need not be h by alpha something of that sort it need not be that way okay so the idea is that the adjacent intervals are not of equal size fine so and as you can see from the fact that the expression is so messy it is possible for us to calculate it but clearly from a thank you clearly from a classroom point of view right it does not it does not add anything I just want you to have an awareness I will do one more I will do one more in this class with unequal intervals as I said but it does not add anything to my class as such right so if you take uneven intervals which you very often will be forced for other reasons right to take uneven intervals when you are forced to take uneven intervals you will get messy expressions and this error seems to be slightly large I mean if alpha is depending on the value of alpha this the magnitude of this error term is going to change okay so you may you should have a reason so you will be changing these h values because you have maybe possibly some knowledge on how the function is varying right and then that is the reason why you are trying to compensate if you know that the triple prime is large then you may be changing your alpha in order to compensate for that it is possible okay there are many reasons you will see that when you run into it at a later date in a more advanced course you will see why you would change take unequal intervals in this class as I said I will do it one more time but the reason why I will not persist I will always assume equivalent rules is because it just makes life easier and the derivations are always possible whatever I do with equivalent rules you can repeat as I have indicated with unequal intervals is that fine okay. So let us look at the second derivative right so far we have looked at the first derivative you have a first order forward difference representation backward difference representation second order forward difference backward difference right you have one third order representation that I did in the last class so you can work out other higher order I would suggest I would strongly recommend that you work out other higher order representations for the first derivative okay for the first derivative. Now let us look at the second derivative again I will repeat the second derivative I will quickly repeat the second derivative right that we did last time so it is second order second derivative just because I am going to do it I am going to do it twice one which we did last class and then I am going to repeat it again with unequal intervals so like I did I have xi xi-1 xi-1 xi-1 xi-1 of course you can also see whether you can get one sided derivatives right for the second derivative one sided representations I leave that to you so what we have is simple expansion again using Taylor series about the point i fi plus h times f prime at i and again h equals xi plus 1-xi equals xi-xi-1 okay this case they are equal plus h squared by 2 factorial second derivative of i plus h cube by 3 factorial third derivative i plus h to the 4th by 4 factorial and so on please remember so we are using Taylor series to expand about the point i xi okay you could as well use Taylor series to expand about any other point expand about the point i because we want the representation at the point i right we want the representation of the derivative at point i therefore we choose i you want the representation elsewhere then you have to expand about that point is that fine that okay so f at i-1 is f at i-h times f prime at i plus h squared by 2 factorial f double prime at i-h cube by 3 factorial so this is sort of a theory process unfortunately you have to get it right okay this it does not work the details very important so if I just add the two this cancels that cancels so it gives me for my first derivative an expression we derived in the last class the second I am sorry the second derivative an expression we derived in the last class that is f double prime at i is f i plus 1-2 f i plus f i-1 divided by h squared-h squared by there are two of them okay this is a truncation error we have seen this we have seen this so the question is what happens if we take unequal intervals okay we look at that and then I sort of we will close the subject of what happens with unequal intervals so what happens if you take unequal intervals for the representation of the second derivative you have seen what happens for the first derivative first derivative we still got a second order representation just that the coefficient of that truncation term can change we will see what happens here so instead of instead of h right instead of h so okay we will just write it again so we have i-1 i i plus 1 this is h this is alpha h okay just to keep it clean we will just redo it quickly so f i plus 1 is f i plus h times the first derivative plus h squared-2 second derivative which is what we want plus h cube by 3 factorial the third derivative plus h to the 4th by 4 factorial 4th derivative and so on okay normally if you did not know how many i would you take a lot of terms depending on what is it how what order you are going to take right you cannot stop at 4th derivative if you want a much higher order I stop at 4th derivative because I know I am going to get something that is of the order of second order f i-1 is f i plus minus alpha h f prime at i plus alpha squared h by 2 f double prime at i-alpha cubed h by 3 factorial f triple prime at i plus alpha to the 4th this is not as bad as the first derivative term right h to the 4th 4 factorial 4th derivative right and so on is that fine h squared h cubed being a little careless today so if I multiply by the now I am a little desperate I want to get rid of that I have to get rid of that first derivative that is my need I want the second derivative so I multiply the first equation by alpha to get rid of that okay so I get alpha f i plus 1 and to that I add the second equation f i-1 equals 1 plus alpha times f i I get an alpha f i plus 1 f i the second term cancels what happens here plus alpha 1 plus alpha into 1 plus alpha h squared by 2 f double prime at i that is the term that I want plus alpha into what happens to the last one now this looks very different this looks very different so if I do if I if I try to evaluate it may be write it here pardon me no I think you can vary if you can check you can check it out okay I get it out of the way we will stick with this we will stick with this material what we are doing right now so f double prime at i is alpha f i plus 1-1 plus alpha f i plus f i-1 divided by alpha into 1 plus alpha h squared to 2 right so if alpha equals 1 that 2 and this 1 plus alpha will cancel that is the idea okay and alpha equals 1 this will be 1 that is 2 that is 1 it goes back to the standard expression what about the truncation error so you have a you have a third derivative term right so you have a minus this is a truncation error so I have divided by alpha 1 plus alpha so it gives me a 1-alpha h by 3 and this gives me a minus 1-alpha plus alpha squared a little algebraic fourth derivative at i and there is an h squared here and this is the difference so for the second derivative term if you take unequal grids the convergence rate drops representation of the derivatives only became the truncation error became first or the convergence is linear it is not quadratic it goes to 0 as h not as h squared this is very important to remember and it introduces a third derivative term this will have significance later on later in the semester I will remind you about this okay later in the semester I will remind you about this it adds so if you have unequal grids if you have unequal grids it introduces a third derivative term is that fine okay and it introduces a convergence which is first order and not something that is second order of course if alpha equals 1 this goes away right and we retrieve what we had for equal intervals is that fine okay so as I said it is always possible for us with unequal intervals to calculate derivatives it is always possible so when you encounter it you know how to do it but we are not going to do it anymore because it is just a mess and it does not add anything else okay but I do want you to remember this very important that if you go for unequal intervals you cannot be sure that the order that you get is the same order you have to recalculate you have to rework okay right now let us supply this now that we have all these derivatives that we can represent and functions that we can represent let us apply these let us apply what we have got right to differential equation some differential equation we will choose a simple one right now we will use we look at Laplace's equation we look at Laplace's equation is that fine okay so we look at Laplace's equation in two dimensions I am going to stick to two dimensions here so this symbol is called nabla right we are used to calling it del but it is called nabla so this would be nabla squared phi 0 and we could choose we could choose to solve this problem in a very simple region we can choose a region that is in the first quadrant may be a unit square and in this unit square you have this equation that is valid and on the boundary possibly we provide boundary conditions so you know of many ways by which you can solve this equation possibly in PDE you have studied variables separable and so on right there are many ways by which you can solve it here we look at see whether we can represent the derivatives on a mesh and whether we can use that to infer the solution okay so to obtain that solution so we know that in two dimensions this would be dou squared phi dou x squared plus dou squared phi dou y squared equal to 0 I will use an alternative notation I may switch between them depending on convenience so if there is no confusion you may occasionally see me write that it is much more compact notation okay both of them represent Laplace's equation in two dimensions and this is an expansion of that nabla squared so what is the typical mesh point that I am going to take just like we said i i plus 1 i minus 1 how are we going to do it in two dimensions in two dimensions in Cartesian coordinates I am going to take equal intervals right so this would have now two indices one for going traversing along in the x direction and one for traversing along in the y direction so that would be i plus 1 j that would be i minus 1 j that would be ij minus 1 and that would be ij plus 1 I will take all of these intervals for the sake of convenience to be h I think you can figure out what will happen if you change the values you could there are so many different things that could happen it could be h here and it could be some different value in the y direction each one of them can be different there are so many possibilities you can work it out. So what is the first derivative what is the first term at the point ij using the expression that we have just derived it is phi i plus 1 j minus 2 phi ij plus phi i minus 1 j divided by h squared okay so at the nodal points if I have the function phi given by i plus 1 j ij i minus 1 j phi at the appropriate points I can estimate the derivative okay and we have a truncation error associated with it I say equals at this point but we know already that I say equals but we know it is an approximation it is a representation right but I will write equals ij in a similar fashion is phi ij plus 1 minus 2 phi ij plus phi ij minus 1 divided by h squared clearly if there were delta x and delta y it would be delta x squared and delta y squared okay that is not a big deal fine. So now to represent Laplace equation at that point so now I am talking about represent so far we are talking about derivatives now I am going to actually represent the equation I will add these two okay I will add the two and I can actually represent Laplace equation xx plus phi yy at the point ij gives me phi ij phi i plus 1 j minus 2 phi ij plus phi i minus 1 j by h squared plus phi ij plus 1 not a big deal minus 2 phi ij plus phi ij minus 1 by h squared and this supposed to be 0 this at that point supposed to be 0 at that point okay. So now I say this represents Laplace equation at that point at the point ij and it is actually possible for us it is actually possible for us to solve for phi at ij after all that is really what we want we want the phi at ij in fact turns out so if you multiply through by h squared the right hand side is 0 so the h squared will go away okay this turns out to be phi i plus 1 j plus phi i minus 1 j plus phi ij plus 1 plus phi ij minus 1 by 4 right by 4 I write you could write by 4 just to encourage you to multiply instead of divide I will write times 0.25 so now I change to a programming notation multiplied by 0.25 okay fine but the by 4 is important it is the average of the neighbors that is the key so solution to this equation is the average of solution to this equation at this point the value at this point is the average of the values at the neighboring point okay is that fine okay. So now we have some mechanism by which we can take represent Laplace equation at a given point we will see what we can do with this yeah you have a question how well in this case it does not matter because we are taking derivatives only with x and y but you can actually write Taylor series in two dimensions there is a there is a Taylor series expansion for two dimensions so if you say you may have seen it in multivariate calculus you can actually expand about point f of x y right and then it will turn out to be I am just I am just writing this f x f y where f x is derivative with respect to x f y is derivative with this and you can go on so you will get an you will get h squared f x x g squared f y y to h g f x y you understand and you can add you can keep on adding terms right so there is a from your multivariate calculus you go back you will look you will see that you will actually pick up okay. So and you would need that you would need that if these grids were not orthogonal to each other that is if you had grids that were not in not only unequal but they are not along the coordinate lines they are not along the coordinate lines so not only are they they could be equal or unequal but they are not along coordinate lines right and then you can run into this problem that you would have to do it in you would have to do it this way but trust me there are there are better ways by which one can do this right so we will see we will see may I do not know whether you need to you need to look up a little tensor calculus I usually do something on tensor calculus we will see whether we get there or not at least I will give you a motivation for why you should learn tensor calculus as we go along okay let us get back here what do we have so this is the problem that we want to solve we will make up a problem and we will see how we would go about solving using what we have just derived. So what we do is again to keep life easy we will break this up we will break up our problem domain using a mesh and you will notice that in this case of course there are 1, 2, 3, 4, 5 at the 3 interior points right so this would be so that would be some point i, j that I was talking about okay and how would the problem be normally prescribed how would you normally prescribe the problem so just say this is a unit square so this is a point 0, 0 which is the origin this is a point 1, 1 right so unit square located at the origin unit square located at the origin and what we want to do is we want to see whether we can solve Laplace's equation. So given some boundary condition we will figure out how to get those boundary conditions given some boundary conditions if I prescribe some boundary conditions which means that the values at these points are known so if I prescribe some boundary conditions the values at these points are known and if we are able to find the values at the interior points right then we have phi i, j's at the interior points and using possibly hat functions or something of that sort we may be able to interpolate right actually we would use hat functions in 2D but I have not defined hat functions in 2D but you can use hat functions and actually use linear interpolates to find the value anywhere in between okay is that fine. So what you can do now is that any given point i, j you just repeat this so phi i, j is phi i plus 1, j phi i minus 1, j phi i, j plus 1 phi i, j minus 1 I am going to say divided by 4 here but you know you have to multiply by 0.25 you just repeat this process for every interior node the conditions on the boundary are given okay so when you are trying to figure out what is the value at that node this is known that is known right let us number these so let us say this point is 0, 0 this is 1, 0 2, 0 3, 0 4, 0 okay so this is 0, 1 and this would be 1, 1 get rid of that 2, 1 and so on and this point is of interest 0, 2 1, 2. So if you want to find the value at 1, 1 you need the values at 1, 2, 2, 1, 0, 1, 1, 0 from the boundary conditions you have the values at 1, 0 and 0, 1 okay we do not have the values at 1, 2 and 2, 1 so we not only do not have the value at 1, 1 we do not have the values anywhere in between. So in order to get this working the process that we will start doing is we will make an assumption about the interior values just make an assumption let us start with the guess right a good guess would be 0 we have nothing to go with right now right there are many possible guesses right now I have not even told you what the boundary conditions are so good guess is 0. So let us just assume that all the interior values are 0 and you are given boundary conditions okay so you can find phi ij at any given point by taking the values of the neighboring point then you can go to the adjacent point take the values of the neighboring point you understand and you can do this for all the interior points you do not touch the boundary points because the boundary points are known already. So if you keep repeating this process for all the interior points as time progresses we hope that the phi ij is evolved that you are generating a sequence of phi ij so this is you are generating a sequence of phi ij right n equals 1, n equals 2, n equals 3, n equals 4 that you generate a sequence of phi ij and you hope that sequence converges right now we hope that sequence converges and if that sequence converges you will have a set of phi values at every grid point okay is that fine okay. So it is better if you just try it out and work a problem and I think then it will work out if you go through that sequence and it has converged how do you know the answer that you have got is the right answer well you have points at these values you have grid points so when you say something as a solution to differential equation what do you do you substitute into the differential equation and verify whether it is true or not right so you could substitute into the original differential equation phi xx plus phi yy equals 0 but in this case you just have points at various nodes okay right and if you substitute in the difference equation you can find out what is it that the difference equation gives you okay but the differential equation itself requires that you take derivatives and you have only discrete nodes okay is that fine right. So I think what you do is you can try this out just give some arbitrary boundary conditions you will work out may be a more specific problem in the next class just try to give it some arbitrary boundary conditions and see what it works what happens right yeah. Sir we have 9 unknown grid points right this what I have drawn you have 9 unknowns right. We have 9 conditions right so we can solve the linear set of equations yeah there are issues so that is we will get to that right so we will see what it is that we are doing that is actually what we are doing right we will actually figure out what is it that we are exactly what exactly are we doing okay right so we will get back to this one day thank you.