Split the datafit example and introduce images 70/9870/2
Sylvestre Ledru [Thu, 29 Nov 2012 14:01:31 +0000 (15:01 +0100)]
Change-Id: I838482e319d65b6fd0683e5cbd0496f4c97dceb5

scilab/modules/optimization/help/en_US/datafit.xml

index fea9afd..2780f07 100644 (file)
 function y=FF(x,p)
   y=p(1)*(x-p(2))+p(3)*x.*x
 endfunction
-X=[];Y=[];
+
+X=[];
+Y=[];
 pg=[34;12;14] //parameter used to generate data
 for x=0:.1:3
   Y=[Y,FF(x,pg)+100*(rand()-.5)];
@@ -246,6 +248,38 @@ scf(0);clf()
 plot2d(X,FF(X,pg),5) //the curve without noise
 plot2d(X,Y,-1)  // the noisy data
 plot2d(X,FF(X,p),12) //the solution
+ ]]></programlisting>
+        <scilab:image>
+            function y=FF(x,p)
+            y=p(1)*(x-p(2))+p(3)*x.*x
+            endfunction
+            
+            X=[];Y=[];
+            pg=[34;12;14]
+            for x=0:.1:3
+            Y=[Y,FF(x,pg)+100*(rand()-.5)];
+            X=[X,x];
+            end
+            Z=[Y;X];
+            
+            function e=G(p,z),
+            y=z(1),x=z(2);
+            e=y-FF(x,p),
+            endfunction
+            
+            p0=[3;5;10]        
+            [p,err]=datafit(G,Z,p0);
+            
+            scf(0);clf()
+            plot2d(X,FF(X,pg),5)
+            plot2d(X,Y,-1)
+            plot2d(X,FF(X,p),12)
+        </scilab:image>
+        <programlisting role="example"><![CDATA[ 
+//generate the data
+function y=FF(x,p)
+  y=p(1)*(x-p(2))+p(3)*x.*x
+endfunction
 
 //the gradient of the criterion function
 function s=DG(p,z),
@@ -253,12 +287,84 @@ function s=DG(p,z),
   s=-[x-b,-a,x*x]
 endfunction
 
+function e=G(p,z),
+  y=z(1),x=z(2);
+  e=y-FF(x,p),
+endfunction
+
+X=[];Y=[];
+pg=[34;12;14]
+for x=0:.1:3
+  Y=[Y,FF(x,pg)+100*(rand()-.5)];
+  X=[X,x];
+end
+Z=[Y;X];
+
+p0=[3;5;10]    
 [p,err]=datafit(G,DG,Z,p0);
 scf(1);
 clf()
 plot2d(X,FF(X,pg),5) //the curve without noise
 plot2d(X,Y,-1)  // the noisy data
 plot2d(X,FF(X,p),12) //the solution
+ ]]></programlisting>
+        <scilab:image>
+            function y=FF(x,p)
+            y=p(1)*(x-p(2))+p(3)*x.*x
+            endfunction
+            
+            function s=DG(p,z),
+            a=p(1),b=p(2),c=p(3),y=z(1),x=z(2),
+            s=-[x-b,-a,x*x]
+            endfunction
+            
+            function e=G(p,z),
+            y=z(1),x=z(2);
+            e=y-FF(x,p),
+            endfunction
+            
+            X=[];Y=[];
+            pg=[34;12;14]
+            for x=0:.1:3
+            Y=[Y,FF(x,pg)+100*(rand()-.5)];
+            X=[X,x];
+            end
+            Z=[Y;X];
+            
+            p0=[3;5;10]        
+            [p,err]=datafit(G,DG,Z,p0);
+            scf(1);
+            clf()
+            plot2d(X,FF(X,pg),5)
+            plot2d(X,Y,-1)
+            plot2d(X,FF(X,p),12)
+        </scilab:image>
+        <programlisting role="example"><![CDATA[
+//generate the data
+function y=FF(x,p)
+  y=p(1)*(x-p(2))+p(3)*x.*x
+endfunction
+
+//the gradient of the criterion function
+function s=DG(p,z),
+  a=p(1),b=p(2),c=p(3),y=z(1),x=z(2),
+  s=-[x-b,-a,x*x]
+endfunction
+
+function e=G(p,z),
+  y=z(1),x=z(2);
+  e=y-FF(x,p),
+endfunction
+
+X=[];Y=[];
+pg=[34;12;14]
+for x=0:.1:3
+  Y=[Y,FF(x,pg)+100*(rand()-.5)];
+  X=[X,x];
+end
+Z=[Y;X];
+
+p0=[3;5;10]    
 
 // Add some bounds on the estimate of the parameters
 // We want positive estimation (the result will not change)
@@ -269,6 +375,38 @@ plot2d(X,FF(X,pg),5) //the curve without noise
 plot2d(X,Y,-1)  // the noisy data
 plot2d(X,FF(X,p),12) //the solution
  ]]></programlisting>
+        <scilab:image>
+            function y=FF(x,p)
+            y=p(1)*(x-p(2))+p(3)*x.*x
+            endfunction
+            
+            function s=DG(p,z),
+            a=p(1),b=p(2),c=p(3),y=z(1),x=z(2),
+            s=-[x-b,-a,x*x]
+            endfunction
+            
+            function e=G(p,z),
+            y=z(1),x=z(2);
+            e=y-FF(x,p),
+            endfunction
+            
+            X=[];Y=[];
+            pg=[34;12;14]
+            for x=0:.1:3
+            Y=[Y,FF(x,pg)+100*(rand()-.5)];
+            X=[X,x];
+            end
+            Z=[Y;X];
+            
+            p0=[3;5;10]        
+            
+            [p,err]=datafit(G,DG,Z,'b',[0;0;0],[%inf;%inf;%inf],p0,algo='gc');
+            scf(1);
+            clf()
+            plot2d(X,FF(X,pg),5)
+            plot2d(X,Y,-1)
+            plot2d(X,FF(X,p),12)
+        </scilab:image>
     </refsection>
     <refsection role="see also">
         <title>See Also</title>