Problem 44507. Curve fitting (linear functions) & function handles
In this problem you are provided some raw data. You need to find a way of summarising the data with just a few parameters, so that it can be reconstructed. You then need to provide a handle to your own (generic) custom function that will indeed reproduce the raw data when provided with the parameters that you determine.
Consider a linear function y = m x + c. Let's say x is a vector of uniformly spaced numbers, such as 1:100. The function operates on the data to produce, say, y = 2:2:200. You are provided with both the vector x and the vector y. The parameters m and c are scalars; in this example m is 2 and c is zero.
So here you should output two things:
- the handle to a generic function that implements a linear function (i.e. of the form y = m x + c) taking two inputs, namely 1 a set of parameters and 2 the vector x, and outputting the corresponding vector y; and
- a set of parameter values (m and c) wrapped into a single MATLAB variable (of any data type).
As the parameters will be used in your own function, the data type will be set by you.
So, for the above example, you could return a function handle @myFunc that you have defined, along with the variable param that has two fields such that param.amplification = 2 and param.verticalShift = NaN.
Or, if you have defined your function differently, then you could return the function handle @myFn along with a cell array variable prms that has four elements such that prms{1}='no', prms{2}=NaN, prms{3} = 'yes' and prms{4} = 2.
And so on.
Note: all of the relevant numbers ( m, c and the elements of x and y) are integers (i.e. whole numbers, not decimals or fractions), even though they have been implicitly specified as being of type double.
- See also Problem 44508. Curve fitting (non-linear functions) & function handles — more difficult.
Solution Stats
Problem Comments
-
2 Comments
It might be nice to replace assert(isequal(y,y_correct)) by something along the lines of assert(max(abs(y-y_correct))<1e-9) to allow for roundoff errors.
Hello, Tim. Thank-you for your suggestion. Although it hadn't been mentioned, _all_ of the relevant numbers (x(i), y(i), m and c) are integers, so I didn't expect rounding would be a problem. [Certainly it wasn't in my reference code.] In any case, the secondary application I had in mind was lossless compression, so I am not keen to change the assertions in this particular problem (although I agree that often, elsewhere, it is indeed good to implement assertions as in your snippet). I will instead add a note to the Problem Statement. —David
Solution Comments
Show commentsProblem Recent Solvers9
Suggested Problems
-
The Hitchhiker's Guide to MATLAB
3301 Solvers
-
463 Solvers
-
503 Solvers
-
96 Solvers
-
13918 Solvers
More from this Author32
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!