How to insert a function handle in simulink !?

조회 수: 4 (최근 30일)
Suppenhuhn
Suppenhuhn 2011년 3월 10일
Hi,
I wrote a function f(x)=a*x(1)+b*x(2) in Matlab (without Simulink) as an .m-file and let ga(@function,2) calculate an optimum for given a,b. My next step is to insert that situation into Simulink - so, that a closed loop delivers different a,b with every step. How can I now implement my genetic algorithm (ga(@function,2)) ?
I actually wrote the function as s-function (with two inputs for a,b). Do I maybe have to write an Block Property/Callback with the command ga(@function,2) or how can I place my gen. algo. !?

답변 (2개)

Guy Rouleau
Guy Rouleau 2011년 3월 11일
In a Level 2 MATLAB S-function, you should be able to use your function in the same way as you do in MATLAB.
Do you receive any error?

Suppenhuhn
Suppenhuhn 2011년 3월 11일
Well - it doesn´t work !? I´m just a bit confused how to insert the order, that I (from pure Matlab) would enter from Workspace. In my Matlab-version I just write evalfis([5 7],[fisdata]) in my .m-function and the optimal Minimum for my function (f(x)=a*x(1)+b*x(2)) is getting calculated by writing ga(@function,2)(for given a,b (=5,7)). But I don´t know how to insert the ga-command in a Simulink-loop, where a and b are getting constantly changed. As I didn´t know better, I wrote a "Letsgo.m"-file with ga(@function, 2) and for my s-function I added a Callback in Block Properties/OpenFcn with the command set_param('Letsgo').
The resulting error is: "Error evaluating registered method 'Outputs' of M-S-Function 'testi_s' in 'Trink_s/Level-2 M-file S-Function'. The following error occurred converting from double to opaque: Error using ==> horzcat Undefined function or method 'opaque' for input arguments of type 'double'. The following is the MATLAB call stack (file names and line numbers) that produced this error: ['C:\...\...\...\testi_s.m'] [126]"
Line 126 is the first line in my s_function that has the parameter a or b !?

카테고리

Help CenterFile Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by