Clear Filters
Clear Filters

Optimizing the coefficients of basis functions to fit a curve

16 views (last 30 days)
I have 3 vectors F1, F2, and F3. I want to linearly combine these vectors such that I create a new function W(z)=a*F1+b*F2+c*F3, where a,b,c are just constant coefficients and the 3 vectors are essentially basis functions. These vectors are 1x70 doubles, and are shown plotted below (note that the horizontal axis 'Range' is the 'z'-variable in the equations below).
The problem I am having is finding a way to calculate the coefficients a,b,c so that W=a*F1+b*F2+c*F3 most closely matches a function given by:
Plotted together, W and r are shown below...
What sort of optimization process can I use to build a function W=a*F1+b*F2+c*F3 that fits 'W' to 'r' and returns the coefficients a,b,c used to do so? Please note that F1,F2, and F3 are all 1x70 doubles, and are not explicitly functions of z anymore. They have each individually been evaluated. And so I tried using the curvefitting app with no success because of this. I really just need a systematic way to optimize the coefficients a,b, and c. Is there a built in way to do this?

Accepted Answer

Matt J
Matt J on 29 Aug 2023
Edited: Matt J on 29 Aug 2023
You don't need any special curve fitting tool. Due to the linearity of the model, the coefficients can be found using mldivide, \
coefficients = [F1(:),F2(:),F3(:)]\r(:)
  2 Comments
Image Analyst
Image Analyst on 29 Aug 2023
Edited: Image Analyst on 29 Aug 2023
Good answer Matt. I wish this trick was better known.
Shouldn't W be in the denominator instead of r?
Also note there is a similar version mrdivide, / that uses forward slash. Since I can never remember which to use I always have to look at the documentation and see which one best matches up with my data and coefficients. If you have a good mnemonic to avoid having to check the documentation, I'd like to hear it.
help mldivide
\ Backslash or left matrix divide. A\B is the matrix division of A into B, which is roughly the same as INV(A)*B , except it is computed in a different way. If A is an N-by-N matrix and B is a column vector with N components, or a matrix with several such columns, then X = A\B is the solution to the equation A*X = B. A warning message is printed if A is badly scaled or nearly singular. A\EYE(SIZE(A)) produces the inverse of A. If A is an M-by-N rectangular matrix with M~=N and B is a column vector with M components, or a matrix with several such columns, then X = A\B is the solution in the least squares sense to the under- or overdetermined system of equations A*X = B. The effective rank, K, of A is determined from the QR decomposition with pivoting. A solution X is computed which has at most K nonzero components per column. If K < N this will usually not be the same solution as PINV(A)*B. A\EYE(SIZE(A)) produces a generalized inverse of A. X = MLDIVIDE(A,B) is called for the syntax 'A \ B' when A or B is an object. See MATLAB Operators and Special Characters for more details. See also LDIVIDE, RDIVIDE, MRDIVIDE, PAGEMLDIVIDE. Documentation for mldivide doc mldivide Other uses of mldivide codistributed/mldivide laurpoly/mldivide comm/mldivide StaticModel/mldivide distributed/mldivide sym/mldivide duration/mldivide symbolic/mldivide DynamicSystem/mldivide tall/mldivide gpuArray/mldivide timeseries/mldivide LagOp/mldivide tsdata.datametadata/mldivide
help mrdivide
/ Right matrix divide. B/A is the matrix division of A into B, which is roughly the same as B*INV(A) , except it is computed in a different way. More precisely, B/A = (A'\B')'. See MLDIVIDE for details. C = MRDIVIDE(B,A) is called for the syntax 'B / A' when B or A is an object. See MATLAB Operators and Special Characters for more details. See also MLDIVIDE, RDIVIDE, LDIVIDE, PAGEMRDIVIDE. Documentation for mrdivide doc mrdivide Other uses of mrdivide codistributed/mrdivide laurpoly/mrdivide distributed/mrdivide se2/mrdivide dlarray/mrdivide StaticModel/mrdivide duration/mrdivide sym/mrdivide DynamicSystem/mrdivide symbolic/mrdivide embedded.fi/mrdivide tall/mrdivide gpuArray/mrdivide timeseries/mrdivide LagOp/mrdivide tsdata.datametadata/mrdivide
Matt J
Matt J on 29 Aug 2023
Shouldn't W be in the denominator instead of r?
W=[F1(:),F2(:),F3(:)] is in the denominator. I jut didn't create a W variable explicitly.

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!