Group Abstract Group Abstract

Message Boards Message Boards

0
|
8.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

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

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

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
POSTED BY: Vedat Erturk
Posted 5 years ago
POSTED BY: Vedat Erturk
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

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

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

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

Dear Hans,

Now it is ok. I fixed it. As a last thing, was my question logical? I mean the plot of G versus qQnum is logical? Right?

POSTED BY: Vedat Erturk

Hello Vedat,

I am afraid I cannot comment on that. I didn't read the whole paper you attached some posts above. What do you mean by "Is it logical to plot quantity1 against quantity2" ? Of course it is. The question is, what do you want to show.

I still don't know what you essentially want to do. Reproduce some figures of the paper? Doing some new things? Please excuse me, but I have no idea what you wanted to do. Rohit says, the code (which version of code exactly) works on his system, and he gets a reasonable plot of qQnum against g. If it this what you wanted I dare say, everything is ok. Or isn't it?

@Rohit: Did you not encounter error messages during the numerical integration? I got some errors concerning convergence of the integrals.

POSTED BY: Hans Dolhaine
Posted 5 years ago

Hi Hans,

There were no error messages. I believe you are running V8? Perhaps fixed in a later version. If you have a Wolfram Cloud account (free), you can see the results here.

POSTED BY: Rohit Namjoshi

@ 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

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

POSTED BY: Vedat Erturk

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
POSTED BY: Vedat Erturk

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 BY: Hans Dolhaine
Posted 5 years ago
Attachments:
POSTED BY: Vedat Erturk
Posted 5 years ago
POSTED BY: Rohit Namjoshi
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 BY: Hans Dolhaine
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

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
Posted 5 years ago

Thank you for your reply.G is constant.but let me explain in detail: Let us assume G=0. one gets the value of x(t) versus this G right? Again, let us assume G=0.1.one gets the value of x(t) versus this G right? Again, let us assume G=0.2.one gets the value of x(t) versus this G right? and so on. Now, is it logical to examine the behaviolur x(t) versus these G's? I mean: is it possible get the plot of G versus x(t)? Do you think it is well posed problem?Thanks.

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