E-school  di  Arrigo Amadori

Calcolo numerico 


Mappa esponenziale


 Per la trattazione teorica alla base di questo programma numerico vedi la pagina :
http://www.arrigoamadori.com/lezioni/Miscellanea/2/GeodeticheEMappeEsp.htm .


tensore metrico :

 g11(u , v) =   g12(u , v) =
 g21(u , v) = g12              g22(u , v) =


parametri immagine :

 lar = alt = (larghezza ed altezza immagine in pixel)
 ua  = ub  = (estremi asse u)
 va  = vb  = (estremi asse v)


mappa esponenziale :

 p1    =   
 p2    =   
 V1(t) =  
 V2(t) =  
 
 ta =    tb = (estremi parametro t) dt = (incremento parametro t)
 
 
dt      = (incremento parametro t per geodetiche) 
 visgeod = (visualizza geodetiche)
 


 

 


Regole :

 1 - le variabili ammesse sono quelle indicate nella varie funzioni
 2 - gli operatori aritmetici ammessi sono : +, -, *, /
 3 - non devono esistere spazi bianchi all'interno della funzione 
 4 - usare solo le parentesi tonde
 5 - per i numeri decimali oltre lo zero utilizzare il punto (.)
 6 - le funzioni permesse sono : sin() cos() tan() atan() sqrt() log() exp() 
 7 - La funzione potenza è :  pow(base, esponente)
 8 - non sono ammessi esponenti contenenti le variabili 

Suite di programmi di calcolo numerico : http://lnx.arrigoamadori.com/CalcoloNumerico/calcolonumericolnx.htm 


Sorgenti PHP :


mappaesponenziale.php


<html>

<head>
<title>Mappa esponenziale</title>
</head>

<body>

<script language="php">

error_reporting (E_ALL ^ E_NOTICE);

//
// disegna vari tipi di curve sul piano (u,v)
//

// inserisce tutti gli include (funzioni esterne)

if (is_integer(strpos($SERVER_SOFTWARE, "Win")))
{
include("..\Routines\sgn.php");
include("..\Routines\contrFunz.php");
include("..\Routines\derivataParz.php");
include("..\Routines\daPowAVb.php");
include("..\Routines\daVbAPow.php");
include("..\Routines\derivataMonom.php");
include("..\Routines\semplEspr.php");
include("..\Routines\calcTensMetrCovarDiV2.php");
include("..\Routines\calcTensMetrContrDiV2.php");
include("..\Routines\calcSimbChris1DiV2.php");
include("..\Routines\calcSimbChris2DiV2.php");
}
else
{
include("../Routines/sgn.php");
include("../Routines/contrFunz.php");
include("../Routines/derivataParz.php");
include("../Routines/daPowAVb.php");
include("../Routines/daVbAPow.php");
include("../Routines/derivataMonom.php");
include("../Routines/semplEspr.php");
include("../Routines/calcTensMetrCovarDiV2.php");
include("../Routines/calcTensMetrContrDiV2.php");
include("../Routines/calcSimbChris1DiV2.php");
include("../Routines/calcSimbChris2DiV2.php");
}

// controlli vari

if ($g11 == "")
{
echo "Attenzione !!! Manca g11 !!!";
return;
}
if ($g12 == "")
{
echo "Attenzione !!! Manca g12 !!!";
return;
}
if ($g22 == "")
{
echo "Attenzione !!! Manca g22 !!!";
return;
}
if ($p1 == "")
{
echo "Attenzione !!! Manca p1 !!!";
return;
}
if ($p2 == "")
{
echo "Attenzione !!! Manca p2 !!!";
return;
}
if ($V1 == "")
{
echo "Attenzione !!! Manca V1 !!!";
return;
}
if ($V2 == "")
{
echo "Attenzione !!! Manca V2 !!!";
return;
}

$g11 = trim($g11);
$contr = contrFunz($g11);
if ($contr == "1")
{
echo "Attenzione !!! La funzione g11 presenta spazi bianchi, impossibile procedere !!!";
return;
}
if ($contr == "2")
{
echo "Attenzione !!! La funzione g11 presenta il carattere ^, impossibile procedere !!! Usare la funzione pow(... , ...) !!!";
return;
}

$g12 = trim($g12);
$contr = contrFunz($g12);
if ($contr == "1")
{
echo "Attenzione !!! La funzione g12 presenta spazi bianchi, impossibile procedere !!!";
return;
}
if ($contr == "2")
{
echo "Attenzione !!! La funzione g12 presenta il carattere ^, impossibile procedere !!! Usare la funzione pow(... , ...) !!!";
return;
}

$g22 = trim($g22);
$contr = contrFunz($g22);
if ($contr == "1")
{
echo "Attenzione !!! La funzione g22 presenta spazi bianchi, impossibile procedere !!!";
return;
}
if ($contr == "2")
{
echo "Attenzione !!! La funzione g22 presenta il carattere ^, impossibile procedere !!! Usare la funzione pow(... , ...) !!!";
return;
}

$V1 = trim($V1);
$contr = contrFunz($V1);
if ($contr == "1")
{
echo "Attenzione !!! La funzione V1 presenta spazi bianchi, impossibile procedere !!!";
return;
}
if ($contr == "2")
{
echo "Attenzione !!! La funzione V1 presenta il carattere ^, impossibile procedere !!! Usare la funzione pow(... , ...) !!!";
return;
}
$V1old = $V1;
$V1 = str_replace("atanh", "aaa", $V1);
$V1 = str_replace("atan", "bbb", $V1);
$V1 = str_replace("tanh", "ccc", $V1);
$V1 = str_replace("tan", "ddd", $V1);
$V1 = str_replace("sqrt", "eee", $V1);
$V1 = str_replace("t", "z", $V1);
$V1 = str_replace("aaa", "atanh", $V1);
$V1 = str_replace("bbb", "atan", $V1);
$V1 = str_replace("ccc", "tanh", $V1);
$V1 = str_replace("ddd", "tan", $V1);
$V1 = str_replace("eee", "sqrt", $V1);

$V2 = trim($V2);
$contr = contrFunz($V2);
if ($contr == "1")
{
echo "Attenzione !!! La funzione V2 presenta spazi bianchi, impossibile procedere !!!";
return;
}
if ($contr == "2")
{
echo "Attenzione !!! La funzione V2 presenta il carattere ^, impossibile procedere !!! Usare la funzione pow(... , ...) !!!";
return;
}
$V2old = $V2;
$V2 = str_replace("atanh", "aaa", $V2);
$V2 = str_replace("atan", "bbb", $V2);
$V2 = str_replace("tanh", "ccc", $V2);
$V2 = str_replace("tan", "ddd", $V2);
$V2 = str_replace("sqrt", "eee", $V2);
$V2 = str_replace("t", "z", $V2);
$V2 = str_replace("aaa", "atanh", $V2);
$V2 = str_replace("bbb", "atan", $V2);
$V2 = str_replace("ccc", "tanh", $V2);
$V2 = str_replace("ddd", "tan", $V2);
$V2 = str_replace("eee", "sqrt", $V2);

// programma

$im = ImageCreate ($lar + 100, $alt + 160); 

$bianco = ImageColorAllocate ($im, 255, 255, 255);
$nero = ImageColorAllocate ($im, 0, 0, 0);
$rosso = ImageColorAllocate ($im, 255, 180, 180); // sbiadito !!!
$rossorosso = ImageColorAllocate ($im, 255, 0, 0); 
$verde = ImageColorAllocate ($im, 0, 255, 0);
$blu = ImageColorAllocate ($im, 180, 180, 255); // sbiadito !!!
$blublu = ImageColorAllocate ($im, 0, 0, 255); 
$rosa = ImageColorAllocate ($im, 255, 0, 255);
$grigio = ImageColorAllocate ($im, 200, 200, 200);

$rV1 = str_replace("z", '$t', $V1);
$rV2 = str_replace("z", '$t', $V2);

// parametri di scala 

$xa = $ua; $xb = $ub;
$ya = $va; $yb = $vb;

$dx = ($xb - $xa) / ($lar - 1); // intervallo di scomposizione asse x 
$dy = ($yb - $ya) / ($alt - 1); // intervallo di scomposizione asse y

// disegno frame con coordinate 

Imageline ($im, 0, 0, $lar, 0, $nero);
Imageline ($im, 0, $alt, $lar, $alt, $nero);
Imageline ($im, 0, 0, 0, $alt, $nero);
Imageline ($im, $lar, 0, $lar, $alt, $nero);

ImageString ($im, 1, 0, $alt + 10, $xa . " (u)", $nero);
ImageString ($im, 1, $lar, $alt + 10, $xb, $nero);
ImageString ($im, 1, $lar + 10, $alt, $ya . " (v)", $nero);
ImageString ($im, 1, $lar + 10, 0, $yb, $nero);

if ($xa < 0) // disegna asse y se dentro intervallo
{
Imageline ($im, abs($xa) / $dx, 0, abs($xa) / $dx, $alt, $nero);
ImageString ($im, 1, abs($xa) / $dx, $alt + 10, "0", $nero);
}
if ($ya < 0) // disegna asse x se dentro intervallo
{
Imageline ($im, 0, $alt - abs($ya) / $dy, $lar, $alt - abs($ya) / $dy, $nero);
ImageString ($im, 1, $lar + 10, $alt - abs($ya) / $dy, "0", $nero);
}


// calcola simboli di Christoffel

$g[1][1] = $g11;
$g[1][2] = $g12;
$g[2][1] = $g12;
$g[2][2] = $g22;

//echo $g[1][1] . "<p>";
//echo $g[1][2] . "<p>";
//echo $g[2][1] . "<p>";
//echo $g[2][2] . "<p>";

$gg = calcTensMetrContrDiV2($g);
$chris1 = calcSimbChris1DiV2($g);
$chris2 = calcSimbChris2DiV2($g, $gg, $chris1);

For ($i = 1; $i <= 2; $i++)
{
For ($j = 1; $j <= 2; $j++)
{
For ($k = 1; $k <= 2; $k++)
{
$f[$i][$j][$k] = $chris2[$i][$j][$k];
$f[$i][$j][$k] = str_replace("u", '$x1', $f[$i][$j][$k]);
$f[$i][$j][$k] = str_replace("v", '$x2', $f[$i][$j][$k]);
}
}
}

// disegna punto p

if ($p1 >= $xa and $p1 <= $xb and $p2 >= $ya and $p2 <= $yb )
{
$xx = ($p1 - $xa) / $dx;
$yy = (abs($yb - $ya) - ($p2 - $ya)) / $dy;
Imageline ($im, $xx, $yy, $xx, $yy, $nero);
}

// disegna curva V

$ni = ($tb - $ta) / $dt; 

for ($i = 1; $i <= $ni; $i++) 
{
$t = $ta + ($i - 1) * $dt;
$x = 0;
eval("\$ff = @($rV1);");
$x = $p1 + $ff;
$y = 0;
eval("\$ff = @($rV2);");
$y = $p2 + $ff;
$xx = 0;
$yy = 0;
if ($x >= $xa and $x <= $xb and $y >= $ya and $y <= $yb )
{
$xx = ($x - $xa) / $dx;
$yy = (abs($yb - $ya) - ($y - $ya)) / $dy;
Imageline ($im, $xx, $yy, $xx, $yy, $nero);
}
}

// disegna curva v e punti mappa esponenziale

$v1 = "(1/sqrt($g11))*(V1)-sqrt((($g12)*($g12))/(($g11)*(($g11)*($g22)-($g12)*($g12))))*(V2)";
$v2 = "sqrt(($g11)/(($g11)*($g22)-($g12)*($g12)))*(V2)";

$v1 = str_replace("u", "$p1", $v1);
$v1 = str_replace("v", "$p2", $v1);
$v2 = str_replace("u", "$p1", $v2);
$v2 = str_replace("v", "$p2", $v2);

$rv1 = $v1;
$rv2 = $v2;

$rv1 = str_replace("V1", "$rV1", $rv1);
$rv1 = str_replace("V2", "$rV2", $rv1);
$rv2 = str_replace("V1", "$rV1", $rv2);
$rv2 = str_replace("V2", "$rV2", $rv2);

//echo $v1 . "<p>";
//echo $v2 . "<p>";

$ni = ($tb - $ta) / $dt; 

for ($i = 1; $i <= $ni; $i++) 
{
$t = $ta + ($i - 1) * $dt;
$x = 0;
eval("\$ff = @($rv1);");
$rrv1 = $ff;
$x = $p1 + $ff;
$y = 0;
eval("\$ff = @($rv2);");
$rrv2 = $ff;
$y = $p2 + $ff;
$xx = 0;
$yy = 0;
if ($x >= $xa and $x <= $xb and $y >= $ya and $y <= $yb )
{
$xx = ($x - $xa) / $dx;
$yy = (abs($yb - $ya) - ($y - $ya)) / $dy;
Imageline ($im, $xx, $yy, $xx, $yy, $verde);
}

// disegna geodetica 

$nig = 1000000;

$lungV = "sqrt(($rrv1)*($rrv1)*($g11)+2*($rrv1)*($rrv2)*($g12)+($rrv2)*($rrv2)*($g22))";

$lungV = str_replace("u", "$p1", $lungV);
$lungV = str_replace("v", "$p2", $lungV);

eval("\$lungV = @($lungV);");

//echo $lungV . "<p>";

$lungG = 0;

$u0 = $p1;
$v0 = $p2;
$du0 = $rrv1;
$dv0 = $rrv2;

$x1 = $p1;
$x2 = $p2;
$ddu0 = 0; 
$ff = $f[1][1][1]; eval("\$fff = @($ff);");
$ddu0 = $ddu0 - $fff * $du0 * $du0;
$ff = $f[1][2][1]; eval("\$fff = @($ff);");
$ddu0 = $ddu0 - $fff * $du0 * $dv0;
$ff = $f[2][1][1]; eval("\$fff = @($ff);");
$ddu0 = $ddu0 - $fff * $dv0 * $du0;
$ff = $f[2][2][1]; eval("\$fff = @($ff);");
$ddu0 = $ddu0 - $fff * $dv0 * $dv0;

$ddv0 = 0; 
$ff = $f[1][1][2]; eval("\$fff = @($ff);");
$ddv0 = $ddv0 - $fff * $du0 * $du0;
$ff = $f[1][2][2]; eval("\$fff = @($ff);");
$ddv0 = $ddv0 - $fff * $du0 * $dv0;
$ff = $f[2][1][2]; eval("\$fff = @($ff);");
$ddv0 = $ddv0 - $fff * $dv0 * $du0;
$ff = $f[2][2][2]; eval("\$fff = @($ff);");
$ddv0 = $ddv0 - $fff * $dv0 * $dv0;

For ($ii = 1; $ii <= $nig; $ii++)
{
// approssimazione della geodetica con metodo delle diff. finite
$u1 = $dtg * $du0 + $u0;
$du1 = $dtg * $ddu0 + $du0;
$v1 = $dtg * $dv0 + $v0;
$dv1 = $dtg * $ddv0 + $dv0;
$ddu1 = 0;
$x1 = $u1;
$x2 = $v1;
$ff = $f[1][1][1]; eval("\$fff = @($ff);");
$ddu1 = $ddu1 - $fff * $du1 * $du1;
$ff = $f[1][2][1]; eval("\$fff = @($ff);");
$ddu1 = $ddu1 - $fff * $du1 * $dv1;
$ff = $f[2][1][1]; eval("\$fff = @($ff);");
$ddu1 = $ddu1 - $fff * $dv1 * $du1;
$ff = $f[2][2][1]; eval("\$fff = @($ff);");
$ddu1 = $ddu1 - $fff * $dv1 * $dv1;
$ddv1 = 0;
$ff = $f[1][1][2]; eval("\$fff = @($ff);");
$ddv1 = $ddv1 - $fff * $du1 * $du1;
$ff = $f[1][2][2]; eval("\$fff = @($ff);");
$ddv1 = $ddv1 - $fff * $du1 * $dv1;
$ff = $f[2][1][2]; eval("\$fff = @($ff);");
$ddv1 = $ddv1 - $fff * $dv1 * $du1;
$ff = $f[2][2][2]; eval("\$fff = @($ff);");
$ddv1 = $ddv1 - $fff * $dv1 * $dv1;

$x = $u1;
$y = $v1;

if ($visgeod == "S")
{
if ($x >= $xa and $x <= $xb and $y >= $ya and $y <= $yb )
{
$xx = ($x - $xa) / $dx;
$yy = (abs($yb - $ya) - ($y - $ya)) / $dy;
Imageline ($im, $xx, $yy, $xx, $yy, $grigio);
}
}

$ds = "sqrt(($g11)*($dtg*$du0)*($dtg*$du0)+2*($g12)*($dtg*$du0)*($dtg*$dv0)+($g22)*($dtg*$dv0)*($dtg*$dv0))";

$ds = str_replace("u", "$u1", $ds);
$ds = str_replace("v", "$v1", $ds);

eval("\$ds = @($ds);");

$lungG = $lungG + $ds;

if ($lungG >= $lungV)
{
if ($x >= $xa and $x <= $xb and $y >= $ya and $y <= $yb )
{
$xx = ($x - $xa) / $dx;
$yy = (abs($yb - $ya) - ($y - $ya)) / $dy;
Imageline ($im, $xx, $yy, $xx, $yy, $rossorosso);
}
break;
}

//echo $lungG . "<p>";

// reiterazione

$u0 = $u1;
$du0 = $du1;
$ddu0 = $ddu1;
$v0 = $v1;
$dv0 = $dv1;
$ddv0 = $ddv1;
}

} // fine geodetica 

// riporta dati iniziali

ImageString ($im, 2, 0, $alt + 30, "g11 = " . $g11 . " ; g12 = " . $g12, $nero);
ImageString ($im, 2, 0, $alt + 40, "g21 = " . $g12 . " ; g22 = " . $g22, $nero);

ImageString ($im, 2, 0, $alt + 60, "punto : ( p1 , p2 ) = ( " . $p1 . " , " . $p2 . " )", $nero);

ImageString ($im, 2, 0, $alt + 80, "vettore V : ( V1(t) , V2(t) ) = ( " . $V1old . " , " . $V2old . " ) ; ta = " . $ta . " ; tb = " . $tb . " ; dt = " . $dt, $nero);

ImageString ($im, 2, 0, $alt + 100, "vettore v", $verde);

ImageString ($im, 2, 0, $alt + 120, "mappa esponenziale", $rossorosso);

ImageString ($im, 2, 0, $alt + 140, "geodetiche", $grigio);

// disegna immagine

$nomefile = $REMOTE_ADDR;
$nomefile = "grafico" .str_replace(".", "-", $nomefile). ".png";

ImagePng ($im, $nomefile); 

echo "&nbsp;<p>";
echo "&nbsp;<p>";
echo "&nbsp;<p>";
echo "&nbsp;<p>";

echo "<a href='$nomefile'>Visualizza il grafico</a>";

</script>

</body>

</html>

</html>


sgn.php


<script language="php">

error_reporting (E_ALL ^ E_NOTICE);

function sgn($x)
{
// funzione sgn
// x = variabile indipendente

if ($x > 0)
{
return +1;

if ($x == 0)
{
return 0;

if ($x < 0)
{
return -1;


} // fine sgn()

</script>


contrFunz.php


<script language="php">

error_reporting (E_ALL ^ E_NOTICE);

function contrFunz($funz)
{
// controllo formale digitazione funzione
// viene controllata l'esistenza dei seguenti caratteri : blank, ^
// se tali caratteri vengono trovati, vine settata la variabile d'uscita
// $funz = funzione da controllare

$errore = "";

if (is_numeric(strpos($funz, ' ')))
{
$errore = "1";


if (is_numeric(strpos($funz, '^')))
{
$errore = "2";


return $errore;

} // fine contrFunz

</script>


derivataParz.php daPowAVb.php daVbAPow.php derivataMonom.php semplEspr.php vedi :  derprimsimb.htm 


calcTensMetrCovarDiV2.php


<script language="php">

error_reporting (E_ALL ^ E_NOTICE);

function calcTensMetrCovarDiV2($x, $y, $z)
{

// calcolo del tensore metrico covariante di V2
// parametri in input :
// x = funzione di u e v
// y = funzione di u e v
// z = funzione di u e v
// parametri in output :
// g(i,k) = tensore metrico covariante di V2 (variabile global)

$g[1][1] = "pow(" . derivataParz($x, "u") . ", 2)";
$g[1][1] = $g[1][1] . "+pow(" . derivataParz($y, "u") . ", 2)";
$g[1][1] = $g[1][1] . "+pow(" . derivataParz($z, "u") . ", 2)";

$g[1][2] = "(" . derivataParz($x, "u") . ")*(" . derivataParz($x, "v") . ")";
$g[1][2] = $g[1][2] . "+(" . derivataParz($y, "u") . ")*(" . derivataParz($y, "v") . ")";
$g[1][2] = $g[1][2] . "+(" . derivataParz($z, "u") . ")*(" . derivataParz($z, "v") . ")";

$g[2][1] = $g[1][2];

$g[2][2] = "pow(" . derivataParz($x, "v") . ", 2)";
$g[2][2] = $g[2][2] . "+pow(" . derivataParz($y, "v") . ", 2)";
$g[2][2] = $g[2][2] . "+pow(" . derivataParz($z, "v") . ", 2)";

return $g;

} // fine calcTensMetrCovarDiV2()

</script>


calcTensMetrContrDiV2.php


<script language="php">

error_reporting (E_ALL ^ E_NOTICE);

function calcTensMetrContrDiV2($g)
{

// calcolo del tensore metrico controvariante di V2
// parametri in input :
// g(i,k) = tensore metrico controvariante di V2 
// parametri in output :
// gcontr(i,k) = tensore metrico controvariante di V2 

$denom = "((" . $g[1][1] . ")*(" . $g[2][2] . ")-(" . $g[1][2] . ")*(" . $g[2][1] . "))";

$gcontr[1][1] = "(" . $g[2][2] . ")/" . $denom;
$gcontr[1][2] = "(-(" . $g[1][2] . ")/" . $denom . ")";
$gcontr[2][1] = "(-(" . $g[2][1] . ")/" . $denom . ")";
$gcontr[2][2] = "(" . $g[1][1] . ")/" . $denom; 

return $gcontr;

} // fine calcTensMetrContrDiV2()

</script>


calcSimbChris1DiV2.php


<script language="php">

error_reporting (E_ALL ^ E_NOTICE);

function calcSimbChris1DiV2($g)
{

// calcolo dei simboli di Christoffel del 1' tipo di V2
// parametri in input :
// g(i,k) = tensore metrico covariante di V2 
// parametri in output :
// chris1(j,l,k) = tensore metrico covariante di V2 

For ($j = 1; $j <= 2; $j++)
{
For ($l = 1; $l <= 2; $l++)
{
For ($k = 1; $k <= 2; $k++)
{
If ($l == 1) {$varl = "u";} 
If ($l == 2) {$varl = "v";} 
If ($j == 1) {$varj = "u";} 
If ($j == 2) {$varj = "v";} 
If ($k == 1) {$vark = "u";} 
If ($k == 2) {$vark = "v";} 
$chris1[$j][$l][$k] = "(1/2)*(" . derivataParz($g[$j][$k], $varl);
$chris1[$j][$l][$k] = $chris1[$j][$l][$k] . "+(" . derivataParz($g[$k][$l], $varj) . ")";
$chris1[$j][$l][$k] = $chris1[$j][$l][$k] . "-(" . derivataParz($g[$j][$l], $vark) . "))";




return $chris1;

} // fine calcSimbChris1DiV2()

</script>


calcSimbChris2DiV2.php


<script language="php">

error_reporting (E_ALL ^ E_NOTICE);

function calcSimbChris2DiV2($g, $gcontr, $chris1)
{

// calcolo dei simboli di Christoffel del 2' tipo di V2
// parametri in input :
// g(i,k) = tensore metrico covariante di V2 
// gcontr(i,k) = tensore metrico controvariante di V2 
// chris1(j,l,k) = simboli di Chistoffel del 1' tipo 
// parametri in output :
// chris2(j,l,k) = simboli di Christoffel del 2' tipo di V2 

For ($j = 1; $j <= 2; $j++)
{
For ($l = 1; $l <= 2; $l++)
{
For ($i = 1; $i <= 2; $i++)
{
$chris2[$j][$l][$i] = "(";
For ($k = 1; $k <= 2; $k++)
{
$chris2[$j][$l][$i] = $chris2[$j][$l][$i] . "(" . 
$gcontr[$i][$k] . ")*(" . $chris1[$j][$l][$k] . ")+";

$chris2[$j][$l][$i] = substr($chris2[$j][$l][$i], 0, strlen($chris2[j][$l][$i]) - 1) . ")";




return $chris2;

} // fine calcSimbChris2DiV2()

</script>