Cody

# Problem 44507. Curve fitting (linear functions) & function handles

Solution 1691497

Submitted on 13 Dec 2018 by Binbin Qi
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
x = 1:100; y_correct = 2:2:200; [fh, pars] = generateFit(x, y_correct); y = fh(pars, x); assert( isequal(y,y_correct) )

2   Pass
x = 1:100; y_correct = 101:200; [fh, pars] = generateFit(x, y_correct); y = fh(pars, x); assert( isequal(y,y_correct) )

3   Pass
x = 1:100; y_correct = 102:2:300; [fh, pars] = generateFit(x, y_correct); y = fh(pars, x); assert( isequal(y,y_correct) )

4   Pass
x = 1:100; y_correct = 102:2:300; [fh, pars] = generateFit(x, y_correct); els = 10+randi(30) : 60+randi(30); y = fh(pars, x(els)); assert( isequal(y,y_correct(els)) ) y = fh(pars, x+100); assert( isequal(y,302:2:500) )

5   Pass
x = 1000:-1:500; y_correct = 0:500; [fh, pars] = generateFit(x, y_correct); y = fh(pars, x); assert( isequal(y,y_correct) )

6   Pass
x = 1000:-1:500; y_correct = -500:2:500; [fh, pars] = generateFit(x, y_correct); y = fh(pars, x); assert( isequal(y,y_correct) )

7   Pass
for i = 1 : 20 x = -randi(1000) : randi(10) : randi(1000); m = randi(20)-10; c = randi(20)-10; y_correct = round(exp(log(c) + log(x))) - m^1 * (x(1))^0; [fh, pars] = generateFit(x, y_correct); y = fh(pars, x); assert( isequal(y,y_correct) ) end;

8   Pass
% "Gradient and intercept" x1 = 1:100; y1_correct = 102:2:300; [fh1, pars1] = generateFit(x1, y1_correct); % "Another gradient and intercept" x2 = 1000:-1:500; y2_correct = 0:500; [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) )

9   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 = 1:1000; y_correct = flip(1:1000); [fh, pars] = generateFit(x, y_correct); pw = whos('pars') assert( pw.bytes < 100 , 'Parameter variable is too big.') y = fh(pars, x); assert( isequal(y,y_correct) )

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