Cody

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

Solution 1432348

Submitted on 2 Feb 2018 by Tim
• Size: 102
• This is the leading solution.
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   Pass
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;

2   Pass
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;

3   Pass
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;

4   Pass
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;

5   Pass
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;

6   Pass
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;

7   Pass
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;

8   Pass
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;

9   Pass
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;

10   Pass
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.

t = 1.9424

11   Pass
% 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.')

pw = struct with fields: name: 'pars' size: [1 2] bytes: 16 class: 'double' global: 0 sparse: 0 complex: 0 nesting: [1×1 struct] persistent: 0