Issue using lsqcurvefit function - App Designer

1 次查看(过去 30 天)
Good morning,
I'm new to App Designer, and I need to solve an optimization problem using the lsqcurvefit function.
I reuse pre-existing calculation codes, I defined a function
F1=myfun_root(app,x,Speed_sample_RPM)
as a private function in the application code. In this same code, I call the lsqcurvefit function in a CallBack Button:
[x,resnorm,residual,exitflag,output] = ...
lsqcurvefit(@myfun_root,app.x0,Speed_sample_RPM_zoom_T,yt_sample_zoom_Total_Nondim,app.lb,app.ub);
All the variables used are defined previously in the code, which I cannot copy/paste here.
When I run the application and click on the button to calculate the interpolation which launches the CallBack including the lsqcurvefit function, I get the error message:
Error using nargin
Function myfun_root does not exist.
While the latter is well defined as a function at the beginning of the program.
I searched on the Q/A of Matlab but I did not find an already existing answer to this concern, of which I do not understand the cause.
Could someone please enlighten me?
I thank you in advance !
  1 个评论
Torsten
Torsten 2023-5-26
编辑:Torsten 2023-5-26
While the latter is well defined as a function at the beginning of the program.
As a function handle ? If it is defined as a function, it must be included at the end of the program (script).

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2023-5-26
Since you didn't share your code, I created a simple example in app designer using this example from the lsqcurvefit documentation page. I made the following observations after comparing to what you've shared.
You haven't defined your function correctly. I would expect it to look something like this
function F1=myfun_root(app,x,Speed_sample_RPM,Speed_sample_zoom)
In addition, your calling syntax needs to be modified to work inside app designer.
[x,resnorm,residual,exitflag,output] = ...
lsqcurvefit(@app.myfun_root,app.x0,Speed_sample_RPM_zoom_T,yt_sample_zoom_Total_Nondim,app.lb,app.ub);
This of course assumes all other variables have been propertly defined.
  2 个评论
Hélène
Hélène 2023-5-26
Thank you for your two answers which made it possible to solve this first problem. @Cris la Pierre I'm not sure I understand adding a third argument to the F1 function.
When I run the code now I get another error message:
Error using assert
FUN must have two input arguments.
However, the myfun_root function has two input arguments, x0 and Speed_sample_RPM_zoom, so I don't really understand the correction to be made.
I'm sorry, this may seem basic to you, but I'm new to Matlab on the one hand and App Designer on the other.
I thank you in advance !
Cris LaPierre
Cris LaPierre 2023-5-26
That was me trying to take a guess at what your code was doing. I had to try a couple things to get the example to run in App Designer, and just didn't go back and minimize the code.
You can remove the 3rd input argument from myfun_root
function F1=myfun_root(app,x,Speed_sample_RPM)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by