Bonjour,
Je suis novice dans la programmation sous vba. Je souhaite résoudre une équation différentielle non linéaire à l'aide de la méthode de Runge-Kutta. J'ai programmé pour cela une macro sous vba qui calcule un résultat. Mais malheureusement, lors de l'incrementation de la variable t de 0.05 il y a une erreur qui apparaît. Finalement, t à partir de la valeur 3.65 n'est plus exacte. Cela me pose problème par la suite et je voudrai savoir si vous sauriez comment corriger ce problème. Je vous mets mon code en dessous :
Code:Public it_MAX As Double Sub calcul() ' 'Definition of inlet variables : w0 = Sheets("note").Cells(104, 6).Value Dt = Sheets("note").Cells(105, 6).Value i = Sheets("note").Cells(106, 6).Value a1 = Sheets("note").Cells(107, 6).Value a2 = Sheets("note").Cells(108, 6).Value a3 = Sheets("note").Cells(109, 6).Value b = Sheets("note").Cells(110, 6).Value C = Sheets("note").Cells(111, 6).Value it_MAX = Sheets("note").Cells(112, 6).Value it_FIN = 3 + it_MAX 'Definition of the number PI Dim Pi As Double Pi = 4 * Atn(1) 'Calculation of different variables : a = a1 + a2 + a3 win = w0 * 2 * Pi / 60 wn = win t = 0 nb = 0 Sheets("calcul").Cells(2, 1).Value = t Sheets("temp").Cells(2, 1).Value = t Sheets("calcul").Cells(2, 2).Value = win Sheets("calcul").Cells(2, 7).Value = w0 Sheets("temp").Cells(2, 7).Value = w0 For n = 0 To it_MAX t = t + Dt 'In order to have more values on the picture at the begining, and less then, I have defined 2 divisions Dim q As Integer ' For the first meshing I have taken 30 s and 120 s for the second q = n / (30 / Dt) r = n - ((30 / Dt) * q) Dim q1 As Integer q1 = n / (120 / Dt) r1 = n - ((120 / Dt) * q1) 'Definition of the iteration : k1 = (Dt / i) * (a * wn ^ (0.5) + b * wn + C * wn ^ (2)) k2 = (Dt / i) * (a * (wn + 1 / 2 * Dt * k1) ^ (0.5) + b * (wn + 1 / 2 * Dt * k1) + C * (wn + 1 / 2 * Dt * k1) ^ (2)) k3 = (Dt / i) * (a * (wn + 1 / 2 * Dt * k2) ^ (0.5) + b * (wn + 1 / 2 * Dt * k2) + C * (wn + 1 / 2 * Dt * k2) ^ (2)) k4 = (Dt / i) * (a * (wn + Dt * k3) ^ (0.5) + b * (wn + Dt * k3) + C * (wn + Dt * k3) ^ (2)) wn = wn + 1 / 6 * k1 + 1 / 3 * k2 + 1 / 3 * k3 + 1 / 6 * k4 wtr = 60 * wn / (2 * Pi) 'Affectation of the results in the file Sheets("calcul").Cells(3 + n, 1).Value = t Sheets("calcul").Cells(3 + n, 2).Value = wn Sheets("calcul").Cells(3 + n - 1, 3).Value = k1 Sheets("calcul").Cells(3 + n - 1, 4).Value = k2 Sheets("calcul").Cells(3 + n - 1, 5).Value = k3 Sheets("calcul").Cells(3 + n - 1, 6).Value = k4 Sheets("calcul").Cells(3 + n, 7).Value = wtr 'Meshing of the values used for drawing the picture If t < 301 Then If r = -1 Then nb = nb + 1 Sheets("temp").Cells(2 + nb, 1).Value = t Sheets("temp").Cells(2 + nb, 7).Value = wtr End If Else If r1 = -1 Then nb = nb + 1 Sheets("temp").Cells(2 + nb, 1).Value = t Sheets("temp").Cells(2 + nb, 7).Value = wtr End If End If Next n 'Definition of the data source of the picture and creation of the picture Sheets("Graph").Select ActiveChart.SeriesCollection(1).Select ActiveChart.SeriesCollection(1).Name = "=""w(tr/min)""" ActiveChart.SeriesCollection(1).XValues = "=temp!$A$2:$A$" & (nb + 2) ActiveChart.SeriesCollection(1).Values = "=temp!$G$2:$G$" & (nb + 2) ' End Sub
Je vous remercie par avance pour votre aide,
Alex1818
-----