// <-- ENGLISH IMPOSED -->
// 1. Test with a scalar function
function y = myfunction (x)
- y = x*x;
+ y = x*x;
endfunction
x = 1.0;
expected = 2.0;
assert_checkalmostequal ( Hcomputed , Hexpected , 1.e-11 );
// 2. Test with a vector function
function y = myfunction2 (x)
- y = x(1)*x(1) + x(2)+ x(1)*x(2);
+ y = x(1)*x(1) + x(2)+ x(1)*x(2);
endfunction
-x = [1.0
+x = [1.0
2.0];
expected = [4.0 2.0];
// 2.1 With default parameters
Jexpected = [4.0 2.0];
Hexpected = [2.0 1.0
1.0 0.0];
-[Jcomputed , Hcomputed] = derivative(myfunction2 , x , H_form='hypermat');
+[Jcomputed , Hcomputed] = derivative(myfunction2 , x , H_form="hypermat");
WARNING: Feature derivative is obsolete.
Please use numderivative instead.
This feature will be permanently removed in Scilab 6.0
assert_checkalmostequal ( Hcomputed , Hexpected , %eps );
// 3.3 Test H_form='hypermat'
Jexpected = [4.0 2.0];
-Hexpected = [2.0 1.0
+Hexpected = [2.0 1.0
1.0 0.0];
-[Jcomputed , Hcomputed] = derivative(myfunction2 , x , H_form='hypermat');
+[Jcomputed , Hcomputed] = derivative(myfunction2 , x , H_form="hypermat");
WARNING: Feature derivative is obsolete.
Please use numderivative instead.
This feature will be permanently removed in Scilab 6.0
// not taken into account, therefore testing the feature "h is used correctly".
myn = 1.e5;
function y = myfunction3 (x)
- y = x^(2/myn);
+ y = x^(2/myn);
endfunction
x = 1.0;
h = 6.055454e-006;
// <-- JVM NOT MANDATORY -->
// 1. Test with a scalar argument
function y = myfunction (x)
- y = x*x;
+ y = x*x;
endfunction
x = 1.0;
expected = 2.0;
assert_checkalmostequal ( Hcomputed , Hexpected , 1.e-11 );
// 2. Test with a vector argument
function y = myfunction2 (x)
- y = x(1)*x(1) + x(2) + x(1)*x(2);
+ y = x(1)*x(1) + x(2) + x(1)*x(2);
endfunction
x = [1.0; 2.0];
Jexpected = [4. 2.];
// 3.2 Test H_form = "hypermat"
Jexpected = [4.0 2.0];
Hexpected = [2.0 1.0
- 1.0 0.0];
+1.0 0.0];
[Jcomputed, Hcomputed] = numderivative(myfunction2, x , [], [], "hypermat");
assert_checkalmostequal ( Jcomputed , Jexpected , 1.e-10 );
assert_checkalmostequal ( Hcomputed , Hexpected , %eps );
// 3.3 Test H_form = "blockmat"
Jexpected = [4.0 2.0];
Hexpected = [2.0 1.0
- 1.0 0.0];
+1.0 0.0];
[Jcomputed, Hcomputed] = numderivative(myfunction2, x, [], [], "blockmat");
assert_checkalmostequal ( Jcomputed , Jexpected , 1.e-10 );
assert_checkalmostequal ( Hcomputed , Hexpected , %eps );
// not taken into account, therefore testing the feature "h is used correctly".
myn = 1.e5;
function y = myfunction3 (x)
- y = x^(2/myn);
+ y = x^(2/myn);
endfunction
x = 1.0;
h = 6.055454e-006;
assert_checkalmostequal ( Hcomputed , Hexpected , 1.e-3 );
// 6. Test Q parameter
function y = myfunction4 (x)
- y = x(1)*x(1) + x(2)+ x(1)*x(2);
+ y = x(1)*x(1) + x(2)+ x(1)*x(2);
endfunction
x = [1.; 2.];
Jexpected = [4. 2.];
//
// 7. Test vector output y
function y = myexample(x)
- f1 = sin(x(1)*x(2)) + exp(x(2)*x(3)+x(1));
- f2 = sum(x.^3);
- y = [f1; f2];
+ f1 = sin(x(1)*x(2)) + exp(x(2)*x(3)+x(1));
+ f2 = sum(x.^3);
+ y = [f1; f2];
endfunction
// The exact gradient
function [g1, g2] = exactg(x)
- g1(1) = cos(x(1)*x(2))*x(2) + exp(x(2)*x(3)+x(1));
- g1(2) = cos(x(1)*x(2))*x(1) + exp(x(2)*x(3)+x(1))*x(3);
- g1(3) = exp(x(2)*x(3)+x(1))*x(2);
- g2(1) = 3*x(1)^2;
- g2(2) = 3*x(2)^2;
- g2(3) = 3*x(3)^2;
+ g1(1) = cos(x(1)*x(2))*x(2) + exp(x(2)*x(3)+x(1));
+ g1(2) = cos(x(1)*x(2))*x(1) + exp(x(2)*x(3)+x(1))*x(3);
+ g1(3) = exp(x(2)*x(3)+x(1))*x(2);
+ g2(1) = 3*x(1)^2;
+ g2(2) = 3*x(2)^2;
+ g2(3) = 3*x(3)^2;
endfunction
// The exact Hessian
function [H1, H2] = exactH(x)
- H1(1, 1) = -sin(x(1)*x(2))*x(2)^2 + exp(x(2)*x(3)+x(1));
- H1(1, 2) = cos(x(1)*x(2)) - sin(x(1)*x(2))*x(2)*x(1) + exp(x(2)*x(3)+x(1))*x(3);
- H1(1, 3) = exp(x(2)*x(3)+x(1))*x(2);
- H1(2, 1) = H1(1, 2);
- H1(2, 2) = -sin(x(1)*x(2))*x(1)^2 + exp(x(2)*x(3)+x(1))*x(3)^2;
- H1(2, 3) = exp(x(2)*x(3)+x(1)) + exp(x(2)*x(3)+x(1))*x(3)*x(2);
- H1(3, 1) = H1(1, 3);
- H1(3, 2) = H1(2, 3);
- H1(3, 3) = exp(x(2)*x(3)+x(1))*x(2)^2;
- //
- H2(1, 1) = 6*x(1);
- H2(1, 2) = 0;
- H2(1, 3) = 0;
- H2(2, 1) = H2(1, 2);
- H2(2, 2) = 6*x(2);
- H2(2, 3) = 0;
- H2(3, 1) = H2(1, 3);
- H2(3, 2) = H2(2, 3);
- H2(3, 3) = 6*x(3);
+ H1(1, 1) = -sin(x(1)*x(2))*x(2)^2 + exp(x(2)*x(3)+x(1));
+ H1(1, 2) = cos(x(1)*x(2)) - sin(x(1)*x(2))*x(2)*x(1) + exp(x(2)*x(3)+x(1))*x(3);
+ H1(1, 3) = exp(x(2)*x(3)+x(1))*x(2);
+ H1(2, 1) = H1(1, 2);
+ H1(2, 2) = -sin(x(1)*x(2))*x(1)^2 + exp(x(2)*x(3)+x(1))*x(3)^2;
+ H1(2, 3) = exp(x(2)*x(3)+x(1)) + exp(x(2)*x(3)+x(1))*x(3)*x(2);
+ H1(3, 1) = H1(1, 3);
+ H1(3, 2) = H1(2, 3);
+ H1(3, 3) = exp(x(2)*x(3)+x(1))*x(2)^2;
+ //
+ H2(1, 1) = 6*x(1);
+ H2(1, 2) = 0;
+ H2(1, 3) = 0;
+ H2(2, 1) = H2(1, 2);
+ H2(2, 2) = 6*x(2);
+ H2(2, 3) = 0;
+ H2(3, 1) = H2(1, 3);
+ H2(3, 2) = H2(2, 3);
+ H2(3, 3) = 6*x(3);
endfunction
x=[1; 2; 3];
[g1, g2] = exactg(x);
// 8. Check the number of function evaluations
function y = myFevalFun(x)
global FEVAL
- FEVAL = FEVAL + 1;
+ FEVAL = FEVAL + 1;
y = sum(x.^3);
endfunction
n = 3;
assert_checkerror (instr, lclmsg, [], "numderivative", _("Invalid index.") );
// 9.2 Cannot evaluate the function - List case
function y = myfunction6 (x, p1, p2)
- y = p1*x*x + p2;
+ y = p1*x*x + p2;
endfunction
x = [1.; 2.];
Q = qr(rand(2, 2));
// x = 1.00D+100, numdiff = 5, derivative = 0, numderivative = 10
// x = 1, numdiff = 7, derivative = 11, numderivative = 11
function y = myfunction10 (x)
- y = x^3;
+ y = x^3;
endfunction
function y = g10 (x)
- y = 3*x^2;
+ y = 3*x^2;
endfunction
for x = [10^-100 10^-32 10^32 10^100 1]
- exact = g10 (x);
- g3 = numderivative(myfunction10, x);
- d3 = assert_computedigits ( g3, exact );
- assert_checktrue ( d3 > 9 );
+ exact = g10 (x);
+ g3 = numderivative(myfunction10, x);
+ d3 = assert_computedigits ( g3, exact );
+ assert_checktrue ( d3 > 9 );
end
// 12. Check that numderivative also accepts row vector x
function f = myfunction11(x)
- f = x(1)*x(1) + x(1)*x(2);
+ f = x(1)*x(1) + x(1)*x(2);
endfunction
// 12.1 Check with row x
x = [5 8];
assert_checkalmostequal ( g , 0.5 , 1.e-8 );
// 14.1 Check that numderivative works when f takes extra arguments
function y = f(x, A, p, w)
- y = x'*A*x + p'*x + w;
+ y = x'*A*x + p'*x + w;
endfunction
// with Jacobian and Hessian given
// by J(x) = x'*(A+A')+p' and H(x) = A+A'.
assert_checkalmostequal( H , A+A' );
// 14.2 Same test with a different function
function y = G(x, p)
- f1 = sin(x(1)*x(2)*p) + exp(x(2)*x(3)+x(1));
- f2 = sum(x.^3);
- y = [f1; f2];
+ f1 = sin(x(1)*x(2)*p) + exp(x(2)*x(3)+x(1));
+ f2 = sum(x.^3);
+ y = [f1; f2];
endfunction
x = rand(3, 1);
p = 1;
df2_dx2 = 3*x(2)^2;
df2_dx3 = 3*x(3)^2;
expectedJ = [df1_dx1 df1_dx2 df1_dx3;
- df2_dx1 df2_dx2 df2_dx3 ];
+df2_dx1 df2_dx2 df2_dx3 ];
df1_dx11 = -sin(x(1)*x(2)*p)*x(2)^2*p^2+exp(x(2)*x(3)+x(1));
df1_dx12 = -sin(x(1)*x(2)*p)*x(1)*x(2)*p^2+cos(x(1)*x(2)*p)*p+exp(x(2)*x(3)+x(1))*x(3);
df1_dx13 = exp(x(2)*x(3)+x(1))*x(2);
df2_dx32 = 0;
df2_dx33 = 6*x(3);
expectedH = [df1_dx11 df1_dx12 df1_dx13 df1_dx21 df1_dx22 df1_dx23 df1_dx31 df1_dx32 df1_dx33;
- df2_dx11 df2_dx12 df2_dx13 df2_dx21 df2_dx22 df2_dx23 df2_dx31 df2_dx32 df2_dx33 ];
+df2_dx11 df2_dx12 df2_dx13 df2_dx21 df2_dx22 df2_dx23 df2_dx31 df2_dx32 df2_dx33 ];
[J, H] = numderivative(list(G, p), x);
assert_checkalmostequal( J , expectedJ );
assert_checkalmostequal( H , expectedH , [], 1e-7);
non_scalar_time=msprintf(_("%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n"),"soundsec",1,1,1);
non_scalar_freq=msprintf(_("%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n"),"soundsec",2,1,1);
//Soundsec must be called with real scalar values
-assert_checkerror('soundsec(""test"",1)',non_real_time);
-assert_checkerror('soundsec(%i,1)',non_real_time);
-assert_checkerror('soundsec(1,""test"")',non_real_freq);
-assert_checkerror('soundsec(1,%i)',non_real_freq);
-assert_checkerror('soundsec(0:10,1)',non_scalar_time);
-assert_checkerror('soundsec(1,0:10)',non_scalar_freq);
+assert_checkerror("soundsec(""test"",1)",non_real_time);
+assert_checkerror("soundsec(%i,1)",non_real_time);
+assert_checkerror("soundsec(1,""test"")",non_real_freq);
+assert_checkerror("soundsec(1,%i)",non_real_freq);
+assert_checkerror("soundsec(0:10,1)",non_scalar_time);
+assert_checkerror("soundsec(1,0:10)",non_scalar_freq);
//Soundsec must be called with positive parameters
-assert_checkerror('soundsec(-1,10)',non_positive_time);
-assert_checkerror('soundsec(1,-1)',non_positive_freq);
+assert_checkerror("soundsec(-1,10)",non_positive_time);
+assert_checkerror("soundsec(1,-1)",non_positive_freq);
//==============================================================================
//Check behaviour
//Soundsec returns empty value for a 0 sec sample
samples_t = soundsec(1,5.2); //samples 0 1/5.2 2/5.2 ... 5/5.2
assert_checkalmostequal(samples_t, (0:5)/5.2);
//Check call on non integer frequency and time
-samples_t = soundsec(1.2324,50.75);
+samples_t = soundsec(1.2324,50.75);
//samples 0 1/50.75 ... 50/50.75 51/51.75 ... 62/50.75
//note that 1.2324 > 62/50.75 and 1.2324 <= 63/50.75
assert_checkalmostequal(samples_t, (0:62)/50.75);