Curve fitting with two variables

Hi community!
I have y = f(x), and z = f(x,y), now i want to fit z vs y and need final graph like z vs y. In general, it can be simply done by replecing x by its y equivalent in z but here it is not possible since x-variable is in the integral.
Thank you for your help!

11 Kommentare

William Rose
William Rose am 28 Dez. 2021
Provide a more specific example of the functions or data you wish to fit. Are you fitting experimental data? If so, can you post a sample?
You say "I have y = f(x), and z = f(x,y)...". In the first instance, f() is a function of one variable, and in the second instance, f() is a function of two variables. That does not make sense. You also say "x-variable is in the integral". What integral?
Please clarify.
Kishor Kumar Johari
Kishor Kumar Johari am 28 Dez. 2021
Thank you so much @William Rose for quick response!
For more clearly expressing the question, I am attaching a snapshot. Kindly, have a look and please help me.
William Rose
William Rose am 28 Dez. 2021
@Kishor Kumar Johari, you say "the range of x can be arbitrarily given for fitting". Does that mean we can adjust both a and x in order to get a good fit?
Can you provide some sample data, i.e. a list of z,y pairs?
I would use fmincon() to find the values of a and x that minimize the squared difference between measured z and predicted z. Threfore you will need a Matlab function that computes z, using equation 3 above, given a, x, and y.
Here is Matlab code to evaluate z, given a, x, and y:
a=1; x=0; y=1;
funNum = @(t,x) t.^-0.5./(1+exp(t-x));
funDen = @(t,x) 1./(1+exp(t-x));
integralNum = integral(@(t) funNum(t,x),0,+Inf)
integralNum = 1.0722
integralDen = integral(@(t) funDen(t,x),0,+Inf)
integralDen = 0.6931
z=15*(y*integralNum/(16*(2*a)^1.5*integralDen)-x)
z = 0.5127
Put the lines above into a function, as shown in the attached code. Then write a function sseZY() that computes the sum squared error between measured z and predicted z. The arguments to sseZY() will be a and x, the values which you want to adjust in order to get the best fit. sseZY() will also need to know the measured y's, in order to calculate z, and he meausred z's in order to computed the sum squared error. sseZY() will call calcZ() N times, where N is the number of z,y pairs.
See the Matlab help for fmincon() for examples of this process.
Generate simulated (y,z) pairs for testing, and save it in file zsim.mat:
a=1; x=0; y=1:10;
for i=1:10, zmeas(i)=calcZ(a,x,y(i))+randn; end %add Gaussian noise to each
data=[y',z'];
save('zsim','data');
See attached file, which you can load in Matlab.
William Rose
William Rose am 28 Dez. 2021
Here is a sample solution. It uses calcZ(), which I posted previously, and it uses the simulated data file which I posted previously. The main program is fitKKJ.m. IT calls fmincon() and it passes funciton sseZY() to fmincon(). Funciton sseZY is attached. There are comments in fitKKJ.m and in sseZY.m which explain how each works.
Image Analyst
Image Analyst am 28 Dez. 2021
William, can you put your sample solution down below in the official "Answers" section - click the Answer this question blue button. Up here in the comments section is where you're just ask the poster for clarification. You can get "reputation points" if you post down there and the poster votes for, or accepts, your Answer.
John D'Errico
John D'Errico am 28 Dez. 2021
Bearbeitet: John D'Errico am 28 Dez. 2021
Note that at least one of these integrals has a direct special function form. For example:
syms x t
int(t/(1+exp(t-x)),t,0,inf)
ans = 
Where polylog is the polylogarithm function.
help polylog
--- help for double/polylog --- POLYLOG polylogarithm function. POLYLOG(0,X) = X./(1-X) POLYLOG(1,X) = -log(1-X) POLYLOG(N+1,X) = int(polylog(N,t)/t,t,0,X) for 0 < X < 1 and any N; or for N >= 1 and any X Documentation for double/polylog doc double/polylog Other functions named polylog single/polylog sym/polylog
I'm reminded of the joke about a famous visiting professor in mathematics and physics, who when asked to give a talk to the students, accepts. He stands at a blackboard (do they even have blackboards anymore?) and covers it with long arcane messes of symbols, filled with various special functions, etc. After a while, the silnece of the students gets to him, so he turns around, and asks the students to raise their hand if they have never seen a Bessel function. One student finally timidly raises their hand, as if to concede they have never seen Bessel functions. The professor nods his head, then turns back to point at the blackboard, with only the statement "There's one now." Then he continues writing on the blackboard.
So, well, there's a polylogarithm.
William Rose
William Rose am 28 Dez. 2021
@Image Analyst, thank you for the suggestion.
@John D'Errico, Good story! I am raising my hand because I've never seen a polylogarithm before.
Kishor Kumar Johari
Kishor Kumar Johari am 7 Jan. 2022
@William Rose Thank you so much for the MATLAB code!
I'll try to fit my data using the provided code.
Kishor Kumar Johari
Kishor Kumar Johari am 7 Jan. 2022
@William Rose Thank you so much for the MATLAB code!
I'll try to fit my data using the provided code.
William Rose
William Rose am 7 Jan. 2022
@Kishor Kumar Johari, You're welcome. Good luck with your work!

Melden Sie sich an, um zu kommentieren.

Antworten (1)

William Rose
William Rose am 28 Dez. 2021
Bearbeitet: William Rose am 28 Dez. 2021

0 Stimmen

Here is a sample solution. It uses calcZ(), which I posted previously, and it uses the simulated data file which I posted previously. The main program is fitKKJ.m. It calls fmincon() and it passes function sseZY() to fmincon(). Function sseZY is attached. There are comments in fitKKJ.m and in sseZY.m which explain how each works.
Here is the console output when I run the script:
fitKKJ
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the value of the constraint tolerance.
<stopping criteria details>
Best fit values: a=0.771, x=0.047.
The simulated data (ten y,z pairs) was generated with a=1, x=0, and Gaussian noise was added.
The script also plots the measured (or simulated, in this case) data and the best-fit approximation. See plot below.

1 Kommentar

Kishor Kumar Johari
Kishor Kumar Johari am 7 Jan. 2022
@William Rose First of all thank you so much for the efforts you made!
I think there is some confusion about 'x'. It is not a constant of fit, 'a' is only the constant of fit.
'x' is the variable on which 'y' as well as 'z' depends. Here, I would like to explain the problem in detail for more clarity.
There are total four parameters 'x', 'y', 'z', and 'a' are to play. Experimetally, 'y' and 'z' are measured, and 'x' can easily be determined using the equation (2) for each value of 'z' which is corressponding to 'y' . Here, it is to be noted that ultimately we get a dataset for all of three 'x', 'y', 'z'. Now, it is simple when we have to calculate 'a' for only a single set of data, for example we have an experimetally measured z = 0.002 corressponding to y = 3.5, then 'x' can easily be calculated using 'z' like, x = 1.2 (taken arbitrary number for example, not exactly calculated), then using the values of 'z' and 'x', 'a' can simply be calculated.
But the problem arises, when we have to fit a data set with n number of data points, to calculate 'a' as constant of fit.
Now, the problem statement be like we have one input parameter 'x' for generating theoretical data for the fit, and the fit plot should appear 'z' vs 'y'.
Here, it is to be noted that the meaning of the range of 'x' can arbitrarily be given means that an arbitrary data set can be given as input to generate data for 'y' and 'z'.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Get Started with Curve Fitting Toolbox finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 27 Dez. 2021

Kommentiert:

am 7 Jan. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by