Using fittype with a symbolic function
조회 수: 19 (최근 30일)
이전 댓글 표시
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
채택된 답변
Torsten
2023년 10월 19일
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)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!