Using fittype with a symbolic function

18 visualizzazioni (ultimi 30 giorni)
Emily
Emily il 18 Ott 2023
Risposto: Torsten il 19 Ott 2023
I have a symbolic function that is the result of a couple of symbolic integrations and some other manipulation. To give you some idea, here it is expressed to two digits:
vpa(qME,2)
ans =
0.28*real(exp(2.7e-4*prob_10)*exp(prob_10^(1/2)*exp(-phi_10*1.0i)*(1.0 - 1.0*prob_10)^(1/2)*(- 0.9 + 7.1e-4i))*exp(prob_10^(1/2)*exp(phi_10*1.0i)*(1.0 - 1.0*prob_10)^(1/2)*(- 0.9 - 7.1e-4i)))
And it is a function of two symbolic variables:
symvar(qME)
ans =
[phi_10, prob_10]
I also have some data on a 100 by 100 grid, created with accumarry (it is a probability distribution).
size(ProbinBinsNoZero)
ans =
100 100
I have set up a grid:
[x,y] = meshgrid(1:size(ProbinBinsNoZero',2), 1:size(ProbinBinsNoZero',1));
The grid above represents a discretization of the region and . So I'll need to normalize x and y for them to represent prob_10 and phi_10.
I'd like to fit the data in ProbinBinsNoZero with the function
offset + scale*qME
so I ran the command:
fitMaxEnt = fittype( @(offset, scale) (offset+scale*qME), 'independent', {'phi_10','prob_10'} );
and matlab complains:
The independent variable phi_10 does not appear in the equation expression.
Use phi_10 in the expression or indicate another variable as the independent variable.
presumably because even though I named the independent variables the same thing as the sym variables, they are not really the same thing?
If the fittype had worked, I would have then used:
[sfMaxEnt, gofMaxEnt] = fit([2*pi*x(:)/100, y(:)/100],ProbinBinsNoZero(:),fitMaxEnt)
(though I haven't tested this and am not sure this is the proper way to normalize x and y).
I also tried creating a function handle:
hqME = matlabFunction(qME);
and using that in place of qME in the definition of the fit type, but that didn't help.
Can you please tell me the proper way to use a symbolic function in fittype, in order to fit my data?
Or how to change the symbolic function into one that I can use in fittype?
Thanks!

Risposta accettata

Torsten
Torsten il 19 Ott 2023
The problem was solved by converting the symbolic function "qme" to a numerical function handle by using
qme_numeric = matlabFunction(qme);
and definining the function to be fitted (with fitting parameters offset and scale) as
qme2 = @(offset,scale,phi_10,prob_10) offset + scale*qme_numeric(phi_10,prob_10);
Then the call to fittype and fit worked:
fitMaxEnt = fittype(@(offset,scale,phi_10,prob_10)qme2(offset,scale,phi_10,prob_10), 'independent', {'phi_10','prob_10'} );
[sfMaxEnt, gofMaxEnt] = fit([2*pi*x(:)/100, y(:)/100],ProbinBinsNoZero(:),fitMaxEnt)
  1 Commento
Emily
Emily il 19 Ott 2023
Spostato: Star Strider il 19 Ott 2023
qme2 = @(offset,scale,phi_10,prob_10) offset + scale*qme_numeric(phi_10,prob_10)
qme2 =
function_handle with value:
@(offset,scale,phi_10,prob_10)offset+scale*qme_numeric(phi_10,prob_10)
>> qme2(1,0.5,1,1)
ans =
1.139428821070130
and now my fittype is successful:
fitMaxEnt = fittype(@(offset,scale,phi_10,prob_10)qme2(offset,scale,phi_10,prob_10), 'independent', {'phi_10','prob_10'} );
[sfMaxEnt, gofMaxEnt] = fit([2*pi*x(:)/100, y(:)/100],ProbinBinsNoZero(:),fitMaxEnt)
Warning: Start point not provided, choosing random start point.
> In curvefit.attention/Warning/throw (line 30)
In fit>iFit (line 318)
In fit (line 117)
General model:
sfMaxEnt(phi_10,prob_10) = qme2(offset,scale,phi_10,prob_10)
Coefficients (with 95% confidence bounds):
offset = -6.688e-05 (-6.795e-05, -6.58e-05)
scale = 0.0005243 (0.0005213, 0.0005273)
gofMaxEnt =
struct with fields:
sse: 6.191350836297529e-06
rsquare: 0.920723255003681
dfe: 9998
adjrsquare: 0.920715325743330
rmse: 2.488491381172208e-05
figure; plot(sfMaxEnt,[2*pi*x(:),y(:)]/100,(ProbinBinsNoZero(:)));
Thank you Torsten! Would you like to write up an answer for me to accept? And would you like to explain what is different between the versions that worked and those that didn't? I am not used to thinking about functions in matlab, so the syntax is sort of mysterious for me. If not I can just make an answer with the correct syntax based on your comments.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Linear and Nonlinear Regression in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by