I am trying to code a method, and for every step I have to call another method (which I coded as a separate Sub).
Sub implicit()
'Setting limits and stepsize
x0 = 0
xfinal = 100
h = 5
Dim y2 As Double
Dim y As Double
'Calculating number of intervals
n = (xfinal - x0) / h
'Setting initial value
y = 100000
'Loop for implicit method
For i = 1 To n
Call methodN(y)
y2 = y + h * (0.1 * y2 - 0.0000008 * y2 ^ 2)
Cells(2 + i, 3) = y2
y = y2
Next i
End Sub
'N method
Sub methodN(y)
y2 = y - (y * (1 - 0.1 * h + 0.0000008 * h * y) - y) / (1 - 0.1 * h + 0.0000016 * h * y)
End Sub
When I put Return y2 in the second sub as the last line I get a compiler error. I know I'm not calling the Sub properly and I'm not returning the y2 value.
In VBA a Sub is a non returning method. You need to use Function instead, and then can Return a value
In short, it should look like this:
Function methodNewton(y)
y2 = y - (y * (1 - 0.1 * h + 0.0000008 * h * y) - y) / (1 - 0.1 * h + 0.0000016 * h * y)
methodNewton = y2
End Function
or more simply:
Function methodNewton(y)
methodNewton = y - (y * (1 - 0.1 * h + 0.0000008 * h * y) - y) / (1 - 0.1 * h + 0.0000016 * h * y)
End Function
and then call it this way instead:
y2 = methodNewton(y)
but you are missing an h
variable, so maybe like this:
Function methodNewton(y, h)
methodNewton = y - (y * (1 - 0.1 * h + 0.0000008 * h * y) - y) / (1 - 0.1 * h + 0.0000016 * h * y)
End Function
and then call it like this
y2 = methodNewton(y, h)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.