fitting with y and x dependent variables
2 次查看(过去 30 天)
显示 更早的评论
I have 2 columns of universal data X and Y. I can plot an universal Y as a function of X.
Now, X = x / Xc. And Y = y / Yc.
x and y are my experimental data.
Xc and Yc are the fitting parameters that I need to retrieve. In other word, I need to find Xc and Yc in order to minimize the difference between the Y versus X (that I know) and the y*Yc versus x*Xc
How can I do that ?
14 个评论
Torsten
2022-6-13
So you want to minimize
sum_i [(Xi*Xc - xi).^2 + (Yi*Yc - yi).^2]
in which Xc and Yc are the fitting parameters and X, Y, x, y are given vectors ?
Torsten
2022-6-13
@laurent jalabert comment moved here:
Yes, what you wrote is the minimum vector length between data and target array of normalized data.
Notice that x and y vectors have different length compared to X and Y.
Torsten
2022-6-13
Notice that x and y vectors have different length compared to X and Y.
Why ?
I thought X = x / Xc and Y = y / Yc where Xc and Yc are scalars. Thus x and X as well as y and Y should have the same length.
Torsten
2022-6-13
@laurent jalabert comment moved here:
first of all, thank you so much to take your time to answer my problem.
You are right, they should have same length. But in the real life of experiments, x and y have less available data point, compared to what computers can output from complicated model. That is why x,y have not same dimension than X and Y.
Torsten
2022-6-13
Please use the "comment" button instead of the "answer" button when you want to make comments.
Torsten
2022-6-13
编辑:Torsten
2022-6-13
That is why x,y have not same dimension than X and Y.
Then you should supply only those X and Y data that correspond to the x and y experimental data.
E.g. if x and y were time-dependent experimental data measured at times t, then you also should only use those X and Y data at the times t of the experimental measurements for the fitting process.
Is this possible ?
Image Analyst
2022-6-13
@laurent jalabert If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
laurent jalabert
2022-6-13
[I am in the comment domain]
x,y are not time dependent data. Xc and Yc are 2 parameters that have to be found as fitting parameters, in order (as you said) to minimize the distance between experimental points and an universal curve provided as a 2 columns array of X = x/Xc and Y = y/Yc. I know X and Y, I know x and y, but I do not know Xc and Yc.
The "shape" of y versus x is about the same than the shape of Y versus X, and a x-axis sliding and y-axis sliding factors that are Xc and Yc.
I am pretty sure that a non linear regression fitting might be programmable, but I do not know how to declare a variable in the x area.
Another approach I though is to plot the universal Y versus X on a graph, and to plot the x and y data. If there was a sliding in x-axis and a sliding in y-axis so that graphically I can superpose the 2 curves, then get the sliding factors, this might be a first approach to solve the problem, even it is not the most relevant.
laurent jalabert
2022-6-13
编辑:laurent jalabert
2022-6-13
OK for the tutorial, I am using this forum for several 20 years maybe. If I post something, that is because it is not trivial. You can find maybe an appropriate way of solving this problem if you master Mathematica where some optimization functions are native. But with Matlab, at some point, the fitting is becoming very complicated, especially for the problem that I am presenting in this post.
In order to have a better understanding, I post (as requested) the theory and a set of my experimental data. OK I admit that I should provide real/Imag parts for both, but it does not change the core of the fitting problem.
Theory.xlsx : Column 1 is X, column 2 is Real(Y), and column 3 is Imag(Y). Those are the universal curves for real and imaginary parts of complex Y.
Experiment.xlsx : I attached a set of experimental data x and y, where column 1 is x, column 2 is the amplitude(y) and column 3 is the phase(y).
y = real(y) + imag(y) = amplitude(y).*exp(i*phase(y))
This slider_plot could have been interesting to slide the experimental points until matching the theory curve, but I could not succeed to use it correctly. This is just a quick way to estimage Xc and Yc. But this is not optimized.
https://fr.mathworks.com/matlabcentral/answers/374973-update-plot-based-on-two-sliders
laurent jalabert
2022-6-15
Is it doable with Matlab ?
I have an array of theoretical data that are x/Xc and y/Yc.
In my experiment, I measure x and y, and I want to find Xc and Yc to fit this curve with the theroretical array above.
I don't know Xc and Yc a priori. I know that if I multiply x by a certain 1/Xc and if I multiply y by a certain 1/Yc, then I can be closed to the theoretical array.
Torsten
2022-6-15
编辑:Torsten
2022-6-15
Suppose you are given Xc and Yc.
Then you can calculate x/Xc and y/Yc.
Now with which X and Y value do you want to compare x/Xc and y/Yc in the optimization process ?
Taking the X that is nearest to x/Xc and the corresponding Y in the table of theory data to compare with y/Yc ?
Or taking the Y value that is nearest to y/Yc and the corresponding X in the table of theory data to compare with x/Xc ?
Or ...
laurent jalabert
2022-6-15
编辑:laurent jalabert
2022-6-16
Now with which X and Y value do you want to compare x/Xc and y/Yc in the optimization process ?
--> X and Y data from the column 1 and 2 of the file theory.xlsx
x,y are experimental data. Xc and Yc are fitting parameters that I want to output. But the model for fitting is not a function, it is array of theoretical points X and Y, with X = x/Xc and Y=y/Yc.
To feel what I what to do, yes, you can define Xc and Yc arbitrary, and plot scaled data (by Xc and Yc) and theory in order to see if it matches or not. I recommend log scale display. If you do that, intuitively, you will find Xc about 100 and Yc about 3.5. But this is indeed not accurate nor optimized.
Now, what I want to do can be done in Mathematica but I want to use Matlab to include this fit into my main data analysis program (already 4000 lines of code).
Basically, in Mathematica, the steps are like this :
1- load theoretical data column 1 (x/Xc) and 2 (real part y/Yc) % data_theo(:,1) and data_theo(:,2)
2- import experimental data x and y
3- define RP as interpolation order 1 of the table of experimental data (x, y) to the length of theoretical vector length(data_theo(:,1)); I can use linspace or logspace.
4- (important) define modelR = Yc * RP[x/Xc] ; % RP as a function of x/Xc
5- fittingR = Quiet[NonlinearModelFit[y, {modelR, {Yc > 0, Xc > 0}}, {Yc, Xc}, x]];
non linear regression fit on data y, using the interpolation modelR, for Yc positive and Xc positive, {Yc,Xc} being the 2 fitting parameters, and x the abscisse from experimental data.
By using Mathematica with the set of data that I provided, the output parameters are Yc = 3.05 with std=0.0122, and Xc = 187.696 with std = 4.025, for a fit R^2 = 0.999944.
Now, on Matlab, I think that I should use griddedinterpolant or scatteredinterpolant .
For point 4, How to define the modelR as a function of the interpolation function that depends on x/Xc (Xc a fit parameter) i.e how to translate the modelR that included RP[x/Xc] in Matlab code ?
modelR = @({Yc,Xc},x) Yc*RP[x/Xc]
% point 1
ds_theo = spreadsheetDatastore('/Users/name/Desktop/theory.xlsx',"FileExtensions",[".xlsx",".xls"]);
data_theo = table2array(read(ds_theo));
% point 2
ds_exp = spreadsheetDatastore('/Users/name/Desktop/experiment.xlsx',"FileExtensions",[".xlsx",".xls"]);
data_exp = table2array(read(ds_exp));
% point 3
% RP = scatteredInterpolant(data_exp,data_theo) ; % does not work
%% plot
FigList = findobj(allchild(0), 'flat', 'Type', 'figure');
nbfig = size(FigList,1);
fig(nbfig+1) = figure('PaperUnits','inches','PaperType','A4','PaperOrientation',...
'landscape','Color',[1 1 1], 'OuterPosition',[1 1 600 600]);
semilogx(data_theo(:,1),data_theo(:,2),'LineWidth',2);
hold on
semilogx(data_theo(:,1),data_theo(:,3),'LineWidth',2);
grid on
% plot experimental data
hold on
semilogx(data_exp(5:end,1),data_exp(5:end,2),'o','LineWidth',2);
hold on
semilogx(data_exp(5:end,1),wrapTo2Pi(data_exp(5:end,3)),'o','LineWidth',2);
legend('theo RP','theo IM','exp RP','exp IM')
set(gca,'FontSize',14,'LineWidth',1)
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)