set Anonymous function through a series of matrix for "lsqcurvefit" or "lsqnonlin"

3 views (last 30 days)
Hello.
I want to obtain some fitting parameters using "lsqcurvefit" or "lsqnonlin" code.
(Actually, my goal is to obtain Mueller matrix parameters in optics thorugh curve fitting.)
To obtain fitting parameters, I use complex fitting equations.
In my case, to write and set the fitting equation, I have to use some matrix calculation such as bottom.
Although I use a series of matrix, the final equation is (1x1) matrix such as
(1 x n)(n x m)(m x k)....(s x 1) --> (1 x 1) matrix
The problem is that, when I use the matrix to set anonymous function with several variables such as x(1) x(2)... , it doesn't work.
For easy example and explanation, I adopted and slightly changed an example from "lsqcurvefit" and "lsqnonlin page https://kr.mathworks.com/help/optim/ug/lsqnonlin.html#buuhch7-4 and https://kr.mathworks.com/help/optim/ug/lsqcurvefit.html#d123e61633.
clc
clear
x0 = [0.3,0.4];
F = @(x,k) 2 + 2*k - exp(k*x(1)) - exp(k*x(2)); % equation for curve fitting
ydata = linspace(0,0,10);
k = 1:10;
aa = lsqcurvefit(F,x0,k,ydata)
Actually, it works well.
However, when I tried to obtain the equation from
[1x4] * [4x1] --> [1x1]matrix,
it doens't work such as bottom script.
clc
clear
x0 = [0.3,0.4];
F = @(x,k) [2, 2, exp(k*x(1)), exp(k*x(2))] * [1; k; -1; -1] % same as above equation, but from matrix expression
ydata = linspace(0,0,10);
k = 1:10;
aa = lsqcurvefit(F,x0,k,ydata)
In my case,
(1) I have to use several x coefficients, such as x(1) x(2) x(3) and so on, which I want to obtain from the lsqcurvefit.
(2) k have to be vectors [1,2,3,4,5,6,7,8,9,10], that is a input variable.
(3) When I type and express the equation in one line, not using the matrix, the equation becomes too long. So I want to obtain the equation from the matrix.
FInally, my question is that, for curfitting, how do I type the script to set the equation through the matrix??
Please help me.
Thank you

Accepted Answer

Torsten
Torsten on 3 Aug 2022
x0 = [0.3,0.4];
F = @(x,k) arrayfun(@(k)[2, 2, exp(k*x(1)), exp(k*x(2))] * [1; k; -1; -1],k) % same as above equation, but from matrix expression
F = function_handle with value:
@(x,k)arrayfun(@(k)[2,2,exp(k*x(1)),exp(k*x(2))]*[1;k;-1;-1],k)
ydata = linspace(0,0,10);
k = 1:10;
aa = lsqcurvefit(F,x0,k,ydata)
Local minimum possible. lsqcurvefit stopped because the size of the current step is less than the value of the step size tolerance.
aa = 1×2
0.2578 0.2578

More Answers (1)

Walter Roberson
Walter Roberson on 3 Aug 2022
k = 1:10;
row vector of length 10
F = @(x,k) [2, 2, exp(k*x(1)), exp(k*x(2))] * [1; k; -1; -1] % same as above equation, but from matrix expression
k is a row vector of length 10 so the exp() expressions are each length 10. You have two of them and you prepend two elements, so you end up with a 1 x 22 vector on the left side of the * operation.
[1; k; -1; -1]
that asks to create a row of length 1, then a row of length 10, then two more of length 1. That will fail.
If you used k(:) then you would be constructing 13 x 1. Which you cannot * with a 1 x 22
  2 Comments
YEONSOO LIM
YEONSOO LIM on 3 Aug 2022
Thank you for reply.
I under stand that final equatiion become [1x22]*[1;1:10;-1;-1], which is very strange.
Then how can I make the sentance to set the equation through a series of matrix??
In my case, it is not easy to write the equation after calculating all the matrix by myself.
I also try to matlabFunction .
However, it only work well with only one x variation.
I want to use several x coefficient x(1) x(2) x(3) ....
If there is other way, please let me know.
Thank you.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!