1

I recently made a code to solve a RLC circuit problem, with a 2th order ODE with Runge-Kutta method, but the code I made doens't match the result with standart result ODE for example, with a 5ohm resistor (R), 10mF capacitor (C), 1H inductor (L), 5v with initial voltage [v(0)], 0A for the electric current [i(0)] and 0.05s for the time (t), with 100 iteration I got 1.5163v, and with the code I got 3.75v, i can't see what I'm doing wrong. Here are the ODE equations:

ODE equations

And here is the equation to find the initial value of v'(0)=w(0):

w(0) initial value

Now, the Runge-Kutta method:

Runge-Kutta 4th order

And finally the code:

for(int i=0;i<iteracao;i++, hX+=h){

        tensaofSec=tensaof;
        wTV=wf;
        wT=wf;

        //Runge-Kutta para a funcao v'(t) e w'(t)

        l1=h*funcaoV;
        k1=h*funcaoW;

        wT=(wf+(k1/2));
        wTV=(wf+(k1/2));
        tensaofSec=(tensaof+(l1/2));

        k2=h*funcaoW;
        l2=h*funcaoV;

        wT=(wf+(k2/2));
        wTV=(wf+(k2/2));
        tensaofSec=(tensaof+(l2/2));

        k3=h*funcaoW;
        l3=h*funcaoV;

        wT=(wf+k3);
        wTV=(wf+k3);
        tensaofSec=(tensaof+l3);

        k4=h*funcaoW;
        l4=h*funcaoV;

        wf=wf+((k1+(2*k2)+(2*k3)+k4)/6);

        tensaof=tensaof+((l1+(2*l2)+(2*l3)+l4)/6);
    }

The complete function for calculate the final voltage

Crimeiaman
  • 51
  • 7
  • 1
    What type do have `funcaoW`, `funcaoV` and `tensaof`? Your code assumes some macro-like qualities, I'm not sure that Java has such facilities. – Lutz Lehmann Sep 16 '18 at 16:12
  • funcaoW, funcaoV and tensaof are double values. – Crimeiaman Sep 16 '18 at 19:03
  • 1
    Then you are integrating a constant and should thus get linear functions as result. – Lutz Lehmann Sep 16 '18 at 19:18
  • @LutzL so, what should I do? – Crimeiaman Sep 16 '18 at 21:51
  • 1
    Make them function calls, possibly via lambda constructs. `funcaoV(wT)` and `funcaoW(tensaofSec,wT)`. Whatever did you intent the difference between `wT` and `wTV` to be? – Lutz Lehmann Sep 17 '18 at 05:38
  • @LutzL because `wTV` it's the value of W for the function `funcaoV` and `wT` it's the value of W for the function `funcaoW` – Crimeiaman Sep 17 '18 at 06:23
  • 1
    But as of yet, they are not functions, they are just doubles that are constant inside the loop. And the two w values are always the same, so why have the same number under a different name? – Lutz Lehmann Sep 17 '18 at 07:43
  • @LutzL I understand now, so I have do create functions called `public double functionW(double tensaoSec, double wT)` and `public double functionV(double wT)` ? – Crimeiaman Sep 17 '18 at 07:47
  • 1
    Yes. It would probably help to store the computation results in arrays so that you can plot the full solution and compare against the theoretically expected behavior. – Lutz Lehmann Sep 18 '18 at 14:10
  • Thaks @LutzL I write them with function, and only altered inside them, and worked. I wanted to plot a graph with the values of voltage, but I don't know how to import the array `tensaof`. – Crimeiaman Sep 18 '18 at 15:29

0 Answers0