Group Abstract Group Abstract

Message Boards Message Boards

0
|
7.7K Views
|
28 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Runge Kutta method for a system

Posted 5 years ago

I want to solve x'=y,y'=6x-y+G,x(0)=1,y(0)=1 with Runge-Kutta Method. Here G lies in [0,1]. I want to get the plot of G versus x. is that possible? May any one help me with that? Here is my code.

ClearAll["Global`*"];    
a = 0; b = 1;
x[0] = 1; y[0] = 1;
h = 0.1; n = (b - a)/h;
Do[t[i] = 0.0 + (i)*h, {i, 0, n}]
Do[G[j] = 0.0 + (j)*h, {j, 0, n}]
f[t_, G_,x_, y_] = y; g[t_,G_, x_, y_] = 6*x - y+G;
Do[S1 = f[t[i], G[i], x[i], y[i]]; P1 = g[t[i], G[i], x[i], y[i]]; 
S2 = f[t[i] + h, G[i] + h, x[i] + h*S1, y[i] + h*P1]; 
P2 = g[t[i] + h, G[i] + h, x[i] + h*S1, y[i] + h*P1]; 
S3 = f[t[i] + h/2, G[i] + h/2, x[i] + h*S2/2, y[i] + h*P2/2]; 
P3 = g[t[i] + h/2, G[i] + h/2, x[i] + h*S2/2, y[i] + h*P2/2];
S4 = f[t[i] + h, G[i] + h, x[i] + h*S3, y[i] + h*P3];
P4 = g[t[i] + h, G[i] + h, x[i] + h*S3, y[i] + h*P3];
x[i + 1] = x[i] + (S1 + 2 S2 + 2 S3 + S4)*h/6; 
y[i + 1] = y[i] + (P1 + 2 P2 + 2 P3 + P4)*h/6, {i, 0, k}]
Table[{G[j], x[j]}, {j, 0, k}];
POSTED BY: Vedat Erturk
28 Replies
Posted 5 years ago

The code works fine for me

tt = Table[{g, qQnum[g]}, {g, 0, Ts}]
(*
{{0, 0.303553}, {1, 0.299951}, {2, 0.296613}, {3, 0.293514},
 {4, 0.290635}, {5, 0.287959}, {6, 0.285475}, {7, 0.283173},
 {8, 0.281044}, {9, 0.279083}, {10, 0.277284}, {11, 0.275644},
 {12, 0.27416}}
*)

ListLinePlot[tt]

enter image description here

POSTED BY: Rohit Namjoshi

Hi Vedat,

here is a code which gives you some values of Qs and Qc, which of course depend on g. Please have a look about typos and if the results are reasonable.

I still don't know what you finally want to do, x[t] is a function and not a value, but I admit I didn't read the paper, only the part around equation ( 4.1 )

eq11a = x'[t] == y[t];
eq11b = y'[t] == 
    lam (x - lam x^3 + lam^2 x^5) (x'[t])^2 - 
     alf gam (1 + lam x^2) x'[t] - w0^2 x - del x^3 - xi x^5 + 
     gam (1 + lam x^2) (f Cos[w t] + g Cos[ww t]) /. {x'[t] -> y[t], 
    x -> x[t]};

val = {alf -> .2, w0 -> I, ww -> 9.842, w -> .5, f -> .05, lam -> 0, 
   beta -> 1, m0 -> 1.5, gam -> 1/m0, del -> beta gam - lam w0^2, 
   xi -> beta gam lam +( lam  w0)^2};

eq11b2 = eq11b //. val;

sol[gg_?NumericQ, t1_?NumericQ] := 
 Module[{}, eq11b3 = eq11b2 /. g -> gg;
  NDSolve[{eq11a, eq11b3, x[0] == 1, y[0] == 1}, {x, y}, {t, 0, t1}]]

nT = 10;
lsg = sol[.3, nT];

fx = x /. lsg[[1, 1]]
fy = y /. lsg[[1, 2]]

qQs = NIntegrate[fx[t] (Sin[w t] /. val), {t, 0, nT}] 2/nT
qQc = NIntegrate[fx[t] (Cos[w t] /. val), {t, 0, nT}] 2/nT
POSTED BY: Hans Dolhaine

Hello Vedat, I am afraid I do not understand what you mean. What do you mean by "one gets the value of x[t] versus G" ? x[t] is a function and not a value! x[ .85 ] for example is a value, and this value certainely depends on G. But for a given t x[ t ] is a linear function of G (look for example at fx[.1, g] ), so you get a straight line. The same is true for y, I attach some code which shows how the functions x[t] and y[t] ( and not a value ) change with G (in my code g)

eq1 = x'[t] == y[t];
eq2 = y'[t] == 6 x[t] - y[t] + g;
sol = DSolve[{eq1, eq2, x[0] == 0, y[0] == 1}, {x, y}, t];
fx[t_, g_] := Evaluate[(x /. sol[[1, 1]])[[2]]]
fy[t_, g_] := Evaluate[(y /. sol[[1, 2]])[[2]]]
(* Show x[t] depending on g  *)
Manipulate[
 Plot[fx[tt, g], {tt, 0, 2}, PlotRange -> {0, 6}],
 {g, 0, 1}]
(* Show both x[t] (red) y[t] (blue)  depending on g  *)
Manipulate[
 Plot[{fx[t, g], fy[t, g]}, {t, 0, 2},
  PlotRange -> {0, 6},
  PlotStyle -> {Red, Blue}],
 {g, 0, 1}]
(* Show the vector { x[t], y[t] } depending on g  *)
Manipulate[
 ParametricPlot[{fx[t, g], fy[t, g]}, {t, 0, 2},
  PlotRange -> {{0, 2}, {0, 4}}],
 {g, 0, 1}]
POSTED BY: Hans Dolhaine

What do you mean by "plot of G versus x"? Isn't G constant?

And what about this?

eq1 = x'[t] == y[t];
eq2 = y'[t] == 6 x[t] - y[t] + g;
DSolve[{eq1, eq2, x[0] == 0, y[0] == 1}, {x, y}, t]
POSTED BY: Hans Dolhaine

Horner seems to be about 10% faster (at least on my machine). And you should change all variables with super and/or subscripts or both.

cc = RandomReal[{1, 10}, 6]
p1 = Sum[cc[[i]] x^(i - 1), {i, 1, Length[cc]}]
p2 = HornerForm[p1]


Timing[
 Do[
  e1 = p1 /. x -> .245,
  {i, 100000}];
 e1]

Out[47]= {0.858, 5.16398}


Timing[
 Do[
  e2 = p2 /. x -> .245,
  {i, 100000}];
 e2]

Out[48]= {0.749, 5.16398}
POSTED BY: Hans Dolhaine
Posted 5 years ago

Dear Hans, Thank you for reply. I will remove capital letters.I have not used Horner's scheme for power-sums before.could you please give a simple example? Moreover I am sure my Do[ For [.... constructs what I really want.

POSTED BY: Vedat Erturk

Hello Vedat,

a pdf is not really better than a foto. No chance to run or edit it. And I am afraid I really can't speed up your code. For me it is too complicated in fact.

Some general remarks: you should avoid variables starting with a capital letter. Such a variable could be an internal symbol. You don't get an error, but results are not what you expect. So: no captial letters. You should avoid variables with super- and/or subscripts, or both. As far as I remember it occurred sometimes that they were not handled in the way you want to.

You should analyze your sums. Are there constants common to some sums and could they be calculated once and outside the sum? I noticed there are power-sums: rewrite them according to Horner's scheme.

And are you sure your

Do[ For [....

constructs is really what you want?

Greetings, Hans

POSTED BY: Hans Dolhaine
Posted 5 years ago

Hello Hans, I hope you are fine and can speed it up my attached code. For large K's and the small values of T/M, my code runs slowly. Please do not hesitate to ask me anything.

Attachments:
POSTED BY: Vedat Erturk
Posted 5 years ago

Hello Hans, Possibly there is some mistyping errors. So, thank you for your help. Vedat. Take care.

POSTED BY: Vedat Erturk
Posted 5 years ago

Dear Hans and Rohit, it should be qnum=Sqrt[qQs^2 + qQc^2]/f instead qnum = Sqrt[qQs^2 + qQc^2] to the paper. Thanks.

POSTED BY: Vedat Erturk

@ Rohit

Hello Rohit, thank you for your answer. " I believe you are running V8? " It is even worse, I am very old fashioned and only have V 7. So this is my situation: NDSolve gave solutions for x[t] and y[t], but NIntegrate gave some errors, and so it is very likely that my qQnum is wrong. And I couldn't reproduce any of the pics in the paper, what seems to be what Vedat wants to do.

POSTED BY: Hans Dolhaine
Posted 5 years ago
POSTED BY: Rohit Namjoshi
Posted 5 years ago
POSTED BY: Vedat Erturk
POSTED BY: Hans Dolhaine
Posted 5 years ago
POSTED BY: Vedat Erturk
Posted 5 years ago

I just copied and pasted all of the code you provided above in a new notebook and ran it against a fresh kernel. Generating the tt table takes ~45 sec. to finish on my laptop running "12.2.0 for Mac OS X x86 (64-bit) (December 12, 2020)"

POSTED BY: Rohit Namjoshi
Posted 5 years ago

I can not get the same result Could you please as a single code again?

POSTED BY: Vedat Erturk
Posted 5 years ago

Helo hans, did you add these n = 1; w = 0.5; T = (2N[Pi])/w; Ts = nT;

POSTED BY: Vedat Erturk
Posted 5 years ago

Hello Hans, According to the paper, n=1,2,3,...,T=2Pi/w and Ts=nT. I have added them to the code. But I can not run it.I need your help.

Attachments:
POSTED BY: Vedat Erturk

Reverse@tt interchanges q and qQnum. So you get both plots,

POSTED BY: Hans Dolhaine

Hmmmm ?

Ok, add to the foregoing code this (but there seems to difficulties during the integration, I didn't have a closer look at this) It doesn't look similar to fig. 4c.

qQnum[g_] := Module[{},
  nT = 10;
  lsg = sol[g, nT];
  fx = x /. lsg[[1, 1]];
  fy = y /. lsg[[1, 2]];
  qQs = NIntegrate[fx[t] (Sin[w t] /. val), {t, 0, nT}] 2/nT;
  qQs = NIntegrate[fx[t] (Sin[w t] /. val), {t, 0, nT}] 2/nT;
  qQc = NIntegrate[fx[t] (Cos[w t] /. val), {t, 0, nT}] 2/nT;
  qnum = Sqrt[qQs^2 + qQc^2]]

and

tt = Table[{qQnum[g], g}, {g, 0, 200}];
ListLinePlot[tt]
ListLinePlot[Reverse /@ tt]
POSTED BY: Hans Dolhaine
Posted 5 years ago

I think

Table[{qQnum[g], g}, {g, 0, 200}]

should be replaced with

Table[{g,qQnum[g]}, {g, 0, 200}]

Right?

POSTED BY: Vedat Erturk
POSTED BY: Hans Dolhaine
Posted 5 years ago

Now first I should calculate Qnum given in Eq.(4.5) and then get the plot of g versus Qnum for this m0 given in Fig.4 c color in green interval in [0,200] for g.

POSTED BY: Vedat Erturk
Posted 5 years ago

Hello again Hans; Please see the attached files for my parameters.we can use them to get for example figure 4 c.&By the way, in your eq11b lam^2 x^4 should be replaced with lam^2 x^5Wolfram Notebook

Attachments:
POSTED BY: Vedat Erturk
POSTED BY: Hans Dolhaine
Posted 5 years ago
Attachments:
POSTED BY: Vedat Erturk
Posted 5 years ago
POSTED BY: Vedat Erturk
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard