How do I create a series of variables (V0, V1, ...Vn) which act as independent variables in conjunction with a series of sums?

2 visualizaciones (últimos 30 días)
Basically, I have a symsum which I intend on feeding into a fittype() function.
The fittype is of the form (V0/2) + (V1/2)*cos(1*x) + (V2/2)*cos(2*x) +... (Vn/2)*cos(n*x) where each Vn is an independent variable in a non-linear least squares.
Those Vn variables are going into a matrix so the values need to be extractable, right now I'm using coeffvalues(fitresult) or manually getting them out with fitresult.V0, V1 etc (that could be a question on its own).
I have the sum of cos funcs
syms x n
Func = symsum((1/2)*cos(n*x),n,[1 5])
but I can't figure out how to (1) create an array of variables V0:Vn and outputting them as variables and (2) how to combine those with a symsum output into the fittype given above.

Respuesta aceptada

Chaitanya Mallela
Chaitanya Mallela el 4 de Feb. de 2021
Editada: Chaitanya Mallela el 4 de Feb. de 2021
fittype function accepts character array as input argument but the symsum function gives symbolic variable. To apply fittype to this function you need to split the symsum expression into terms and convert them to character array and generate independent variable Vn as coefficients to fittype function.
syms x n
Func = cell2sym(children(symsum((1/2)*cos(n*x),n,[1 5])));
g = fittype(arrayfun(@char,Func,'UniformOutput',false),'coefficients',arrayfun(@char,sym('V',[1,5]),'UniformOutput',false))
  1 comentario
Domantas Laurinavicius
Domantas Laurinavicius el 4 de Feb. de 2021
Editada: Domantas Laurinavicius el 4 de Feb. de 2021
That works wonderfully, thank you very much! Now, Why is the output all out of order?
g =
Linear model:
g(V1,V2,V3,V4,V5,x) = V1*cos(2*x)/2 + V2*cos(3*x)/2 + V3*cos(4*x)/2 + V4*cos(x)/2 + V5*1/2
I would've expected V1 to be paired with cos(1*x) and V2 with cos(2*x) but matlab seems to preferentially place the lowest index term last.
Regardless, when I put this equation as the fittype(), I get the message:
Warning: The given fit options specify a NonlinearLeastSquares fit but the model specifies a LinearLeastSquares fit.
Any hints there?
Thanks again!
Edit: thought the full context would help. I'm inputting that linear model to this set of cftool code:
ft = fittype( '(V0/2) + (V1/2)*cos(1*x) + (V2/2)*cos(2*x) + (V3/2)*cos(3*x) + (V4/2)*cos(4*x) + (V5/2)*cos(5*x);', 'independent', 'x', 'dependent', 'y' )
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.421761282626275 0.915735525189067 0.792207329559554 0.959492426392903 0.655740699156587 0.0357116785741896];
% Fit model to data
[fitresult, gof] = fit( xData, yData, ft, opts );

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Linear and Nonlinear Regression en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by