Using fittype with a symbolic function
18 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
Torsten
on 19 Oct 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 Comment
More Answers (0)
See Also
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!