2
$\begingroup$

I am new to programming and try to calculate the values of A[t] for $t$ from 0 to 0.5 in steps of 0.025 as follows but it does not work;

Block[{$RecursionLimit=Infinity},
  Mass = m = 500000;
  StiffnessCoefficient = k =300000000;
  {NaturalFrequency = Sqrt[k/m], Period = 2*Pi/NaturalFrequency, FundamentalFrequency = 2*Pi}//N;
  P0 = 5000000;
  P[t_] := P0*Sin[FundamentalFrequency*t];
  Δτ = 0.025;
  A[0] = 0;
  A[t_] := A[t - Δτ] + P[t - Δτ]*Cos[FundamentalFrequency*(t - Δτ)] + P[t]*Cos[FundamentalFrequency*t];
  Table[A[t], {t, 0.025, 0.5, 0.025}]
]

I also tried different looping structures using Do, For or While sequences but I don't understand how to achieve it.

Could you help me solve my problem please ?

$\endgroup$
2
$\begingroup$

The issue with your code is that you are indexing into A with real (floating point) numbers. Since these are never exact, there are values that cannot be computed. To fix, remove the Block[ ] and change:

Δτ = 25/1000;
Table[A[t], {t, 25/1000, 1/2, 25/1000}] // N

This indexes with exact numbers and so gives an answer.

$\endgroup$
4
  • $\begingroup$ Thank you very much @bill s that is exactly what I wanted! $\endgroup$ – Mav Nov 24 '19 at 15:36
  • $\begingroup$ Probably a more economical change (and possibly faster) is to change the definition of A[0] to A[0.] $\endgroup$ – Carlo Nov 25 '19 at 15:46
  • $\begingroup$ @Carlo -- I don't think that works. The problem is that real/floating point numbers experience round off errors and once an error (no matter how tiny) occurs, you'll end up with undefined terms. $\endgroup$ – bill s Nov 26 '19 at 2:48
  • $\begingroup$ You might be right, I only checked without the Block and the first two iterations were working. $\endgroup$ – Carlo Nov 27 '19 at 7:26
1
$\begingroup$

Maybe this produces what you seek

P[t_] := 5000000 Sin[2. Pi t];
Δτ = 0.025;
Accumulate[
 Table[
  P[t - Δτ] Cos[2. Pi (t - Δτ)] + P[t] Cos[2. Pi t], 
  {t, Δτ, 0.5, Δτ}]
 ]
$\endgroup$
6
  • $\begingroup$ Thank you for your help, but why isn't P evaluated in your piece of code @Henrick Schumacher ? $\endgroup$ – Mav Nov 24 '19 at 15:38
  • $\begingroup$ I do not understand your question; P is evaluated. Maybe you are missing A? That's what Accumulate is good for. $\endgroup$ – Henrik Schumacher Nov 24 '19 at 17:38
  • $\begingroup$ Sorry for that, I said that because I obtain {1. p[0.]+0.987688 p[0.025],1. p[0.]+1.97538 etc... instead of a list of the values I seek. $\endgroup$ – Mav Nov 24 '19 at 18:22
  • $\begingroup$ With my code? Impossible. There is no lower case p. $\endgroup$ – Henrik Schumacher Nov 24 '19 at 18:34
  • 1
    $\begingroup$ Please, restart the kernel (with Exit and Shift+Enter) and copy the code here into your notebook. $\endgroup$ – Henrik Schumacher Nov 24 '19 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.