Cody

# Problem 44508. Curve fitting (non-linear functions) & function handles

Solution 2525450

Submitted on 12 Jun 2020
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Fail
for i = 1 : 5 x = uint64(randi(10):60-randi(40)); m = uint64(2); c = uint64(3); y_correct = m.^x + c; [fh, pars] = generateFit(x, y_correct); y = fh(pars, x); assert( isequal(y,y_correct) ) end;

Undefined function 'generateFit' for input arguments of type 'uint64'. Error in Test1 (line 6) [fh, pars] = generateFit(x, y_correct);

2   Fail
for i = 1 : 5 x = uint64(randi(10):40-randi(10)); m = uint64(3); c = uint64(2); y_correct = m.^x + c; [fh, pars] = generateFit(x, y_correct); y = fh(pars, x); assert( isequal(y,y_correct) ) end;

Undefined function 'generateFit' for input arguments of type 'uint64'. Error in Test2 (line 6) [fh, pars] = generateFit(x, y_correct);

3   Fail
for i = 1 : 5 x = int64( randi(5):25-randi(5) ); m = int64( randi(10) - 5 ); c = int64( 10000 - randi(20000) ); y_correct = m.^x + c; [fh, pars] = generateFit(x, y_correct); y = fh(pars, x); assert( isequal(y,y_correct) ) end;

Undefined function 'generateFit' for input arguments of type 'int64'. Error in Test3 (line 6) [fh, pars] = generateFit(x, y_correct);

4   Fail
for i = 1 : 5 x = int64( +25-randi(5) : -randi(5) : randi(5) ); m = int64( randi(10) - 5 ); c = int64( 10000 - randi(20000) ); y_correct = m.^x + c; [fh, pars] = generateFit(x, y_correct); y = fh(pars, x); assert( isequal(y,y_correct) ) end;

Undefined function 'generateFit' for input arguments of type 'int64'. Error in Test4 (line 6) [fh, pars] = generateFit(x, y_correct);

5   Fail
for i = 1 : 5 x = int64( randi(5) : randi(5) : 25-randi(5) ); els = randperm( length(x) ); x = x(els); m = int64( randi(10)-5 ); c = int64( 10000 - randi(20000) ); y_correct = m.^x + c; [fh, pars] = generateFit(x, y_correct); y = fh(pars, x); assert( isequal(y,y_correct) ) end;

Undefined function 'generateFit' for input arguments of type 'int64'. Error in Test5 (line 8) [fh, pars] = generateFit(x, y_correct);

6   Fail
for i = 1 : 5 x = int64( randi(5) : randi(5) : 25-randi(5) ); x = repmat(x, [1,50]); els = randperm( length(x) ); x = x(els); m = int64( randi(10)-5 ); c = int64( 10000 - randi(20000) ); y_correct = m.^x + c; [fh, pars] = generateFit(x, y_correct); y = fh(pars, x); assert( isequal(y,y_correct) ) end;

Undefined function 'generateFit' for input arguments of type 'int64'. Error in Test6 (line 9) [fh, pars] = generateFit(x, y_correct);

7   Fail
for i = 1 : 5 x = int64( 1+randi(4) : 2*randi(2)-1 : 25-randi(4) ); % Must be odd step to ensure both odd and even values of x here. els = randperm( length(x) ); x = x(els); m = int64( randi(10)-5 ); c = int64( 10000 - randi(20000) ); y_correct = m.^x + c; [fh, pars] = generateFit(x, y_correct); y = fh(pars, x); assert( isequal(y,y_correct) , 'Failed test 7a' ) idx = [1+randi(2) : length(x)-randi(2)]; z = x(idx); y = fh(pars, z); assert( isequal(y, y_correct(idx)) , 'Failed test 7b' ) z = x-1; y = fh(pars, z); assert( isequal(y, m.^z + c) , 'Failed test 7c' ) z = x(idx)+1; y = fh(pars, z); assert( isequal(y, m.^z + c) , 'Failed test 7d' ) end;

Undefined function 'generateFit' for input arguments of type 'int64'. Error in Test7 (line 8) [fh, pars] = generateFit(x, y_correct);

8   Fail
for i = 1 : 5 % Hard-coded m and c (1). x1 = int64(randi(10):60-randi(40)); m = int64(2); c = int64(3); y1_correct = m.^x1 + c; [fh1, pars1] = generateFit(x1, y1_correct); % Hard-coded m and c (2). x2 = int64(randi(10):60-randi(40)); m = int64(3); c = int64(2); y2_correct = m.^x2 + c; [fh2, pars2] = generateFit(x2, y2_correct); % According to the Problem Statament, fh1 and fh2 should be interchangeable. y1 = fh2(pars1, x1); assert( isequal(y1,y1_correct) ) y2 = fh1(pars2, x2); assert( isequal(y2,y2_correct) ) end;

Undefined function 'generateFit' for input arguments of type 'int64'. Error in Test8 (line 7) [fh1, pars1] = generateFit(x1, y1_correct);

9   Fail
for i = 1 : 5 x = int64( randi(3):13-randi(3) ); m = int64( randi(60) - 30 ); c = int64( 10000 - randi(20000) ); y_correct = m.^x + c; [fh, pars] = generateFit(x, y_correct); y = fh(pars, x); assert( isequal(y,y_correct) ) end;

Undefined function 'generateFit' for input arguments of type 'int64'. Error in Test9 (line 6) [fh, pars] = generateFit(x, y_correct);

10   Fail
generateFit( uint32([1:10]), uint32(7.^[1:10] - 7) ); tic for i = 1 : 10 x = uint64(randi(10):25-randi(10)); x = repmat(x, [1,100]); m = uint64( randi(5) ); c = uint64( randi(1000) ); y_correct = m.^x + c; for j = 1 : 200 [fh, pars] = generateFit(x, y_correct); y = fh(pars, x); end; assert( isequal(y,y_correct) ) end; t = toc assert( t < 2 ) % Seconds.

Undefined function 'generateFit' for input arguments of type 'uint32'. Error in Test10 (line 1) generateFit( uint32([1:10]), uint32(7.^[1:10] - 7) );

11   Fail
% Finally, check that the user is sending a small number of parameters to their % custom function (to be called via the function handle), and not simply sending % the entire vector y. x = uint64(randi(10):60-randi(40)); m = uint64(2); c = uint64(3); y_correct = m.^x + c; [fh, pars] = generateFit(x, y_correct); y = fh(pars, x); assert( isequal(y,y_correct) ) pw = whos('pars') assert( pw.bytes < 100 , 'Parameter variable is too big.')

Undefined function 'generateFit' for input arguments of type 'uint64'. Error in Test11 (line 8) [fh, pars] = generateFit(x, y_correct);