Fitttype inside app designer

11 views (last 30 days)
Shankar Dutt
Shankar Dutt on 16 Dec 2019
Answered: Abhishek Kumar on 29 Sep 2020
I am having trouble using fittype inside app designer.
ft = fittype("HC(x,Radius,Amp,Background,Pd)");
returns Expression HC(x,Radius,Amp,Background,Pd) is not a valid MATLAB expression, has non-scalar coefficients, or cannot be evaluated:
Error in fittype expression ==> HC(x,Radius,Amp,Background,Pd)
??? Undefined function 'HC' for input arguments of type 'double'.
but I have defined the function HC.
I even tried ft = fittype("HC(x,Radius,Amp,Background,Pd)");
but it doesn't help.
function HC_Fitting(app)
%28th November 2019 - Shankar Dutt -shankar.dutt@anu.edu.au
%It initiates the fitting procedure
if exist('Graph')
delete(app.Graph)
end
xData = app.q;
yData= app.r;
ft = fittype("HC(x,Radius,Amp,Background,Pd)");
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'final';
opts.MaxFunEvals = 60000;
opts.MaxIter = 40000;
opts.TolFun=1e-6;
opts.TolX=1e-6;
Radius=app.CoreRadiusEditField.Value;
Amp=app.AmplitudeEditField.Value;
Background=app.BackgroundEditField.Value;
Pd=app.PolydispersityEditField.Value;
opts.StartPoint = [Amp Background Pd Radius];
opts.Lower = [0 0 0 0];
opts.Upper = [Inf 50 0.5 Inf];
% Fit model to data.
[fitresult, ~] = fit( xData, yData, ft, opts );
app.CoreRadiusEditField.Value=fitresult.Radius;
app.AmplitudeEditField.Value=fitresult.Amp;
app.BackgroundEditField.Value=fitresult.Background;
app.PolydispersityEditField.Value=fitresult.Pd;
HC_plot(app);
end
function y=HC(app,q,Radius,Amp,Background,Pd)
%28th November 2019 - Shankar Dutt -shankar.dutt@anu.edu.au
%This function incorporates the Hard Cylinder FormFactor and Gaussian
%Distribution
syms z;
%Amp = 0.01;Background=0.001;Radius=400;Pd=20;
y1 = @(z) HC_formfactor(app,q,z); % This loads the Form_Factor of Hard Cylinder Model
%In the following we define the distribution and its integration.
%Limits are technically 0 to infinity but I have chosen them in a way that
%it gives results without much computational energy.
va = Radius - app.NumptsEditField.Value*Pd; %This is the lower limit of the integration
vb = Radius + app.NumptsEditField.Value*Pd;%This is the upper limit of the integration
if(va<0)
va=0;
end
D =@(z) LN_Distribution(app,z,Radius,Pd);
fun = @(z) y1(z).*D(z);
y1=integral(D,va,vb,'ArrayValued',true);
y=integral(fun,va,vb,'ArrayValued',true)/y1;
y=abs(y.*Amp-Background);
end

Answers (1)

Abhishek Kumar
Abhishek Kumar on 29 Sep 2020
As I understand your are trying to use fittype function, as you have already defined the function HC, please use the following syntax to resolve the error.
ft = fittype(HC(x,Radius,Amp,Background,Pd));
You can refer to the following documentation of fittype for further reference:

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by