**You are now following this question**

- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.

# fitting with y and x dependent variables

5 views (last 30 days)

Show older comments

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 Comments

Torsten
on 13 Jun 2022

@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
on 13 Jun 2022

@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
on 13 Jun 2022

Edited: Torsten
on 13 Jun 2022

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
on 13 Jun 2022

laurent jalabert
on 13 Jun 2022

[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
on 13 Jun 2022

Edited: laurent jalabert
on 13 Jun 2022

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
on 13 Jun 2022

fortunately, Xc and Yc are not complex.

laurent jalabert
on 15 Jun 2022

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
on 15 Jun 2022

Edited: Torsten
on 15 Jun 2022

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
on 15 Jun 2022

Edited: laurent jalabert
on 16 Jun 2022

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)

### Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

- América Latina (Español)
- Canada (English)
- United States (English)

Europe

- 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)

Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)