lsqcurvefit of a summation function where the parameter to evaluate is the last index of the summation (so an integer)

1 次查看(过去 30 天)
Hi, I'm doing the fit of a function substantially similar to this one: f(x)= sum ( exp(1i * x * jz * c) ) , where: - sum represent the summation over the index jz - x is the variable, - jz = 1,2,...,nz where nz is a parameter to find using the fit, - c is another parameter. The complete function is the product of several series of this type.
In the curve fitting tool I can't enter a summation neither using symsum nor using an iteration. Moreover I can't use nz as a parameter.
Any idea?
Many thanks, Gianluca

采纳的回答

Oleg Komarov
Oleg Komarov 2011-5-30
lsqcurvefit is already MIN oof the SUM of euclidean distances of x from y. So, you don't have to use inlcude a sum in your handle and 1:x(2) but:
fh = @(x,xdata) exp(1i.*x(1).*xdata.*x(2)) .* conj(exp(1i.*x(1).*xdata.*x(2)));
  4 个评论
gianluca messina
gianluca messina 2011-5-31
Sorry, reference in what sense?
I'm fitting an experimental diffraction pattern obtained by xray diffraction from a crystal using the square of an exponential summation (the square because we measure the intensity), and the nz (x(2)) parameter is the number of atomic planes composing the sample
Oleg Komarov
Oleg Komarov 2011-5-31
I am not sure that you are supplying your problem in the required form, i.e. in lsqcurvefit you can supply a zero ydata and you'll end up with the min wrt nz of SUM{f(.)^2} where the fcn handle is cast in the form I supplied in the body.

请先登录,再进行评论。

更多回答(4 个)

Walter Roberson
Walter Roberson 2011-5-27
f = @(x) sum(exp(1i.*c.*x.*(1:nz)))
  1 个评论
gianluca messina
gianluca messina 2011-5-27
Thanks for your help, but I find this error when I run that function:
Undefined function or method 'isnan' for input arguments of type 'function_handle'.
Gianluca

请先登录,再进行评论。


Arnaud Miege
Arnaud Miege 2011-5-27
I'm not sure this is something that can be done with the Curve Fitting Toolbox. I think a better approach would be to use the Optimization Toolbox with one of the Least Squares (Curve Fitting) functions, such as lsqcurvefit.
HTH,
Arnaud

gianluca messina
gianluca messina 2011-5-30
Hi Arnaud, I followed your advice, the problem is that when I calculate the sum both using an iteration and f = @(x,xdata) sum(exp(1i.*x(1).*xdata.*(1:(x(2))))), matlab doesn't recognize that is a function of xdata. So I can't fit it because it sees f and ydata as data of different dimension. Thanks for your help. (And thanks Oleg).
  1 个评论
Oleg Komarov
Oleg Komarov 2011-5-30
Please post the code snippet you're using (preferably editing this question) and the whole error message. Additional info about the size of your inputs would be beneficial.

请先登录,再进行评论。


gianluca messina
gianluca messina 2011-5-30
Here is the code:
clear all; close all;
load 'C:\Users\gianluca\Desktop\002Cr-V_Lscan\Cr_MgO_69.txt';
xdata = (Cr_MgO_69(:,1)*2/292.094)'; ydata = (Cr_MgO_69(:,2))';
fh = @(x,xdata) sum(exp(1i.*x(1).*xdata.*(1:x(2)))) .* conj(sum(exp(1i.*x(1).*xdata.*(1:x(2)))));
x0 = [3e-10; 100]; x = lsqcurvefit(fh,x0,xdata,ydata);
and this is the error:
??? Error using ==> lsqncommon at 101 LSQCURVEFIT cannot continue because user supplied objective function failed with the following error: Error using ==> times Matrix dimensions must agree.
Error in ==> lsqcurvefit at 186 [x,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...
Should I try to use an iteration maybe to define the funtion, instead of fh = @(x,xdata)...*(1:x(2))..? I tried but I don't know how to insert x(2) as parameter if it is at the beginning of the for cycle and so before the statement of the funtion:
for jz = 1 : x(2) ... f(jz) = @(x,xdata) ...jz...
end
Yesterday I was using some different code, and I got another error:
??? Error using ==> lsqncommon at 134 Function value and YDATA sizes are incommensurate. (but I think the code I used contained some mistakes)
Many thanks Gianluca

类别

Help CenterFile Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by