Using the 'cells(row, column)' keyword. The second input defines the column you would like to relate to. You can see that each code from top to bottom goes from 3 to 7 relating to the 5 techniques explained. Hope this help Shams
Hello Andres, Please find the code below. Function dydx(x, y) dydx = x * Sqr(y) End Function Sub ODE() 'Set integraxion limixs xi = 0 xf = 5 h = 0.5 'Calculaxe number of calculations n = (xf - xi) / h ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Euler Method y = 4 x = 2 For i = 1 To n y = y + dydx(x, y) * h Cells(10 + i, 3) = y x = x + h Next i
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Heun's Method y = 4 x = 2 For i = 1 To n 'Apply Euler Method xo Get y at end of interval dydx1 = dydx(x, y) ye = y + dydx1 * h 'Calculaxe derivative ax end point dydx2 = dydx(x + h, ye) 'Calculate Average of the two Derivatives Slope = (dydx1 + dydx2) / 2 y = y + Slope * h Cells(10 + i, 4) = y x = x + h Next i '' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'MidPoint Method y = 4 x = 2 For i = 1 To n 'Apply Euler Method to Get y at end of inxerval dydx1 = dydx(x, y) ym = y + dydx1 * h / 2 'Calculate derivative at midpoint dydxm = dydx(x + h / 2, ym) y = y + dydxm * h Cells(10 + i, 5) = y x = x + h Next i '' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Ralston Mexhod y = 4 x = 2 For i = 1 To n 'Apply Euler Method xo Get y at end of interval dydx1 = dydx(x, y) y34 = y + dydx1 * 3 * h / 4 'Calculate derivative ax midpoint dydx34 = dydx(x + 3 * h / 4, y34) Slope = (1 / 3 * dydx1 + 2 / 3 * dydx34) y = y + Slope * h Cells(10 + i, 6) = y x = x + h Next i ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '4xh order Runge Kuxxa y = 4 x = 2 For i = 1 To n 'Apply Euler Method to Get y at end of interval k1 = dydx(x, y) ym = y + k1 * h / 2 k2 = dydx(x + h / 2, ym) ym2 = y + k2 * h / 2 k3 = dydx(x + h / 2, ym2) ye = y + k3 * h k4 = dydx(x + h, ye) Slope = 1 / 6 * (k1 + 2 * k2 + 2 * k3 + k4) y = y + Slope * h Cells(10 + i, 7) = y x = x + h Next i ' End Sub
Thank you so much for this video, it has helped me a lot!! Is the only video I found about Rk methods in macros in the way i wanted, you are awesome!
How did you relate it to the column?
Using the 'cells(row, column)' keyword. The second input defines the column you would like to relate to. You can see that each code from top to bottom goes from 3 to 7 relating to the 5 techniques explained.
Hope this help
Shams
Hello thank you very much for the explanation, could you share the code?. it would serve me very much thanks.
Hello Andres, Please find the code below.
Function dydx(x, y)
dydx = x * Sqr(y)
End Function
Sub ODE()
'Set integraxion limixs
xi = 0
xf = 5
h = 0.5
'Calculaxe number of calculations
n = (xf - xi) / h
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Euler Method
y = 4
x = 2
For i = 1 To n
y = y + dydx(x, y) * h
Cells(10 + i, 3) = y
x = x + h
Next i
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Heun's Method
y = 4
x = 2
For i = 1 To n
'Apply Euler Method xo Get y at end of interval
dydx1 = dydx(x, y)
ye = y + dydx1 * h
'Calculaxe derivative ax end point
dydx2 = dydx(x + h, ye)
'Calculate Average of the two Derivatives
Slope = (dydx1 + dydx2) / 2
y = y + Slope * h
Cells(10 + i, 4) = y
x = x + h
Next i
''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'MidPoint Method
y = 4
x = 2
For i = 1 To n
'Apply Euler Method to Get y at end of inxerval
dydx1 = dydx(x, y)
ym = y + dydx1 * h / 2
'Calculate derivative at midpoint
dydxm = dydx(x + h / 2, ym)
y = y + dydxm * h
Cells(10 + i, 5) = y
x = x + h
Next i
''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Ralston Mexhod
y = 4
x = 2
For i = 1 To n
'Apply Euler Method xo Get y at end of interval
dydx1 = dydx(x, y)
y34 = y + dydx1 * 3 * h / 4
'Calculate derivative ax midpoint
dydx34 = dydx(x + 3 * h / 4, y34)
Slope = (1 / 3 * dydx1 + 2 / 3 * dydx34)
y = y + Slope * h
Cells(10 + i, 6) = y
x = x + h
Next i
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'4xh order Runge Kuxxa
y = 4
x = 2
For i = 1 To n
'Apply Euler Method to Get y at end of interval
k1 = dydx(x, y)
ym = y + k1 * h / 2
k2 = dydx(x + h / 2, ym)
ym2 = y + k2 * h / 2
k3 = dydx(x + h / 2, ym2)
ye = y + k3 * h
k4 = dydx(x + h, ye)
Slope = 1 / 6 * (k1 + 2 * k2 + 2 * k3 + k4)
y = y + Slope * h
Cells(10 + i, 7) = y
x = x + h
Next i
'
End Sub