Euler method: Difference between revisions

no edit summary
(→‎{{header|Maxima}}: add a solution)
No edit summary
Line 608:
90.000000000000000 20.001574639859214
100.00000000000000 20.000472391953071
</pre>
=={{header|freebasic}}==
<lang freebasic>
 
'Freebasic .9
 
'Custom rounding
#define round(x,N) Rtrim(Rtrim(Left(Str((x)+(.5*Sgn((x)))/(10^(N))),Instr(Str((x)+(.5*Sgn((x)))/(10^(N))),".")+(N)),"0"),".")
 
#macro Euler(fn,_y,min,max,h,printoption)
Print "Step ";#h;":":Print
Print "time","Euler"," Analytic"
If printoption<>"print" Then Print "Data omitted ..."
Scope
Dim As Double temp=(min),y=(_y)
Do
If printoption="print" Then Print temp,round(y,3),20+80*Exp(-0.07*temp)
y=y+(h)*(fn)
temp=temp+(h)
Loop Until temp>(max)
Print"________________"
Print
End Scope
#endmacro
 
Euler(-.07*(y-20),100,0,100,2,"don't print")
Euler(-.07*(y-20),100,0,100,5,"print")
Euler(-.07*(y-20),100,0,100,10,"print")
Sleep
</lang>
outputs (steps 5 and 10)
<pre>
Step 2:
 
time Euler Analytic
Data omitted ...
________________
 
Step 5:
 
time Euler Analytic
0 100 100
5 72 76.37504717749707
10 53.8 59.72682430331276
15 41.97 47.99501992889243
20 34.281 39.72775711532852
25 29.282 33.90191547603561
30 26.034 29.79651426023855
35 23.922 26.90348691994964
40 22.549 24.86480501001743
45 21.657 23.42817014936322
50 21.077 22.41579067378548
55 20.7 21.70237891507017
60 20.455 21.19964614563822
65 20.296 20.84537635070821
70 20.192 20.59572664567395
75 20.125 20.41980147193451
80 20.081 20.29582909731863
85 20.053 20.20846724147268
90 20.034 20.14690438216231
95 20.022 20.10352176843727
100 20.014 20.07295055724436
________________
 
Step 10:
 
time Euler Analytic
0 100 100
10 44 59.72682430331276
20 27.2 39.72775711532852
30 22.16 29.79651426023855
40 20.648 24.86480501001743
50 20.194 22.41579067378548
60 20.058 21.19964614563822
70 20.017 20.59572664567395
80 20.005 20.29582909731863
90 20.002 20.14690438216231
100 20 20.07295055724436
________________
 
</pre>
 
Anonymous user