function k=choc(x,z) if ((x<0.05)|(x>0.95))&(z<0.05) then k=3;//rebond dans un coin elseif (z<0.05) then k=2;// rebond sur le fond elseif (x<0.05)|(x>0.95) then k=1;//rebond sur un coté else k=0; end endfunction function [vx,vz]=v_apres_choc(k,vx,vz) pert=0.9; if (k==1)|(k==3) then vx=-vx*pert;//rebond sur un bord end if (k==2)|(k==3) then vz=-vz*pert;//rebond sur le fond end endfunction //lancement affichage xset("color",1); titlepage(["résolution numérique d une EDO";" ";"une balle rebondissant dans une boite"]) xtitle("(click gauche pour démarrer, click droit pour arrêter)") [c_i,c_x,c_y,c_w]=xclick(); xset("pixmap",1)//ouverture du buffer video rect=[-0.2,-0.2,1.2,1.2]; while (c_i<>2)&(c_i<>5) //tant qu'on a pas fait un click droit xbasc() clear_pixmap() //affichage de la boite u=[0;0];v=[1;1];w=[0;1]; plot2d([u v w],[w w u],[1 1 1],frameflag=5,axesflag=0,rect=rect) winnum=winsid() ; toolbar(winnum(1),'off'); xtitle("(click gauche pour définir la position initiale de la balle, click droit pour arrêter)") show_pixmap() //initialisations [c_i,x,z,c_w]=xclick(); if ((x<1)&(x>0))&((z<1)&(z>0)) then g=40; frot=0.1; dt=0.01;T=5; X=[x];Z=[z]; vx=4-8*rand(); vz=1-2*rand();//la balle est lachée avec une vitesse "faible" et aléatoire for t=0:dt:T clear_pixmap() //accélération de la balle ax=-frot*vx; if z>0.05 then az=-g-frot*vz; else az=0;//si on touche le fond end //calcul des vitesses vx=vx+ax*dt; if z>0.05 then vz=vz+az*dt; else vz=vz*0.8; end //calcul de la nouvelle position x=x+vx*dt; z=z+vz*dt; //modification de la vitesse en cas de choc k=choc(x,z); if k<>0 then [vx,vz]=v_apres_choc(k,vx,vz); end //modification de la position de balle //pour pas sortir de la boite //à placer après le contrôle de choc //sinon calcul de vz est faussé! if x<0.05 then x=0.05; elseif x>0.95 then x=0.95; end if z<0.05 then z=0.04844;//ajusté pour éviter l'effet "sautillement" end X=[X; x]; Z=[Z; z]; //affichage xbasc() clear_pixmap() plot2d([u v w],[w w u],[1 1 1],frameflag=5,axesflag=0,rect=rect) winnum=winsid(); toolbar(winnum(1),'off'); plot2d(X,Z,2)//la trajectoire xset("color",5); xfarc(x-0.05,z+0.05,0.1,0.1,0,360*64)//la balle xset("color",1); xstring(0,1.1,["t=" string(t)]);//temps écoulé show_pixmap() end else xbasc() clear_pixmap() xtitle("le point n''est pas dans la boite (faire un clic revenir au menu)") show_pixmap() [c_i,c_x,c_y,c_w]=xclick(); end xs2gif(0,'balle_rebond.gif') xtitle(" simulation terminée (faire un clic pour revenir au menu)") show_pixmap() xbasc() clear_pixmap() [c_i,c_x,c_y,c_w]=xclick(); xbasc() clear_pixmap() titlepage(["résolution numérique d une EDO";" ";"une balle rebondissant dans une boite"]) xtitle("(click gauche pour démarrer, click droit pour arrêter)") show_pixmap() [c_i,c_x,c_y,c_w]=xclick(); end xset("pixmap",0)//fermeture du buffer video a=winsid(); xdel(a)//destrcution dela fenêtre graphique