Using lsqcurvefit in a Matlab App

조회 수: 3 (최근 30일)
Laura Andre
Laura Andre 2019년 12월 11일
댓글: Laura Andre 2019년 12월 11일
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
Adam Danz
Adam Danz 2019년 12월 11일
편집: Adam Danz 2019년 12월 11일
" 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 2019년 12월 11일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2019년 12월 11일
편집: Matt J 2019년 12월 11일
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
Laura Andre
Laura Andre 2019년 12월 11일
Calling the functions that way worked. Thank you so much!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by