]> SALOME platform Git repositories - modules/adao.git/blob - doc/fr/scripts/simple_NonLinearLeastSquares.py
Salome HOME
Minor documentation and code review corrections (39)
[modules/adao.git] / doc / fr / scripts / simple_NonLinearLeastSquares.py
1 # -*- coding: utf-8 -*-
2 #
3 from numpy import array, ravel
4 def QuadFunction( coefficients ):
5     """
6     Simulation quadratique aux points x : y = a x^2 + b x + c
7     """
8     a, b, c = list(ravel(coefficients))
9     x_points = (-5, 0, 1, 3, 10)
10     y_points = []
11     for x in x_points:
12         y_points.append( a*x*x + b*x + c )
13     return array(y_points)
14 #
15 Xb   = array([1., 1., 1.])
16 Yobs = array([57, 2, 3, 17, 192])
17 #
18 print("Résolution du problème de calage")
19 print("--------------------------------")
20 print("")
21 from adao import adaoBuilder
22 case = adaoBuilder.New()
23 case.setBackground( Vector = Xb, Stored=True )
24 case.setObservation( Vector = Yobs, Stored=True )
25 case.setObservationError( ScalarSparseMatrix = 1. )
26 case.setObservationOperator( OneFunction = QuadFunction )
27 case.setAlgorithmParameters(
28     Algorithm='NonLinearLeastSquares',
29     Parameters={
30         'MaximumNumberOfIterations': 100,
31         'StoreSupplementaryCalculations': [
32             'CurrentState',
33             ],
34         },
35     )
36 case.setObserver(
37     Info="  État intermédiaire en itération courante :",
38     Template='ValuePrinter',
39     Variable='CurrentState',
40     )
41 case.execute()
42 print("")
43 #
44 #-------------------------------------------------------------------------------
45 #
46 print("Calage de %i coefficients pour une forme quadratique 1D sur %i mesures"%(
47     len(case.get('Background')),
48     len(case.get('Observation')),
49     ))
50 print("--------------------------------------------------------------------")
51 print("")
52 print("Vecteur d'observation.............:", ravel(case.get('Observation')))
53 print("État d'ébauche a priori...........:", ravel(case.get('Background')))
54 print("")
55 print("Coefficients théoriques attendus..:", ravel((2,-1,2)))
56 print("")
57 print("Nombre d'itérations...............:", len(case.get('CurrentState')))
58 print("Nombre de simulations.............:", len(case.get('CurrentState'))*4)
59 print("Coefficients résultants du calage.:", ravel(case.get('Analysis')[-1]))
60 #
61 Xa = case.get('Analysis')[-1]
62 import matplotlib.pyplot as plt
63 plt.rcParams['figure.figsize'] = (10, 4)
64 #
65 plt.figure()
66 plt.plot((-5,0,1,3,10),QuadFunction(Xb),'b-',label="Simulation à l'ébauche")
67 plt.plot((-5,0,1,3,10),Yobs,            'kX',label='Observation',markersize=10)
68 plt.plot((-5,0,1,3,10),QuadFunction(Xa),'r-',label="Simulation à l'optimum")
69 plt.legend()
70 plt.title('Calage de coefficients', fontweight='bold')
71 plt.xlabel('Coordonnée arbitraire')
72 plt.ylabel('Observations')
73 plt.savefig("simple_NonLinearLeastSquares.png")