Curve fitting with two variables
Mostra commenti meno recenti
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 Commenti
William Rose
il 28 Dic 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
il 28 Dic 2021
William Rose
il 28 Dic 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.
William Rose
il 28 Dic 2021
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.
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
il 28 Dic 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
il 28 Dic 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
il 28 Dic 2021
Modificato: John D'Errico
il 28 Dic 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)
Where polylog is the polylogarithm function.
help 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
il 28 Dic 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
il 7 Gen 2022
Kishor Kumar Johari
il 7 Gen 2022
William Rose
il 7 Gen 2022
@Kishor Kumar Johari, You're welcome. Good luck with your work!
Risposte (1)
William Rose
il 28 Dic 2021
Modificato: William Rose
il 28 Dic 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 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 Commento
Kishor Kumar Johari
il 7 Gen 2022
Categorie
Scopri di più su Get Started with Curve Fitting Toolbox in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

