javaScript example (one arch bridge)
Session 98: Taylor's Series
see clip one introduction to Taylor serie MIT
########################################################
#One Arch Bridge JavaScript code
########################################################
#you must have these5 slider in graphic window
#TileNumber= slider
#TileLength
#TileThickness
#TileStartingY0
#TileStartingX0
code in reset button javascript tab:
// this code in Reset Button, in "On clik" javascript tab
function DeleteTile(FirstTile,LastTile) {
alert("FirstTile:" + String(FirstTile) +" LastTile:" + String(LastTile));
for (var i = FirstTile ; i < LastTile ; i++)
{
ggbApplet.evalCommand("Delete[ Tile_{"+i+"} ]");
}
ggbApplet.evalCommand("OldTileNumber=0" );
}
DeleteTile(0,ggbApplet.getValue('TileNumber'));
#code in Global Javascript tab of slider TileNumber:
var CmSequenceJS=[];
var CmSeriesJS=[];
# code in update JavaScript Tab of slider TileNumber
function CMBuild() {
var TileNumberJS=ggbApplet.getValue('TileNumber');
var TileLengthJS=ggbApplet.getValue('TileLength');
var TileThicknessJS=ggbApplet.getValue('TileThickness');
var TileStartingY0JS=ggbApplet.getValue('TileStartingY0');
var TileStartingX0JS=ggbApplet.getValue('TileStartingX0');
var ParamJS=[TileNumberJS,TileLengthJS,TileThicknessJS,TileStartingY0JS,TileStartingX0JS];
// both arrays are now placed in global tab
//var CmSequenceJS=[];
//var CmSeriesJS=[];
var StartIntegrationJS=0.0;
var EndIntegrationJS=0.0;
var TileCenterOfMassJS=0.0;
var TileCenterOfMassPositionJS=0.0;
var TileMassJS=0.0;
var OldTileNumberJS;
var t=0.0;
//alert("trough CmBuild");
TileNumberJS=ggbApplet.getValue('TileNumber');
OldTileNumberJS=ggbApplet.getValue('OldTileNumber');
// first Tile x position = left Tile beginning here -->[______]
if (TileNumberJS >= OldTileNumberJS)
{
//alert("TileMassJS:" + String(TileMassJS));
//alert( "TileCenterOfMassPositionJS:" );
// the first Tile is positioned at his First center of mass at Zero
// so, we need to calculate his position in case of f= Linear Density not constant
StartIntegrationJS=0 ;EndIntegrationJS=TileLengthJS; // first Tile
Density(StartIntegrationJS) ;
ggbApplet.evalCommand("TileMass=Integral[ f,"+StartIntegrationJS+" , "+EndIntegrationJS+" ]");
TileMassJS=ggbApplet.getValue('TileMass');
ggbApplet.evalCommand("TileCenterOfMass=(1/"+TileMassJS+")*Integral[ x*f,"+StartIntegrationJS+" , "+EndIntegrationJS+" ]");
TileCenterOfMassPositionJS=ggbApplet.getValue('TileCenterOfMass');
CmSeriesJS[0]=0;
for (var i = 1 ; i < TileNumberJS ; i++)
{
// xcm = weighted average of position = (Sum[m_i*x_i] )/(Sum[m_i])
// CmSeriesJS[i]= (CmSeriesJS[i-1]*i*TileMassJS + TileCenterOfMassPositionJS *TileMassJS) /(i*TileMassJS);
// so dividing numerator and denominator of the right term above by TileMassJS
CmSeriesJS[i]= (CmSeriesJS[i-1]*i + TileCenterOfMassPositionJS ) /i;
OldTileNumberJS=i+1 ;
//alert("OldTileNumberJS:" + String(OldTileNumberJS));
ggbApplet.evalCommand("OldTileNumber="+OldTileNumberJS+" ");
CmSeriesJS.length=i+1 ; //size the sequence array to number of elements
}
// add array element in first position (no more needed)
//CmSequenceJS.unshift(0);
DisplayTile(CmSeriesJS,TileLengthJS,TileThicknessJS,TileCenterOfMassPositionJS,TileStartingX0JS,TileStartingY0JS);
}
else
{
TileNumberJS=ggbApplet.getValue('TileNumber');
OldTileNumberJS=ggbApplet.getValue('OldTileNumber');
//alert("TileNumber:" + String(TileNumberJS) + " OldTileNumber:" + String(OldTileNumberJS));
DeleteTileS(TileNumberJS,OldTileNumberJS);
OldTileNumberJS=OldTileNumberJS-1;
ggbApplet.evalCommand("OldTileNumber="+OldTileNumberJS+" ");
CmSeriesJS.length = TileNumberJS ;
}
return CmSeriesJS ;
}
function showArray(Arr){
var quote = "";
for (var i = 0; i < Arr.length; i++){
quote += Arr[i] + " ";
}
return quote;
}
// Linear Density Function
function Density(StartIntegrationJS)
{
//Constant
//ggbApplet.evalCommand("f(x)=1");
//density growing on the right for the tile
// f(x)=(x)
}
function BuildSeries(CmSequenceJS) {
var CmSeriesJS=[];
CmSeriesJS[0]=0.0;
for (var i = 1 ; i < CmSequenceJS.length ; i++)
CmSeriesJS[i]= CmSeriesJS[i-1] + CmSequenceJS[i] ;
return CmSeriesJS ;
}
function BuildSequence(CmSeriesJS) {
var CmSequenceJS=[];
CmSequenceJS[0]=0.0;
for (var i = 1 ; i < CmSeriesJS.length ; i++)
CmSequenceJS[i]= CmSeriesJS[i]- CmSequenceJS[i-1] ;
return CmSequenceJS ;
}
function DisplayTile(CmSeriesJS,TileLengthJS,TileThicknessJS,TileCenterOfMassPositionJS,TileStartingX0JS,TileStartingY0JS) {
var cornerJS=[0,1,2,3,4,5,6,7];
//alert("TileStartingY0JS: " + String(TileStartingY0JS));
/*
alert("cornerJS=[3][0]: " + String(cornerJS=[8]));
alert(String(TileLengthJS));
alert(String(TileThicknessJS));
alert(String(TileStartingX0JS));
alert(String(TileStartingY0JS));
[2,3]________ [0,1]
| |
|_____________|[6,7]
[4,5] |Cm
*/
for (var i = 0 ; i < CmSeriesJS.length ; i++)
{
cornerJS=
[ // [0,1,2,3,4,5,6,7]
TileStartingX0JS+CmSeriesJS[i]+(TileLengthJS-TileCenterOfMassPositionJS),TileStartingY0JS-i*TileThicknessJS, //[0,1] -->("+cornerJS[0]+","+cornerJS[1]+")
TileStartingX0JS+CmSeriesJS[i]-(TileCenterOfMassPositionJS),TileStartingY0JS-i*TileThicknessJS, //[2,3] -->("+cornerJS[2]+","+cornerJS[3]+")
TileStartingX0JS+CmSeriesJS[i]-(TileCenterOfMassPositionJS),TileStartingY0JS-(i+1)*TileThicknessJS, //[4,5] -->("+cornerJS[4]+","+cornerJS[5]+")
TileStartingX0JS+CmSeriesJS[i]+(TileLengthJS-TileCenterOfMassPositionJS),TileStartingY0JS-(i+1)*TileThicknessJS //[6,7] -->("+cornerJS[6]+","+cornerJS[7]+")
];
ggbApplet.evalCommand("Tile_{"+i+"}=Polygon[("+cornerJS[0]+","+cornerJS[1]+"),("+cornerJS[2]+","+cornerJS[3]+"),("+cornerJS[4]+","+cornerJS[5]+"),("+cornerJS[6]+","+cornerJS[7]+")]");
}
return cornerJS ;
}
// this code is also in Reset Button, in "On clik" javascript tab
function DeleteTileS(FirstTile,LastTile) {
//alert("FirstTile:" + String(FirstTile) +" LastTile:" + String(LastTile)));
for (var i = FirstTile ; i < LastTile ; i++)
{
ggbApplet.evalCommand("Delete[ Tile_{"+i+"} ]");
}
}
showArray(CMBuild());
# end One arch bridge Javascript code