Numerical methods

  • ihtus
  • Born
  • Born
  • ihtus
  • Posts: 4

Post 3+ Months Ago

Hallo!
My aim is to convert some Delphi code => Actionscript code.
I am solving a simple system of two differential equations using Euler method (the most simple method).
The equation i have is: y''+w0*w0*y = 0 - this is the equation of movement for a free oscillator. To use Euler methos i must have first order differentiak equation, that why i divide that 2 order equation into two => we have a system:
z=y'
z'=-w0*w0*y

Euler method gives solution for 1'st order diff. equations, as e have in the system, I'll describe briefly how it works:


we have a differntial equation: y'(x)=dy/dx=f(x,y)
and initial condition: y(x0)=y0
Then the solution of the diff equation (anothewords y values) will ba calculated using this formula: yk+1=yk+h*f(xk,yk)
h - step for x

So..I have Delphi code, you can download source project and also the compiled file from here, that works fine:

Code: [ Select ]
procedure TForm1.Button1Click(Sender: TObject);
begin
g1:=gettickcount();
timer1.Enabled:=true;
memo1.Clear;
memo2.Clear;
memo3.Clear;
memo4.Clear;
t:=0; h:=0.01; y:=1; z:=0; i:=0;
w:=10;
form1.Canvas.Brush.Color:=clred;
form1.Canvas.moveTo(round((g2-g1)/10),round(y*100)+500);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
g2:=gettickcount();
label9.Caption:=inttostr(g2-g1);

z:=z+h*(-w*w*y);
y:=y+h*z;

u:=cos(w*t);
memo1.Lines.add(floattostr(t));
memo2.Lines.add(floattostr(y));
memo3.Lines.add(floattostr(u));
memo4.Lines.add(floattostr((g2-g1)/10));

form1.Canvas.Pen.Color:=clred;
form1.Canvas.LineTo(round((g2-g1)/100),round(y*100)+500);

end;
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3. g1:=gettickcount();
  4. timer1.Enabled:=true;
  5. memo1.Clear;
  6. memo2.Clear;
  7. memo3.Clear;
  8. memo4.Clear;
  9. t:=0; h:=0.01; y:=1; z:=0; i:=0;
  10. w:=10;
  11. form1.Canvas.Brush.Color:=clred;
  12. form1.Canvas.moveTo(round((g2-g1)/10),round(y*100)+500);
  13. end;
  14. procedure TForm1.Timer1Timer(Sender: TObject);
  15. begin
  16. g2:=gettickcount();
  17. label9.Caption:=inttostr(g2-g1);
  18. z:=z+h*(-w*w*y);
  19. y:=y+h*z;
  20. u:=cos(w*t);
  21. memo1.Lines.add(floattostr(t));
  22. memo2.Lines.add(floattostr(y));
  23. memo3.Lines.add(floattostr(u));
  24. memo4.Lines.add(floattostr((g2-g1)/10));
  25. form1.Canvas.Pen.Color:=clred;
  26. form1.Canvas.LineTo(round((g2-g1)/100),round(y*100)+500);
  27. end;

And here is a screen of delphi program:
Image


And here is my ActionScript code (you can download flash source project from here ):

Code: [ Select ]
w=10; l=0;
lineStyle(1, 255 << 16 | 255 << 8 | 255, 100);
  1. w=10; l=0;
  2. lineStyle(1, 255 << 16 | 255 << 8 | 255, 100);


button code
Code: [ Select ]
on (release) {
t=0; h=0.01; y=1; z=0;
_root.moveTo(t/100,y*100+500);

ti=getTimer();

_root.onEnterFrame=function() {
t=(getTimer()-ti);

z=z+h*(-100*y);
y=y+h*z;

text2.text=y;
_root.lineTo(t/100,y*100+500);

}
}
  1. on (release) {
  2. t=0; h=0.01; y=1; z=0;
  3. _root.moveTo(t/100,y*100+500);
  4. ti=getTimer();
  5. _root.onEnterFrame=function() {
  6. t=(getTimer()-ti);
  7. z=z+h*(-100*y);
  8. y=y+h*z;
  9. text2.text=y;
  10. _root.lineTo(t/100,y*100+500);
  11. }
  12. }


and flash screen
Image

fps i set to 120fps

And here is the problem: flash calculates good at the beggining, but after several seconds it takes more and more time to calculate y values, and the distance between sinusoidals - is increasing...
I am despered...don't know what to do...delphi works fine...but in flash - such a problem. But i really need that code to work good in flash

Could anyone help me? :(
I apreciate any ideas!
Thank you!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • IceCold
  • Guru
  • Guru
  • User avatar
  • Posts: 1254
  • Loc: Ro

Post 3+ Months Ago

help ... nah,
explain what happens, yea
when you draw shapes or line like you did, flash redraws the whole object at each new shape added.
So now you can figure out why it goes slower and slower after more lines are added.
Also, about the frame rate, if you used 120 it means that 120 is the maximum frame rate that flash will use. It doesnt mean it will be 120 all the time; it will vary, function of the resources that it have. Because if you play 120 frames/sec on a pentium 2, make sure that computer cant afford 120 frames/sec.

What you can do is to increase the t with a certain amount instead of using the real time ... and that i just explained why.
so instead: t=(getTimer()-ti);
use : t += 10; // try a number till the graphic is the same
as speed, it will behave the same, good start but poor ending, but hey, at least the graphic will be uniform.
Probably if you use 8 ball you could overcome this issue by changing directly the pixels on a bitmap.
There might be a workaround, but didnt think of it yet.
goodluck
  • ihtus
  • Born
  • Born
  • ihtus
  • Posts: 4

Post 3+ Months Ago

hi again!

i have two cases of code:

1.
Code: [ Select ]
function f1 (y,z)
{
    return -9.8/1.5*Math.sin(y) ;
}
function f2(y,z)
{
    return z;
}

import flash.display.BitmapData;

var myBitmapData:BitmapData = new BitmapData(640, 480, true, 0xCCFFFFFF);

var mc:MovieClip = this.createEmptyMovieClip("mc", this.getNextHighestDepth());
mc.attachBitmap(myBitmapData, this.getNextHighestDepth());

var t=0, h=0.01, y=0.8*Math.PI, z=0, i=0;
var k11=0, k12=0; k13=0, k14=0, k21=0, k22=0, k23=0, k24=0;

//BUTTON

on (release) {
    presstime=getTimer();

_root.onEnterFrame=function() {
    time=getTimer()-presstime;
    
k11=h*f1(y,z);
k12=h*f1(y+h/2,z+k11/2);
k13=h*f1(y+h/2,z+k12/2);
k14=h*f1(y+h,z+k23);
z=z+(k11+2*k12+2*k13+k14)/6;

k21=h*f2(y,z);
k22=h*f2(y+h/2,z+k21/2);
k23=h*f2(y+h/2,z+k22/2);
k24=h*f2(y+h,z+k23);
y=y+(k21+2*k22+2*k23+k24)/6;


myBitmapData.setPixel(Math.round(y*50+200),time/500, 0x000000);

res1._rotation=90-180*y/Math.PI;
text1.text='fi= '+res1._rotation;

    }
}
  1. function f1 (y,z)
  2. {
  3.     return -9.8/1.5*Math.sin(y) ;
  4. }
  5. function f2(y,z)
  6. {
  7.     return z;
  8. }
  9. import flash.display.BitmapData;
  10. var myBitmapData:BitmapData = new BitmapData(640, 480, true, 0xCCFFFFFF);
  11. var mc:MovieClip = this.createEmptyMovieClip("mc", this.getNextHighestDepth());
  12. mc.attachBitmap(myBitmapData, this.getNextHighestDepth());
  13. var t=0, h=0.01, y=0.8*Math.PI, z=0, i=0;
  14. var k11=0, k12=0; k13=0, k14=0, k21=0, k22=0, k23=0, k24=0;
  15. //BUTTON
  16. on (release) {
  17.     presstime=getTimer();
  18. _root.onEnterFrame=function() {
  19.     time=getTimer()-presstime;
  20.     
  21. k11=h*f1(y,z);
  22. k12=h*f1(y+h/2,z+k11/2);
  23. k13=h*f1(y+h/2,z+k12/2);
  24. k14=h*f1(y+h,z+k23);
  25. z=z+(k11+2*k12+2*k13+k14)/6;
  26. k21=h*f2(y,z);
  27. k22=h*f2(y+h/2,z+k21/2);
  28. k23=h*f2(y+h/2,z+k22/2);
  29. k24=h*f2(y+h,z+k23);
  30. y=y+(k21+2*k22+2*k23+k24)/6;
  31. myBitmapData.setPixel(Math.round(y*50+200),time/500, 0x000000);
  32. res1._rotation=90-180*y/Math.PI;
  33. text1.text='fi= '+res1._rotation;
  34.     }
  35. }

source code - http://ihtus.xmgfree.com/flash_sources/pendulum/1.fla
swf - http://ihtus.xmgfree.com/flash_sources/pendulum/1.swf

when i ctrl+enter it - works great!
but if I ctrl+f12 (or open swf through a broswer) - the pendulum oscilates slower!
WHY?

2.
Code: [ Select ]
function f1 (y,z)
{
    return -9.8/1.5*Math.sin(y) ;
}
function f2(y,z)
{
    return z;
}

import flash.display.BitmapData;

var myBitmapData:BitmapData = new BitmapData(640, 480, true, 0xCCFFFFFF);

var mc:MovieClip = this.createEmptyMovieClip("mc", this.getNextHighestDepth());
mc.attachBitmap(myBitmapData, this.getNextHighestDepth());

var t=0, h=0.01, y=0.8*Math.PI, z=0, i=0;
var k11=0, k12=0; k13=0, k14=0, k21=0, k22=0, k23=0, k24=0;

pendul=function() {
    time=getTimer()-presstime;
    
k11=h*f1(y,z);
k12=h*f1(y+h/2,z+k11/2);
k13=h*f1(y+h/2,z+k12/2);
k14=h*f1(y+h,z+k23);
z=z+(k11+2*k12+2*k13+k14)/6;

k21=h*f2(y,z);
k22=h*f2(y+h/2,z+k21/2);
k23=h*f2(y+h/2,z+k22/2);
k24=h*f2(y+h,z+k23);
y=y+(k21+2*k22+2*k23+k24)/6;


myBitmapData.setPixel(Math.round(y*50+200),time/500, 0x000000);

res1._rotation=90-180*y/Math.PI;
text1.text='time= '+time/1000;

    }

//BUTTON

on (release) {
    
        presstime=getTimer();
        
setInterval(pendul,1);

}
  1. function f1 (y,z)
  2. {
  3.     return -9.8/1.5*Math.sin(y) ;
  4. }
  5. function f2(y,z)
  6. {
  7.     return z;
  8. }
  9. import flash.display.BitmapData;
  10. var myBitmapData:BitmapData = new BitmapData(640, 480, true, 0xCCFFFFFF);
  11. var mc:MovieClip = this.createEmptyMovieClip("mc", this.getNextHighestDepth());
  12. mc.attachBitmap(myBitmapData, this.getNextHighestDepth());
  13. var t=0, h=0.01, y=0.8*Math.PI, z=0, i=0;
  14. var k11=0, k12=0; k13=0, k14=0, k21=0, k22=0, k23=0, k24=0;
  15. pendul=function() {
  16.     time=getTimer()-presstime;
  17.     
  18. k11=h*f1(y,z);
  19. k12=h*f1(y+h/2,z+k11/2);
  20. k13=h*f1(y+h/2,z+k12/2);
  21. k14=h*f1(y+h,z+k23);
  22. z=z+(k11+2*k12+2*k13+k14)/6;
  23. k21=h*f2(y,z);
  24. k22=h*f2(y+h/2,z+k21/2);
  25. k23=h*f2(y+h/2,z+k22/2);
  26. k24=h*f2(y+h,z+k23);
  27. y=y+(k21+2*k22+2*k23+k24)/6;
  28. myBitmapData.setPixel(Math.round(y*50+200),time/500, 0x000000);
  29. res1._rotation=90-180*y/Math.PI;
  30. text1.text='time= '+time/1000;
  31.     }
  32. //BUTTON
  33. on (release) {
  34.     
  35.         presstime=getTimer();
  36.         
  37. setInterval(pendul,1);
  38. }

source code - http://ihtus.xmgfree.com/flash_sources/pendulum/2.fla
swf - http://ihtus.xmgfree.com/flash_sources/pendulum/2.swf

and the same problem:
when i ctrl+enter it - works great!
but if I ctrl+f12 (or open swf through a broswer) - the pendulum oscilates slower!
WHY?

So i realy need the application to work through broswer (player plugin) as it works in flashplayer!

Accidentely (for 2'nd code case) i hitted several times the button - and the pendulum started to oscilate faster and faster.
And I made some changes in button code
Code: [ Select ]
on (release) {
    
        presstime=getTimer();
        
setInterval(pendul,1);
setInterval(pendul,1);
setInterval(pendul,1);
setInterval(pendul,1);
}
  1. on (release) {
  2.     
  3.         presstime=getTimer();
  4.         
  5. setInterval(pendul,1);
  6. setInterval(pendul,1);
  7. setInterval(pendul,1);
  8. setInterval(pendul,1);
  9. }

I hit once and it oscilates faster then it did.

So my big questions are:
1) why can't broswer (ie, opera, firefox) just play the swf as flashplayer plays it?
2) why when i put multi setInterval functions - it began oscilates faster in the browser, and also in flashplayer - but here much more faster!
3) how can i make the speed of oscilations to be the same for browser player plugin, and for flashplayer?
Is multi setInterval functions - the only way to make broswer play the movie as it should be?

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 11 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.