Les Snippets

Connexion

INTERPOLATION POLYNOMIALE

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 07/02/2008 00:28:34 et initié par us_30 [Liste]
Date de mise à jour : 07/02/2008 23:55:57
Vue : 7689
Catégorie(s) : Maths
Langages dispo pour ce code :
- VB6, VBA
- ObjectiveCaml
- PHP 5



Langage : VB6 , VBA
Date ajout : 07/02/2008
Posté par us_30 [Liste]
 
Function Interpoly(x() As Double, y() As Double, Xp As Double) As Double 
'   INTERPOLATION POLYNOMIALE de même degré que le nb de points données (x,y) 
'   par la methode de Aitken. Renvoi yp pour xp donné quelconque. 
 
'   Paramètres 
    Dim n As Long, j As Long, i As Long 
 
'   Algo 
    n = UBound(y) 
    For j = 1 To n - 1 
        For i = j + 1 To n 
            y(i) = (y(j) * (x(i) - Xp) - y(i) * (x(j) - Xp)) / (x(i) - x(j)) 
        Next i 
    Next j 
 
'   Renvoi 
    Interpoly = y(n) 
 
End Function

Sub Test() ' TEST (avec l'équation particulière y=x^2) ' Paramètres Dim ValeurX() As Double Dim ValeurY() As Double Dim Tempo1() As String Dim Tempo2() As String Dim Nb As Long, t As Long Dim Xp As Double ' Valeurs connues Tempo1 = Split("1 2 4 8") ' les Xi Tempo2 = Split("1 4 16 64") ' les Yi ' Conversion des valeurs en numériques Nb = UBound(Tempo1) ReDim ValeurX(Nb), ValeurY(Nb) For t = 1 To Nb ValeurX(t) = CDbl(Tempo1(t - 1)) ValeurY(t) = CDbl(Tempo2(t - 1)) Next t ' Valeur interpolée pour xp = 10 Xp = 10 MsgBox "pour x=" & Xp & " y=" & Interpoly(ValeurX, ValeurY, Xp) End Sub
Remarque :
Simple, rapide et trés efficace.
Langage : ObjectiveCaml
Date ajout : 09/02/2008
Posté par coucou747 [Liste]

type poly = Zero | Un | X | Add of poly * poly | Mult of poly * poly | Scal of poly * float;;
let rec eval x = function
    | Zero -> 0.
    | Un -> 1.
    | X -> x
    | Add(a,b) -> (eval x a) +. (eval x b)
    | Mult(a,b) -> (eval x a) *. (eval x b)
    | Scal(a,b) -> (eval x a) *. b
;;

type point = {x: float; y: float};;
let new_point px py = {x=px;y=py;};;
let rec interpolate=function
    | ([], j) -> Un
    | (hd::lt, j) ->
        if j==0 then
            interpolate (lt, j-1)
        else
            Mult(
                Add(X,Scal(Un,0. -. hd.x)),
                interpolate (lt, j-1)
            );;
let reponseInterpolate liste =
    let rec f i = function
        | [] -> Zero
        | hd::lt -> let p=interpolate (liste, i) in
            Add(
                Scal ( p, hd.y /. (eval hd.x p) ),
                f (i+1) lt
            )
    in f 0 liste;;


Remarque :
pour tester on peut faire par exemple :
let p=reponseInterpolate [ {x=1.;y=1.}; {x=2.;y=2.}; {x=3.;y=1.} ];;

eval 1. p;;
eval 2. p;;
eval 3. p;;
Langage : PHP 5
Date ajout : 16/04/2008
Posté par Eregon [Liste]
<?php
//Structure pour les points du graph
class Point
{
    public $x;
    public $y;
    function __construct($x, $y)
    {
        $this->x = $x;
        $this->y = $y;
    }
}
//Affichage des polynomes
class Number
{
    public static function show_sign($n)
    {
        return ($n >= 0) ? '+ ' : '- ';
    }
    public static function show($n, $var = '', $pow = 1, $first_element = false)
    {
        if($pow > 1)
            $var .= '<sup>'.$pow.'</sup>';
        elseif($pow == 0)
            $var = '';
        $var .= ' ';
        if($n == 0)
            return '';
        elseif($pow != 0 && ($n == 1 || $n == -1))
            $s = self::show_sign($n);
        else//if($n != 0)
            $s = self::show_sign($n).abs($n);
        if($first_element && $n >= 0)
            return ($n==1) ? $var : $n.$var;
        return $s.$var;
    }
}
//Structure et fonctions des polynomes
class Polynom implements ArrayAccess
{
    private $coef = Array();//coeficients
    private $degree;
    function __construct($coeficients)
    {
        if(!is_array($coeficients))
            $coeficients = func_get_args();
        $this->degree = count($coeficients)-1;
        for($d=0; $d<=$this->degree; $d++)
            $this->coef[$d] = $coeficients[$this->degree-$d];
    }
    function __toString()
    {
        $return = Number::show($this[$this->degree], 'x', $this->degree, true);
        for($d=$this->degree-1; $d>=0; $d--)
            $return .= Number::show($this[$d], 'x', $d);
        return $return;
    }
    //ArrayAccess
    public function offsetGet($offset)
        { return $this->coef[$offset]; }
    public function offsetSet($offset, $value)
        { $this->coef[$offset] = $value; }
    public function offsetUnset($offset) {}
    public function offsetExists($offset)
        { return array_key_exists($offset, $this->coef); }
    public function add(Polynom $p)
    {
        $max = ($this->degree > $p->degree) ? $this : $p;
        $min = ($this->degree > $p->degree) ? $p : $this;
        for($d=0; $d<=$min->degree; $d++)
            $max[$d] += $min[$d];
        return $max;
    }
    public function product(Polynom $p)
    {
        $result = new Polynom(array_fill(0, $this->degree+$p->degree+1, 0));
        for($d=0; $d<=$this->degree; $d++)
        {
            for($e=0; $e<=$p->degree; $e++)
                $result[$d+$e] += $this[$d]*$p[$e];
        }
        return $result;
    }
}
class Interpolation
{
    public static function Lagrange($p)
    {//Interpolation polynomiale de Lagrange(Array(Point) $points)
        $n = count($p);
        $result = new Polynom(0);
        for($k=0; $k<$n; $k++)
        {
            $t = new Polynom($p[$k]->y);
            for($i=0; $i<$n; $i++)
            {
                if($i != $k)
                {
                    $poly[$k][$i] = new Polynom(
                    1/($p[$k]->x - $p[$i]->x) ,
                    -$p[$i]->x/($p[$k]->x-$p[$i]->x) );
                    $t = $t->product($poly[$k][$i]);
                }
            }
            $result = $result->add($t);
        }
        return $result;
    }
}
//TEST\\
//Points : (−9, 5), (−4, 2), (−1, −2), (7, 9)
$a = new Point(-9, 5);
$b = new Point(-4, 2);
$c = new Point(-1, -2);
$d = new Point(7, 9);
echo $polynom = Interpolation::Lagrange(Array($a, $b, $c, $d));
?>

Remarque :
Requiert la classe ArrayAccess de la SPL (utilisée pour plus de simplicité avec les polynômes)

Snippets en rapport avec : Points, Interpolation, Polynomiale



Codes sources en rapport avec : Points, Interpolation, Polynomiale

{Flash} INTERFACE VIP MEMBER (MC TWEEN MOTION)
Cette source est reliée à ma source sur le forum Graphisme présente à cette adresse : http://www.gra...

{Javascript / DHTML} STRIKER BASEBALL
Un petit code en javascript (et html pour le support) que je me suis amusé à faire pour le groupe de...

{Delphi} ENCORE LOUPÉ
pour reprendre le thème de la loupe, voilà ma version. qu'est-ce qu'il y a de nouveau avec cette so...

{Flash} DÉPLACEMENT OBJET
Ici se trouve le code pour déplacer un objet sur la scène, sans interpolations. Vous trouverez un c...

{C / C++ / C++.NET} APPLICATION MATHÉMATIQUE EN C++ ET QT
Cette application mathématiques a été réalisée avec le langage C++ et la QT 2.3.0 . Elle traite : ...

{Delphi} RÉGRESSION POLYNOMIALE
Calcul le meilleurs polynome approximant n points expérimentaux....

{Visual Basic, VB6, VB.NET, VB 2005} ALGORITHME DE PATHFINDING ASTAR
Cet algorithme célèbre est un algorithme de type PathFinding. Il recherche le chemin le plus rap...

{Visual Basic, VB6, VB.NET, VB 2005} GRAPHIQUE EN TEMPS RÉEL (AVEC LISSAGE DES COURBES ET ANTIALIAS)
Bonjour à tous, voilà je cherchais un controle qui me permettrait d'afficher un graphique en temp...

{Visual Basic, VB6, VB.NET, VB 2005} INTÉGRER DES SCORES À VOS JEUX
Très simplement, ce Module sert à écrire dans le registre 5 joueurs et leurs scores. Avec modificat...

{Flash} UTILISATION DES INTERPOLATIONS DE FORME ET DES REPÈRES
C’est juste un exemple de ce que l’on peut produire avec les interpolations de forme Le fichier sou...