Using lsqcurvefit in a Matlab App

2 vues (au cours des 30 derniers jours)
Laura Andre
Laura Andre le 11 Déc 2019
Commenté : Laura Andre le 11 Déc 2019
I am using App Designer in Matlab 2019b to create an app that takes in experiemental data and then fits it to a theoretical expression. I am writing to ask how one might implement the lsqcurvefit function within a Matlab App? The function has the following inputs: lsqcurvefit(fun,x0,xdata,ydata). I have successful used the the lsqcurvefit function to accomplish this task by calling it in a normal Matlab script (i.e. *.m file), but I am have not been able to figure out a way to use it within a Matlab app (i.e. *.mlapp file). The issue is that the nonlinear function fun(x,xdata) to be optimized must take in two inputs - first the variables you want to optimize and second the data you want to fit. I am defining the fucntion within the app, so app needs to be the first input to the function. Further, both the optimization variables and the data are inputted to the app, so they need to be called as app.x and app.xdata. If I keep app the first input to fun then I get the following error:
Error using lsqfcnchk (line 108)
FUN must be a function, a valid character vector expression, or an inline function object.
If I remove app from the inputs fun of then I get the following error:
Undefined function 'fun' for input arguments of type 'double'.
I didn't think this would work anyway, since I need to be able to pass the input app into fun so that I can use the app's global vaiables within that function.
Has anyone encountered this or found a work around to use lsqcurvefit within App Designer? Hope the question and issues are clear. Let me know if you need me to post more code. Thanks in advance for the assitance.
  2 commentaires
Adam Danz
Adam Danz le 11 Déc 2019
Modifié(e) : Adam Danz le 11 Déc 2019
" I am defining the fucntion within the app, so app needs to be the first input to the function."
I assume the function is defined within app as something like app.optiFcn. In that case, you wouldn't pass app into the lsqcurvefit function. You would pass in lsqcurvefit(app.optiFnc,...).
Further, both the optimization variables and the data are inputted to the app, so they need to be called as app.x and app.xdata. "
I don't see what the problem is with doing that. Note that you can pass additional parameters into lsqcurvefit() using this method:
Maybe a snippet of code would be useful to convey the problem.
Laura Andre
Laura Andre le 11 Déc 2019
Here is a snippet of the code I am working with.
properties (Access = public)
function time_vec = fun(app,guess,time)
I_time_vec = zeros(size(time));
for i = 1:length(time)
time_vec(i) = int_I(app,guess,time(i)); %Other function int_I computes the integral in the theoretical expression I am modeling
end
end
end
methods (Access = private)
% Button pushed function: FitTLSDataButton
function FitDataButtonPushed(app, event)
fit_values = lsqcurvefit(@fun,app.guess,app.time,app.data,app.lb,app.ub);
end
end
To your first point, I am not trying to pass app in the lsqcurvefit function. I am trying to pass app in the nonlinear function to be fitted, fun.
To your second point, I thought you had to pass app as an input in order to call something like app.xdata within that function. Sorry if that I misunderstood that, I am new to working with the App Designer.

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 11 Déc 2019
Modifié(e) : Matt J le 11 Déc 2019
Here is a snippet of the code I am working with.
No, the code should look like this,
methods (Access = private)
% Button pushed function: FitTLSDataButton
function FitDataButtonPushed(app, event)
fit_values = lsqcurvefit(@(x,xd) app.fun(x,xd),...
app.guess,app.time,app.data,app.lb,app.ub);
end
function time_vec = fun(app, guess,time)
I_time_vec = zeros(size(time));
for i = 1:length(time)
time_vec(i) = int_I(app,guess,time(i)); %Other function int_I computes the integral in the theoretical expression I am modeling
end
end
end
  1 commentaire
Laura Andre
Laura Andre le 11 Déc 2019
Calling the functions that way worked. Thank you so much!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Develop Apps Using App Designer dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by